Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does CMake EXPORT require static libraries used to build a shared library?

I'm attempting to make it easier for other projects to link against the shared libraries we distribute with our project. When I try to take advantage of the EXPORT mechanism, CMake complains that I'm not including the static libraries used to build the shared libraries in the export set. This seems unnecessary to me as the other projects only need to link against the shared library and I don't really want to install the static libraries. This seems to be very similar to this bug, but I might just be misunderstanding how this all works. Here is a minimal example:

CMAKE_MINIMUM_REQUIRED(VERSION 3.2.1) 
PROJECT(ExportTest)

ADD_LIBRARY(myStaticLib STATIC staticLib.c)
ADD_LIBRARY(mySharedLib SHARED sharedLib.c)
TARGET_LINK_LIBRARIES(mySharedLib myStaticLib)

INSTALL(TARGETS mySharedLib EXPORT myExport DESTINATION lib)
INSTALL(EXPORT myExport DESTINATION include)

Which results in the following error message:

CMake Error: install(EXPORT "myExport" ...) includes target "mySharedLib"
which requires target "myStaticLib" that is not in the export set.
like image 838
rpmcnally Avatar asked Oct 21 '16 19:10

rpmcnally


1 Answers

When using target_link_libraries like you did, library dependencies are transitive by default. Try:

target_link_libraries(mySharedLib PRIVATE myStaticLib)
like image 76
rgmt Avatar answered Sep 23 '22 15:09

rgmt