0

In CMake there are imported modules that are used to simply add external modules to local targets. For example if we want to use boost::filesystem library in our project we could have a CMakeLists.txt like this:

project(foo CXX)

find_packge(Boost REQUIRED COMPONENTS filesystem)

add_executable(foo main.cpp)
target_link_libraries(foo Boost::filesystem)

With above configuration CMake will add proper compiler options and include directories among required libraries to building process of the foo.

Now we have to build a library instead of an executable and we don't want to link boost::filesystem libraries to our library. We want only compiler options and include directories to be added to our target. Could we use imported modules concepts here? I mean that if we could use Boost::filesystem syntax for adding those options to our target?

project(foo CXX)

find_packge(Boost REQUIRED COMPONENTS filesystem)

add_library(foo STATIC foo.cpp)
# what should be wrote here to only add headers and configs to foo not the libs?
E. Vakili
  • 1,036
  • 1
  • 10
  • 25
  • `target_include_directories(foo PRIVATE ${Boost_INCLUDE_DIRS})`? –  May 24 '18 at 16:36
  • I'm pretty sure that `add_library(STATIC)` won't link the Boost library into itself (see e.g. [here](https://cmake.org/Bug/view.php?id=9732)). – Florian May 25 '18 at 20:44
  • @AmitSingh I know that but I like to use syntax like Boost::filesystem – E. Vakili May 26 '18 at 13:33
  • @Florian the bug you referenced is about VC++ I think. Isn't that? – E. Vakili May 26 '18 at 13:35
  • @E.Vakili Yes, but it describes the details why you won't want to link depending libraries into a static library. In short, if two static libraries would include e.g. `Boost::filesystem` and then you link both of those libraries into an executable you would get `duplicate symbol` errors. So CMake by default, does not add linker options like [`--whole-archive` for `gcc`](https://stackoverflow.com/questions/805555/ld-linker-question-the-whole-archive-option) or `LinkLibraryDependencies` for `VC`. So `target_link_libraries(foo Boost::filesystem)` should work, it just describes the dependency. – Florian May 26 '18 at 14:21
  • @Florian Thanks. I got the point. Would you add and answer? – E. Vakili May 28 '18 at 04:54
  • @E.Vakili You're welcome. Added an answer. – Florian May 28 '18 at 10:26

1 Answers1

1

Turning my comments into an answer

add_library(STATIC) won't link the target_link_libraries() dependencies into itself.

In short, if two static libraries would include e.g. Boost::filesystem and then you link both of those libraries into an executable (where the external symbols get actually resolved) you would get duplicate symbol errors.

So CMake by default, does not add linker options like --whole-archive for gcc or LinkLibraryDependencies for VC.

target_link_libraries(foo Boost::filesystem) should work, it just describes the dependency resolved later when building a executable or shared library.

References

Florian
  • 39,996
  • 9
  • 133
  • 149