Cmake: linking to static internal library without exporting it

Viewed 3730

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.

2 Answers
Related