I am building a static lib in cmake, which is dependent on many other static libs. I would like them all to be included in the output .lib/.a file, so I can just ship a big lib file to customers. In VS2010 there is an option "Link Library Dependencies" which does exactly this.
But I cant find how to do it in cmake. Can you set this flag via cmake, or get the same result some other way? I have tried target_link_libraries(...) and also add_dependencies(...) but cmake seems to simply ignore this line for static libs
Okay, so I have a solution. First it's important to recognize that static libraries do not link other static libraries into the code. A combined library must be created, which on linux can be done with
ar
. See Linking static libraries to other static libraries for more info there.Consider two source files:
test1.c:
test2.c:
The
CMakeLists.txt
to create two libraries and then create a combined library looks like:project(test)
The options to the
ar
command are platform-dependent in this case, although theCMAKE_AR
variable is platform-independent. I will poke around to see if there is a more general way to do this, but this approach will work on systems that usear
.Edit:
Based on How to set the options for CMAKE_AR it looks like the better way to do this would be:
This should be platform-independent because this is the command structure used to create archives internally by CMake. Provided of course the only options you want to pass to your archive command are
rc
as these are hardwired into CMake for thear
command.I'd like to enhance the other solutions by providing my
CMakeLists.txt
file that actually works also in terms of building dependencies.Solution misusing CMake
Note that this solution works so far with Visual Studio but I guess it can be made multi-platform compliant. I can imagine that the following version might work for Unix-based platforms:
Note that these solutions somehow misuse CMake as it would complain about a target of type UTILITY (instead of STATIC or SHARED) if you place the
target_link_libraries
call after theadd_custom_target
declaration.CMake target-declaration-compliant solution
To make it CMake compliant, you can replace the `target_link_libraries' call by
In my case it is not entirely satisfactory because
mainexec
has to know aboutcombinedLib
although it expects all dependencies to be handled by thetarget_link_libraries
call.Alternative solution with less coupling
Looking a bit further towards imported targets I eventually found a solution that solves my last problem:
If you intend to modularize the whole add
GLOBAL
afterSTATIC IMPORTED
to make the imported target globally visible.Portable CMake solution
With the current CMake versions CMake provides full support for transitive dependencies and interface libraries. An interface library can then "link" against other libraries and this interface library can, in turn, be "linked" against. Why quotation marks? While this works good, this actually doesn't create a physical, combined library but rather creates a kind of an alias to the set of "sub-libs". Still this was the solution we eventually needed, which is why I wanted to add it here.
That's it!