4

I have a project with this structure:

/path/to/my/project
├── CMakeLists.txt
├── internal-libs
│   ├── internal-lib1
├── libs
│   ├── lib1
│   ├── lib2

lib1 is a static library.

lib2 is a static library.

internal-lib1 is a static library.

lib2 links statically to lib2 and internal-lib1. lib1 and lib2 are going to be exported but internal-lib1 will be left behind. For the linkage, I have:

target_link_libraries(lib2 PRIVATE internal-lib1)
target_link_libraries(lib2 PRIVATE lib1)

My understanding is that because I am linking statically and privately, All the information about internal-lib1 would be contained in lib2 and that I won't have to export internal-lib1 to the outside world.

However, when I try to use it in a client program, I get the error:

/usr/bin/ld cannot find -llib-internal1
collect2: error: ld returned 1 exit status

in my generated Export config file, I have:

# Create imported target lib2
add_library(lib2 STATIC IMPORTED)
set_target_properties(lib2 PROPERTIES
INTERFACE_LINK_LIBRARIES "$<LINK_ONLY:lib1>;**$<LINK_ONLY:internal-lib1>**"
)
# Create imported target lib1
add_library(lib1 STATIC IMPORTED)

Am I misunderstanding the static linking or is there something wrong with my setup? I am using cmake 3.2.2. All my target includes are PRIVATE. I don't understand why INTERFACE_LINK_LIBRARIES is populated with entries and what LINK_ONLY means.

p.s. Actually lib1 and lib2 are supposed to be shared libraries but I can't even get the static version working so for simplicity here I am describing the static case for the exportable libraries.

Maths noob
  • 1,684
  • 20
  • 42

2 Answers2

4

For static libaries lib1 and lib2, the CMake command

target_link_libraries(lib2 PRIVATE lib1)

does not imply that the library lib1 is copied to the library lib2 upon linking. The PRIVATE keyword only affects transitive usage requirements of the library lib1 when another library links to lib2.

To have CMake merge lib1 into lib2 upon linking, use libtool and a POST_BUILD action:

add_custom_command(TARGET lib2 POST_BUILD
    COMMAND /usr/bin/libtool -static -o $<TARGET_FILE:lib2>
    $<TARGET_FILE:lib2> $<TARGET_FILE:lib1>

In that case, there is no need to link lib1 to lib2 with target_link_libraries.

sakra
  • 62,199
  • 16
  • 168
  • 151
  • I see thanks. I was basically trying to do what is attempted [here](http://stackoverflow.com/a/11448878/4376555) for the simple case of static libraries as I thought that his methods but unnecessarily complicated. However I think your solution is even more hacky. – Maths noob Mar 26 '16 at 14:24
  • "does not imply that the library lib1 is copied to the library lib2 upon linking." That's not true for shared libraries: lib1's .a definitely gets copied inside lib2's .so – Jean-Michaël Celerier Aug 09 '22 at 11:49
0

One option is to create object library instead of static library:

add_library(foo OBJECT foo.cpp)
add_executable(baz $<TARGET_OBJECTS:foo> baz.cpp)

You can find more docs about this feature here: https://cgold.readthedocs.io/en/latest/rejected/object-libraries.html

gatis paeglis
  • 541
  • 5
  • 7