[CMake] link static lib to dll

Michael Wild themiwi at gmail.com
Tue Feb 9 05:34:47 EST 2010


On 9. Feb, 2010, at 11:05 , Micha Renner wrote:

> A DLL-library is linked to static library. 
> The structure of the project is this:
> 
> main-project 
> 	+--------- Project STATIC
> 	|
> 	+--------- Project DLL
> 
> This works very well.
> But if I include the following export part to the project of the DLL...
> 
> INSTALL(TARGETS T-DLL EXPORT T-DLLExport 
> 		RUNTIME DESTINATION dll		
> 		LIBRARY DESTINATION lib
> 	 	ARCHIVE DESTINATION lib)
> INSTALL(EXPORT T-DLLExport DESTINATION lib/${T-DLL})
> 
> ... CMake generates the message: "CMake Error: INSTALL(EXPORT
> "TLIBExport" ...) includes target "T-DLL" which requires target
> "T-Static" that is not in the export set."
> 
> This is unexpected. I don't want export the static library because it is
> already linked to the DLL and it is used only by the DLL.
> 
> Micha

But CMake can't know that somebody linking against the DLL won't need the static library. This is because the static library might contain additional symbols which are not used directly by any of the code in the DLL, and thus are not present in there. That's one of the most surprising facts of static linking...

The solution is to tell CMake explicitly which of the libraries are "in the link interface" and thus must be also exported using the LINK_INTERFACE_LIBRARIES target property of your DLL. Set it to the list of libraries which appear in the "link interface" of your library (i.e. NOT the static library you're talking about). But be careful, if you miss any of the real dependencies, all hell might break loose at any time, even far in the future...

HTH

Michael



More information about the CMake mailing list