Home > Software design >  cmake link two libraries
cmake link two libraries

Time:02-05

I'm trying to link two libraries. One depends on the other. They are both on the same level. I tried to follow the documentation for my version but when I compile it the include with <> doesn't work.

The dependency

include_directories(headers)

add_library(log SHARED log.c)
target_compile_definitions(log PUBLIC log)

The dependent

include_directories(headers)

add_library(window OBJECT dialog.c ncurses_add.c window.c)
target_compile_definitions(window PUBLIC OBJ)
target_link_libraries(window log)

Could anyone help me understand why is that ? Thanks

CodePudding user response:

Add target_include_directories(log INTERFACE headers) or target_include_directories(log PUBLIC headers) to the log CMakeLists.txt.

From the target_include_directories manual:

PUBLIC and INTERFACE items will populate the INTERFACE_INCLUDE_DIRECTORIES property of <target>

From the INTERFACE_INCLUDE_DIRECTORIES manual:

List of public include directories requirements for a library. Targets may populate this property to publish the include directories required to compile against the headers for the target

What that means is that any dependent targets will get the include paths of the dependency added to their build commands.

  •  Tags:  
  • Related