fluent / fluent-bit-plugin

Fluent Bit Dynamic Plugin Development
Apache License 2.0
13 stars 12 forks source link

linker issues when building a standalone plugin + fix suggestion #10

Open jordanauge opened 1 year ago

jordanauge commented 1 year ago

Hi,

I followed the steps in the README to compile the sample output plugin as standalone.

As suggested i have cloned the fluent-bit source code from current master, and have the cmake command point to it through FLB_SOURCE

I did not test using header packages either, since there is this open issue that i also faced: https://github.com/fluent/fluent-bit/issues/7028

I also had to follow this comment and run cmake to have autogenerated files working: https://github.com/fluent/fluent-bit-plugin/issues/2#issuecomment-520614809

So far i could only test this on MacOS, and I still have the following error:

[100%] Linking C shared library ../flb-out_stdout2.dylib
Undefined symbols for architecture arm64:
  "_co_switch", referenced from:
      _flb_output_return_do in stdout2.c.o
  "_flb_coro_get", referenced from:
      _flb_output_return_do in stdout2.c.o
  "_flb_errno_print", referenced from:
      _flb_output_return in stdout2.c.o
  "_flb_event_chunk_destroy", referenced from:
      _flb_output_return in stdout2.c.o
  "_flb_output_flush_prepare_destroy", referenced from:
      _flb_output_return in stdout2.c.o
  "_flb_output_thread_instance_get", referenced from:
      _flb_output_return in stdout2.c.o
  "_flb_pack_print", referenced from:
      _cb_stdout_flush in stdout2.c.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [flb-out_stdout2.dylib] Error 1
make[1]: *** [out_stdout2/CMakeFiles/flb-out_stdout2.dir/all] Error 2
make: *** [all] Error 2

A fix that worked for me was to update the FLB_PLUGIN_MACRO with code borrowed from here: https://github.com/icn-team/cmake-modules/blob/a7b8a8e60fb4b4d35df628003f1d80438736603b/Modules/BuildMacros.cmake#L334

The working macro:

macro(FLB_PLUGIN name src deps)
  add_library(flb-${name} SHARED ${src})
  set_target_properties(flb-${name} PROPERTIES PREFIX "")
  set_target_properties(flb-${name} PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

  if (${CMAKE_SYSTEM_NAME} MATCHES Darwin)
    list(APPEND ARG_LINK_FLAGS "-Wl,-undefined,dynamic_lookup")
  elseif(${CMAKE_SYSTEM_NAME} MATCHES iOS)
    list(APPEND ARG_LINK_FLAGS "-Wl,-undefined,dynamic_lookup")
  elseif(${CMAKE_SYSTEM_NAME} MATCHES Linux)
    list(APPEND ARG_LINK_FLAGS "-Wl,-unresolved-symbols=ignore-all")
  elseif(${CMAKE_SYSTEM_NAME} MATCHES Windows)
    list(APPEND ARG_LINK_FLAGS "/wd4275")
  else()
    message(FATAL_ERROR "Trying to build module on a not supportd platform. Aborting.")
  endif()
  target_link_options(flb-${name} PRIVATE ${ARG_LINK_FLAGS})

  target_link_libraries(flb-${name} ${deps})
endmacro()

Note that depending on the target cmake version, there is also the possibility to pass the flags as the second parameter of FLB_PLUGIN, which passes it to target_link_libraries, as documented here: https://cmake.org/cmake/help/latest/command/target_link_libraries.html

What are your opinions on this ? Thanks in advance.

:raised_hands: Thanks to Mauro for suggesting the fix

schlesg commented 5 months ago

on Windows it should be " list(APPEND ARG_LINK_FLAGS "/FORCE:UNRESOLVED")" instead of "/wd4275" flag.