Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Statically Link 3rd Party Library Built with CMake into Qt 5.12.11 Project Built with QMake in Qt Creator
Forum Updated to NodeBB v4.3 + New Features

Statically Link 3rd Party Library Built with CMake into Qt 5.12.11 Project Built with QMake in Qt Creator

Scheduled Pinned Locked Moved Solved General and Desktop
37 Posts 2 Posters 7.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • SGaistS SGaist

    @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.

    C Offline
    C Offline
    Crag_Hack
    wrote on last edited by Crag_Hack
    #5

    @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?

    C 1 Reply Last reply
    0
    • C Crag_Hack

      @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?

      C Offline
      C Offline
      Crag_Hack
      wrote on last edited by Crag_Hack
      #6

      @SGaist I tried rebuilding the simple mail libraries with no luck. Here are a couple screenshots of the shared lib settings:

      3.png 5.png

      C 1 Reply Last reply
      0
      • C Crag_Hack

        @SGaist I tried rebuilding the simple mail libraries with no luck. Here are a couple screenshots of the shared lib settings:

        3.png 5.png

        C Offline
        C Offline
        Crag_Hack
        wrote on last edited by Crag_Hack
        #7

        @Crag_Hack And here's the library build directory

        4.png

        SGaistS 1 Reply Last reply
        0
        • C Crag_Hack

          @Crag_Hack And here's the library build directory

          4.png

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #8

          A .lib file can either be an import or a static library. Seeing the size of yours, it's clearly the former.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          C 1 Reply Last reply
          0
          • SGaistS SGaist

            A .lib file can either be an import or a static library. Seeing the size of yours, it's clearly the former.

            C Offline
            C Offline
            Crag_Hack
            wrote on last edited by Crag_Hack
            #9

            @SGaist Thanks, any way to force build of a static library instead of import library? couldn't find anything on google...

            SGaistS 1 Reply Last reply
            0
            • C Crag_Hack

              @SGaist Thanks, any way to force build of a static library instead of import library? couldn't find anything on google...

              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #10

              Clean the content of the build folder and rebuild the project ensuring that BUILD_SHARED_LIBS is set to off.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              C 1 Reply Last reply
              0
              • SGaistS SGaist

                Clean the content of the build folder and rebuild the project ensuring that BUILD_SHARED_LIBS is set to off.

                C Offline
                C Offline
                Crag_Hack
                wrote on last edited by Crag_Hack
                #11

                @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!

                5.png

                C 1 Reply Last reply
                0
                • C Crag_Hack

                  @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!

                  5.png

                  C Offline
                  C Offline
                  Crag_Hack
                  wrote on last edited by
                  #12

                  @SGaist Got any input? A little might go a long way, thanks!

                  SGaistS 1 Reply Last reply
                  0
                  • C Crag_Hack

                    @SGaist Got any input? A little might go a long way, thanks!

                    SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #13

                    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).

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    C 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      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).

                      C Offline
                      C Offline
                      Crag_Hack
                      wrote on last edited by
                      #14

                      @SGaist
                      Here 'tis. I'm looking at the CMakeLists.txt in the src directory which is the location of the library code.

                      SGaistS 1 Reply Last reply
                      0
                      • C Crag_Hack

                        @SGaist
                        Here 'tis. I'm looking at the CMakeLists.txt in the src directory which is the location of the library code.

                        SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        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 the add_library line, configure the build to be static and you will have a static build of it.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        C 1 Reply Last reply
                        1
                        • SGaistS SGaist

                          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 the add_library line, configure the build to be static and you will have a static build of it.

                          C Offline
                          C Offline
                          Crag_Hack
                          wrote on last edited by
                          #16

                          @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!

                          SGaistS 1 Reply Last reply
                          0
                          • C Crag_Hack

                            @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!

                            SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #17

                            Do you mean you can't get a static build of the library ?

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            C 1 Reply Last reply
                            0
                            • SGaistS SGaist

                              Do you mean you can't get a static build of the library ?

                              C Offline
                              C Offline
                              Crag_Hack
                              wrote on last edited by
                              #18

                              @SGaist These are the errors/warnings I get when I try to build the library without the SHARED option passed to add_library:

                              alt text

                              SGaistS 1 Reply Last reply
                              0
                              • C Crag_Hack

                                @SGaist These are the errors/warnings I get when I try to build the library without the SHARED option passed to add_library:

                                alt text

                                SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #19

                                With BUILD_SHARED_LIBS set to OFF ?

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                C 1 Reply Last reply
                                0
                                • SGaistS SGaist

                                  With BUILD_SHARED_LIBS set to OFF ?

                                  C Offline
                                  C Offline
                                  Crag_Hack
                                  wrote on last edited by
                                  #20

                                  @SGaist Yep that's correct.

                                  C 1 Reply Last reply
                                  0
                                  • C Crag_Hack

                                    @SGaist Yep that's correct.

                                    C Offline
                                    C Offline
                                    Crag_Hack
                                    wrote on last edited by
                                    #21

                                    I tried add_compile_definitions(PLUGIN_LIBRARY) to both CMakeLists.txt with no luck.

                                    C 1 Reply Last reply
                                    0
                                    • C Crag_Hack

                                      I tried add_compile_definitions(PLUGIN_LIBRARY) to both CMakeLists.txt with no luck.

                                      C Offline
                                      C Offline
                                      Crag_Hack
                                      wrote on last edited by
                                      #22

                                      @SGaist I might have just gotten it to work... will confirm tomorrow but it looks pretty convincing. I added add_compile_definitions(SimpleMail2Qt5_EXPORTS) to both CMakeLists.txt's. Forgot how I arrived at this necessity I'll try to remember tomorrrow :) Thanks for the help!

                                      C 1 Reply Last reply
                                      0
                                      • C Crag_Hack

                                        @SGaist I might have just gotten it to work... will confirm tomorrow but it looks pretty convincing. I added add_compile_definitions(SimpleMail2Qt5_EXPORTS) to both CMakeLists.txt's. Forgot how I arrived at this necessity I'll try to remember tomorrrow :) Thanks for the help!

                                        C Offline
                                        C Offline
                                        Crag_Hack
                                        wrote on last edited by Crag_Hack
                                        #23

                                        @SGaist I finally got the library to compile statically. I had to remove SHARED from add_library, set BUILD_SHARED_LIBS to off, and add add_compile_definitions(SimpleMail2Qt5_EXPORTS) to CMakeLists.txt.

                                        In the SimpleMail project we have src/smtpexports.h with:

                                        #if defined(SimpleMail2Qt5_EXPORTS) || defined(SimpleMail2Qt6_EXPORTS)
                                        #define SMTP_EXPORT Q_DECL_EXPORT
                                        #else
                                        #define SMTP_EXPORT Q_DECL_IMPORT
                                        #endif
                                        

                                        The Q_DECL_EXPORT seems to be essential.

                                        Now I get another error! Bloody hell.

                                        Running NMake trying to build my project that uses SimpleMail I get some of these:

                                        SimpleMail2Qt5.lib(emailaddress.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in qtsingleapplication.obj
                                        

                                        and a lot of these:

                                        SimpleMail2Qt5.lib(mimemessage.cpp.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QByteArray::QByteArray(class QByteArray const &)" (__imp_??0QByteArray@@QEAA@AEBV0@@Z) referenced in function "public: __cdecl QStringBuilder<class QByteArray,class QByteArray>::operator class QByteArray(void)const " (??B?$QStringBuilder@VQByteArray@@V1@@@QEBA?AVQByteArray@@XZ)
                                        

                                        and these:

                                        SimpleMail2Qt5.lib(mimepart.cpp.obj) : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __cdecl QByteArray::QByteArray(class QByteArray const &)" (__imp_??0QByteArray@@QEAA@AEBV0@@Z)
                                        

                                        I'll google these guys... do you know off the top of your head what's going on?
                                        Thanks

                                        C 1 Reply Last reply
                                        0
                                        • C Crag_Hack

                                          @SGaist I finally got the library to compile statically. I had to remove SHARED from add_library, set BUILD_SHARED_LIBS to off, and add add_compile_definitions(SimpleMail2Qt5_EXPORTS) to CMakeLists.txt.

                                          In the SimpleMail project we have src/smtpexports.h with:

                                          #if defined(SimpleMail2Qt5_EXPORTS) || defined(SimpleMail2Qt6_EXPORTS)
                                          #define SMTP_EXPORT Q_DECL_EXPORT
                                          #else
                                          #define SMTP_EXPORT Q_DECL_IMPORT
                                          #endif
                                          

                                          The Q_DECL_EXPORT seems to be essential.

                                          Now I get another error! Bloody hell.

                                          Running NMake trying to build my project that uses SimpleMail I get some of these:

                                          SimpleMail2Qt5.lib(emailaddress.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in qtsingleapplication.obj
                                          

                                          and a lot of these:

                                          SimpleMail2Qt5.lib(mimemessage.cpp.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QByteArray::QByteArray(class QByteArray const &)" (__imp_??0QByteArray@@QEAA@AEBV0@@Z) referenced in function "public: __cdecl QStringBuilder<class QByteArray,class QByteArray>::operator class QByteArray(void)const " (??B?$QStringBuilder@VQByteArray@@V1@@@QEBA?AVQByteArray@@XZ)
                                          

                                          and these:

                                          SimpleMail2Qt5.lib(mimepart.cpp.obj) : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __cdecl QByteArray::QByteArray(class QByteArray const &)" (__imp_??0QByteArray@@QEAA@AEBV0@@Z)
                                          

                                          I'll google these guys... do you know off the top of your head what's going on?
                                          Thanks

                                          C Offline
                                          C Offline
                                          Crag_Hack
                                          wrote on last edited by Crag_Hack
                                          #24

                                          @SGaist ( or anybody ) Making progress on this, it looks like error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' is due to symbol mismatch detected by the linker. So one part of the program uses the DLL version of the run time library and another uses static version of the run time library. See here and here.

                                          For those who aren't following this thread I am getting this error (and many others probably related to this) when trying to compile my project using a static build of the SimpleMail plugin:

                                          SimpleMail2Qt5.lib(emailaddress.cpp.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in qtsingleapplication.obj
                                          

                                          The fix should be easy, just make the library use the static run time library. I tried to do this in CMakeLists.txt using both:

                                          set_property(TARGET SimpleMail2Qt${QT_VERSION_MAJOR} PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
                                          //used at the end of the CMakeLists.txt in src directory of SimpleMail project directory
                                          

                                          and

                                          set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
                                          //used at the beginning of both CMakeLists.txt files (the one in the SimpleMail root and src subdirectory of SimpleMail project)
                                          

                                          but still get the linker error. See here and here and for these CMake variables. Any idea what might be wrong? Is there another way to specify whether to use the static or dll run time library when building a project using CMake and Qt Creator?

                                          C 1 Reply Last reply
                                          0

                                          • Login

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved