Discussion:
[CMake] link static lib to dll
Micha Renner
2010-02-09 10:05:40 UTC
Permalink
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
Michael Wild
2010-02-09 10:34:47 UTC
Permalink
Post by Micha Renner
A DLL-library is linked to static library.
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
Micha Renner
2010-02-10 06:12:35 UTC
Permalink
Post by Michael Wild
Post by Micha Renner
A DLL-library is linked to static library.
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...
This is true. Now, I understand the default behavior of CMake (Not bad,
CMake of course).
Post by Michael Wild
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...
Hopeless, I would have never found this solution.
Thank you very much.
Micha

Loading...