Statically Link 3rd Party Library Built with CMake into Qt 5.12.11 Project Built with QMake in Qt Creator
-
Hi I am trying to statically link the cutelyst simplemail library into my Qt project. I've been scouring the web or several hours now without a definitive soution. Cutelyst simplemail is built with CMake while my project is built with QMake and Qt 5.12.11 in Qt Creator. I have gotten the dynamic linking to work but am having trouble with static linking. Here are my questions:
- How do I build the cutelyst simpemail project statically using CMake?
- After building it statically how do I add it to my Qt project? Is it as simple as a LIBS+= or right-clicking on the project in Qt Creator and going to add library and following the wizard?
- Do I need to rebuild Qt static and integrate the cutelyst simplemail library into it?
- Are there any other approaches or information I've missed?
A minor detail that might complicate things is that cutelyst simplemail depends on the Qt5Core and Qt5Network libraries.
Of course this assumes all of this is possible... Thanks!
-
@SGaist I can be a little flaky sometimes... was using a different computer to compile the library than that which I was compiling my program. DUH! I'll post an answer with everything I did later. Thanks SGaist!
Here is what I had to do to compile the SimpleMail library statically:
- Load SimpleMail into Qt Creator by opening CMakeLists.txt from the SimpleMail directory
- In Qt Creator set the BUILD_SHARED_LIBS option to OFF in Project settings for SimpleMail (see post 6)
- Remove SHARED from the end of the add_library(SimpleMail2Qt${QT_VERSION_MAJOR} command in CMakeLists.txt in the src subdir (it will then default to SHARED or STATIC based on the BUILD_SHARED_LIBS setting)
- Add the following commands to the end of CMakeLists.txt in the src subdir (at the end so they override any previous compiler flags set)
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
These tell CMake to use the static version of the MSVC run-time library. - Turn off demo compilation with option(BUILD_DEMOS "Build the demos" OFF)
- SimpleMail was not configured to support static build by default so I had to change some code in smtpexports.h to change functions from being linked for DLL usage to static usage:
#define SMTP_EXPORT
/#if defined(SimpleMail2Qt5_EXPORTS)
#define SMTP_EXPORT Q_DECL_EXPORT
#else
#define SMTP_EXPORT Q_DECL_IMPORT
#endif/
For any library that is not built for static builds by default like SimpleMail you have to do the same. Q_DECL_EXPORT and Q_DECL_IMPORT are Qt macros for __declspec(dllexport) and __declspec(dllimport) respectively so if your library uses those you'll have to do the same for those guys in the library. By doing a #define for the macro without a value it will be empty and therefore avoid adding those __declspec guys to the library. - Make sure to build the library using a static build of Qt.
-
Hi I am trying to statically link the cutelyst simplemail library into my Qt project. I've been scouring the web or several hours now without a definitive soution. Cutelyst simplemail is built with CMake while my project is built with QMake and Qt 5.12.11 in Qt Creator. I have gotten the dynamic linking to work but am having trouble with static linking. Here are my questions:
- How do I build the cutelyst simpemail project statically using CMake?
- After building it statically how do I add it to my Qt project? Is it as simple as a LIBS+= or right-clicking on the project in Qt Creator and going to add library and following the wizard?
- Do I need to rebuild Qt static and integrate the cutelyst simplemail library into it?
- Are there any other approaches or information I've missed?
A minor detail that might complicate things is that cutelyst simplemail depends on the Qt5Core and Qt5Network libraries.
Of course this assumes all of this is possible... Thanks!
Hi,
- Check this part of the CMake documentation. Short version: set the BUILD_SHARED_LIBS option to false.
- Yes
- No
- I don't think so
-
Hi,
- Check this part of the CMake documentation. Short version: set the BUILD_SHARED_LIBS option to false.
- Yes
- No
- I don't think so
@SGaist Thanks I've tried compiling in Qt Creator with BUILD_SHARED_LIBS set to off in CMakeLists.txt, in Qt Creator with BUILD_SHARED_LIBS set to off via project settings, and also compiling via the command line using CMake from MSVS then building the project in MSVS.
I get the same results no matter what, my program asks for SimpleMail2Qt5.dll, Qt5Core, and Qt5Network.dll. A static build of cuetlyst simplemail should negate the requirement for bundling those dlls right? Am I missing/overlooking something simple here?
-
@SGaist Thanks I've tried compiling in Qt Creator with BUILD_SHARED_LIBS set to off in CMakeLists.txt, in Qt Creator with BUILD_SHARED_LIBS set to off via project settings, and also compiling via the command line using CMake from MSVS then building the project in MSVS.
I get the same results no matter what, my program asks for SimpleMail2Qt5.dll, Qt5Core, and Qt5Network.dll. A static build of cuetlyst simplemail should negate the requirement for bundling those dlls right? Am I missing/overlooking something simple here?
@Crag_Hack check whether you have the dynamic and static libraries in the same folder, if so, move the shared one somewhere else otherwise the linker will prefer them depending on how the arguments were feed.
As for QtCore and QtNetwork, that's normal. Unless you use only static libraries, you will have such dynamic dependencies.
The only way to have "no dependencies at all" is to build absolutely everything statically, including setting the VS runtime to static, which means that you will have to rebuild everything when they release a new runtime (think of security updates). This also implies additional requirements with regard to the license your dependencies are using (for example LGPL for Qt). That said, since you are already using a dynamic version of Qt, I don't really see the benefit of using a static version of SimpleMail2 beside removing one file to deploy.
-
@Crag_Hack check whether you have the dynamic and static libraries in the same folder, if so, move the shared one somewhere else otherwise the linker will prefer them depending on how the arguments were feed.
As for QtCore and QtNetwork, that's normal. Unless you use only static libraries, you will have such dynamic dependencies.
The only way to have "no dependencies at all" is to build absolutely everything statically, including setting the VS runtime to static, which means that you will have to rebuild everything when they release a new runtime (think of security updates). This also implies additional requirements with regard to the license your dependencies are using (for example LGPL for Qt). That said, since you are already using a dynamic version of Qt, I don't really see the benefit of using a static version of SimpleMail2 beside removing one file to deploy.
@SGaist I had the dll and the lib for Simple Mail in the same directory so I renamed the dll and still have the same DLL dependency after rebuilding the program. lib files are always static right? I could try rebuilding the libraries but I'm pretty sure I did it right the first time.
Also re
including setting the VS runtime to static
you just mean build Qt static right?
-
@SGaist I had the dll and the lib for Simple Mail in the same directory so I renamed the dll and still have the same DLL dependency after rebuilding the program. lib files are always static right? I could try rebuilding the libraries but I'm pretty sure I did it right the first time.
Also re
including setting the VS runtime to static
you just mean build Qt static right?
-
@SGaist I tried rebuilding the simple mail libraries with no luck. Here are a couple screenshots of the shared lib settings:
@Crag_Hack And here's the library build directory
-
@Crag_Hack And here's the library build directory
A
.lib
file can either be an import or a static library. Seeing the size of yours, it's clearly the former. -
A
.lib
file can either be an import or a static library. Seeing the size of yours, it's clearly the former. -
@SGaist Thanks, any way to force build of a static library instead of import library? couldn't find anything on google...
Clean the content of the build folder and rebuild the project ensuring that BUILD_SHARED_LIBS is set to off.
-
Clean the content of the build folder and rebuild the project ensuring that BUILD_SHARED_LIBS is set to off.
@SGaist I think I found the problem after some head smashing... in the CMakeLists.txt for the Simple Mail library it has:
add_library(SimpleMail2Qt${QT_VERSION_MAJOR} SHARED ${simplemailqt_SRC} ${simplemailqt_HEADERS} ${simplemailqt_HEADERS_PRIVATE} )
The ${simplemailqt_***} variables are just lists of source files.
I changed to STATIC instead of shared but then get a bunch of inconsistent dll linkage warnings and three definition of dllimport static data member not allowed errors (see screenshot below).
Does that mean the developer designed the library to be shared only? Would it take a herculean effort to convert it to static? Thanks again!
-
@SGaist I think I found the problem after some head smashing... in the CMakeLists.txt for the Simple Mail library it has:
add_library(SimpleMail2Qt${QT_VERSION_MAJOR} SHARED ${simplemailqt_SRC} ${simplemailqt_HEADERS} ${simplemailqt_HEADERS_PRIVATE} )
The ${simplemailqt_***} variables are just lists of source files.
I changed to STATIC instead of shared but then get a bunch of inconsistent dll linkage warnings and three definition of dllimport static data member not allowed errors (see screenshot below).
Does that mean the developer designed the library to be shared only? Would it take a herculean effort to convert it to static? Thanks again!
-
Sorry I lost track of this thread. Can you post the exact link of the sources of that library (just to be sure we are looking at the same thing).
-
Sorry I lost track of this thread. Can you post the exact link of the sources of that library (just to be sure we are looking at the same thing).
-
I found the issue, there's a glitch in the src
CMakeLists.txt
, they have hard coded the library build as shared. Just removed that from theadd_library
line, configure the build to be static and you will have a static build of it. -
I found the issue, there's a glitch in the src
CMakeLists.txt
, they have hard coded the library build as shared. Just removed that from theadd_library
line, configure the build to be static and you will have a static build of it.@SGaist Thanks, SGaist, I found the same issue, see three posts ago. I just now tried leaving both STATIC and SHARED out of add_library and get the same compile errors/warnings as using STATIC with add_library.
I have the same question as before... Does this mean the developer designed the library to be shared only? Would it take a herculean effort to convert it to static? Thanks again!
-
@SGaist Thanks, SGaist, I found the same issue, see three posts ago. I just now tried leaving both STATIC and SHARED out of add_library and get the same compile errors/warnings as using STATIC with add_library.
I have the same question as before... Does this mean the developer designed the library to be shared only? Would it take a herculean effort to convert it to static? Thanks again!
Do you mean you can't get a static build of the library ?
-
@SGaist These are the errors/warnings I get when I try to build the library without the SHARED option passed to add_library:
With
BUILD_SHARED_LIBS
set to OFF ?