Maths noob
Maths noob

Reputation: 1802

Cmake: linking to static internal library without exporting it

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.

Upvotes: 6

Views: 5340

Answers (2)

sakra
sakra

Reputation: 65931

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.

Upvotes: 4

gatis paeglis
gatis paeglis

Reputation: 611

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

Upvotes: 1

Related Questions