Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use headers from library but do not link to it with CMake?

Tags:

c++

linker

cmake

I have an executable and a dynamic library that both depend on a static libary. If more context helps, the executable embeds a Python interperter and the dynamic library is a Python extension module. The dynamic library should not link to the static library because the later is already linked by the executable. However, the dynamic library requires headers from the static library to compile, which is only supplied implicitly via target_link_libraries. In effect, What I need is a target_link_libraries that adds include paths and does nothing else.

Here is what I've tried:

  • Retrieve include directories from target property of the static library, but indirectly included headers are still missing.

  • Have the executable and the dynamic library both link to the static library. That can't be expected to work, and it doesn't.

  • Turn the static library into a dynamic library. That would work, but I would like to avoid touching build scripts for the static library and the executable as much as possible.

  • Make the dynamic library static and links it with executable. This is impossible because Python extensions must be dynamically loaded.

like image 578
Huazuo Gao Avatar asked Oct 28 '25 08:10

Huazuo Gao


1 Answers

Usually find_package(Foo) defines packages' include directories in a variable FOO_INCLUDE_DIR.

You can simply add that to your project's include path with

include_directories("${FOO_INCLUDE_DIR}")
like image 141
rustyx Avatar answered Oct 31 '25 00:10

rustyx