0

I have two libraries:

  • a;
  • b: a header-only library which depends on a.

I am not sure if it is possible to link b to a. If so, how can I do it?

In fact, I have a third library c that depends on both. This CMake script does not work:

[...]

add_library(a ${a_SRC})
target_link_libraries(a CONAN_PKG::<foo>)

add_library(b INTERFACE)
target_sources(b INTERFACE ${b_SRC})
target_include_directories(b INTERFACE "${PROJECT_SOURCE_DIR}/src/include/b/")
target_link_libraries(b INTERFACE a) # This has no effect, I think!

add_executable(c ${c_SRC})
target_link_libraries(c b a)

I used a workaround by adding the source files of a into the definition of c.


[...]
add_executable(c ${c_SRC} ${a_SRC})
[...]

  • 1
    You don't _link_ to header-only libraries. `I used a workaround by adding the source files of a into the definition of c.` Header-Only libraries shouldn't have source files, or if they do, they will be included by the headers. No need to compile them. – tkausl Sep 01 '20 at 14:01
  • 4
    It sounds a bit weird. A header-only library can really only have dependencies on other header-only libraries. Else it wouldn't be "header-only". – MSalters Sep 01 '20 at 14:03
  • Thanks, @MSalters! If I understand well. This means that either, `a` must be header-only, or that I need to add a dummy.cpp file to `b` for it to "compile" and be linked with `a`. – Oussama Ennafii Sep 01 '20 at 14:12
  • @ethiy: I don't see what that dummy file adds to the equation. I would use _real_ implementation files. Header-only files avoid linking complexity at the cost of compilation time, but b has the worst of both worlds. – MSalters Sep 01 '20 at 14:16

0 Answers0