Results 1 to 5 of 5

Thread: Linking protocol buffers library statically

  1. #1
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Linking protocol buffers library statically

    Hi,
    I am trying to statically link Google Protocol Buffers to my application. I tried every combination i could; but unfortunatelly i couldnt manage to compile...

    I am using MSVC2015 compiler with Qt 5.7.1 on Qt Creator.

    I compiled the protocol buffers library with the same compiler in statically release mode.

    And added it to .pro file:

    Qt Code:
    1. LIBS += -L$$PWD/libs/ -llibprotobuf
    To copy to clipboard, switch view to plain text mode 

    But i am getting hundres of error messages like that:

    Qt Code:
    1. libprotobuf.lib(common.cc.obj):-1: error: LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' in drivermessage.pb.obj
    To copy to clipboard, switch view to plain text mode 

    It complains for a mismatch. But i think, i am adding the static library as it should be. Am i missing something ? Any idea?
    Here is a test project containing static probuf libraries:

    https://dl.dropboxusercontent.com/u/.../Messaging.rar

    Thanks in advance..

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Linking protocol buffers library statically

    Don't confuse compiling with linking. Your code compiles just fine, it is the linking that is failing. It looks like when you built the library, you used a flag that said it should be linked statically to the Windows runtime library (MT), but the rest of your code is linking dynamically via DLLs (MD). See this StackOverflow article or this one for the explanation and solution.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Linking protocol buffers library statically

    Thanks for reply,

    Yes you are right. Actually i mean, the binary is incomplete since I am trying to statically link the library. I followed google's instruction for compiling on their website here . If I had used -Dprotobuf_BUILD_SHARED_LIBS=ON flag for CMake, it would have compiled the shared libraries. But I didn't. The default behaviour is static library.

    And another clue for the type of library is its size. It has a quite big size, 31 MBs. Its shared lib version is about 3 MBs. So i think there is not a problem with the compiled static library.

  4. #4
    Join Date
    Dec 2007
    Location
    London
    Posts
    206
    Thanks
    40
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android

    Default Re: Linking protocol buffers library statically

    I just realised something, when i change the compile mode to debug, the warning changes to:

    Qt Code:
    1. libprotobuf.lib(generated_message_util.obj):-1: error: LNK2038: mismatch detected for 'RuntimeLibrary': value 'MT_StaticRelease' doesn't match value 'MDd_DynamicDebug' in drivermessage.pb.obj
    To copy to clipboard, switch view to plain text mode 

    "MD_DynamicRelease" changed to "MDd_DynamicDebug".

    I think it says: "Ok the library is static but your build is "MDd_DynamicDebug" or "MD_DynamicRelease"

    The question is "why is qmake configuring my project as :"MD_Dynamic" "

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Linking protocol buffers library statically

    I think it says: "Ok the library is static but your build is "MDd_DynamicDebug" or "MD_DynamicRelease"
    Partly right - changing your project build to "Debug" changes the runtime library linkage to Debug also.

    The problem is that CMake has been told to build your library to link to the C++ "Static Release" runtime library. This has nothing to do with your library itself - your library was built as a static release library. The problem is that the library has been told that to resolve the runtime library symbols it uses, it needs to look for the C++ static release library. The rest of your project has been told to look for the C++ dynamic release (or debug) library. You can't have both at the same time.

    So you probably need to run CMake again to generate your library's build tree and make sure the options including linking against the dynamic release runtime. You can do this by adding "/MD" to the CMAKE_CXX_FLAGS variable (and removing "/MT" if you see it). You will probably find this variable somewhere in the top-level CMakelists.txt file for the library. You will need to delete the old build tree to make sure the Makefiles / vcxproj files are regenerated (depending on which CMake generator you use).

    The question is "why is qmake configuring my project as :"MD_Dynamic" "
    Because that is the default, and that is the one you want, because Qt is built to link that way. You would have to rebuild all of Qt if you want to change it. Better to change your library instead.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. The following user says thank you to d_stranz for this useful post:

    yagabey (3rd January 2017)

Similar Threads

  1. Replies: 5
    Last Post: 4th October 2016, 17:04
  2. Getting Device Interface Protocol (SCSI or ATA protocol)
    By Ryuuji in forum General Programming
    Replies: 1
    Last Post: 18th July 2013, 08:37
  3. Replies: 3
    Last Post: 28th June 2011, 18:32
  4. Statically linking QT libs on symbian
    By bullwinkle in forum Newbie
    Replies: 1
    Last Post: 6th February 2010, 10:25
  5. Statically Linking... everything
    By bpetty in forum Newbie
    Replies: 5
    Last Post: 26th October 2006, 17:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.