Linking static Protobuf libraries with VS2012 and CMake

3,370 views
Skip to first unread message

Klaim J. Lamotte

unread,
Oct 20, 2012, 10:25:34 AM10/20/12
to prot...@googlegroups.com
I've been setting up a project which have several libraries which contain protobuf generated source code.
I was using the last stable Protobuf version (from sources) but tried few hours ago with the last trunk version, with the same exact result (but with compilation errors fixed, thumbs up guys)

Basically, I have this configuration:

libprotobuf <- myprotolib <- myexecutable

Here <- means "link with". More details:
 - libprotobuf is in static linking, as default when using visual studio projects (I tried dynamic too but there is too much problems, as evoked in the readme);
 - myprotolib is in static linking, contain code generated with protoc
 - myexecutable is an executable

I'm using Visual Studio 2012 and CMake 2.8.9 to generate myprotolib and myexecutable.
The CMake script will execute a .bat file if on Windows, that will do exactly this:

copy protobuf\vsprojects\protobuf.sln protobuf\vsprojects\protobuf_vc11.sln
call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat"
devenv protobuf/vsprojects/protobuf_vc11.sln /upgrade
devenv protobuf/vsprojects/protobuf_vc11.sln /project libprotobuf /build Debug
devenv protobuf/vsprojects/protobuf_vc11.sln /project protoc /build Debug
devenv protobuf/vsprojects/protobuf_vc11.sln /project libprotobuf /build Release
devenv protobuf/vsprojects/protobuf_vc11.sln /project protoc /build Release

This script will just convert the visual studio project files to VS 2012 project files, without overwriting the solution file (allowing me to just delete my 
protobuf copy and copy paste the last version in the directory and execute this script and it's updated).

So far, I didn't have any problem because I was working only inside myprotolib, to add some coding facilities related to the message types.
However, at the moment I linked myexecutable to myprotolib, I got this kind of errors:

3>libprotobuf.lib(common.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(common.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(message_lite.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(message_lite.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(descriptor.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(descriptor.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(message.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(message.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(unknown_field_set.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(unknown_field_set.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(generated_message_reflection.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(generated_message_reflection.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(once.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(once.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(coded_stream.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(coded_stream.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(wire_format_lite.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(wire_format_lite.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(reflection_ops.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(reflection_ops.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(wire_format.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(wire_format.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(generated_message_util.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(generated_message_util.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(repeated_field.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(repeated_field.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(zero_copy_stream_impl_lite.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(zero_copy_stream_impl_lite.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(descriptor_database.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(descriptor_database.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(descriptor.pb.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(descriptor.pb.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(dynamic_message.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(dynamic_message.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(text_format.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(text_format.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(tokenizer.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(tokenizer.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(strutil.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(strutil.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(substitute.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(substitute.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(zero_copy_stream_impl.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(zero_copy_stream_impl.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(extension_set_heavy.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(extension_set_heavy.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(extension_set.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(extension_set.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(atomicops_internals_x86_msvc.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(atomicops_internals_x86_msvc.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(structurally_valid.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(structurally_valid.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(zero_copy_stream.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(zero_copy_stream.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>libprotobuf.lib(stringprintf.obj) : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in main.obj
3>libprotobuf.lib(stringprintf.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in main.obj
3>LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library

All of these errors are apparently because of mismatching configurations between libprotobuf and the two other projects.
They are generated only when compiling the exectuable, and only in Debug mode. 
Online searches indicates that some part of the libprotobuf configuration is set in a way that makes the linker detect it to be compiled in Release mode instead of Debug.

The source of the problem is in the project file configuration BUT.... 
I checked the libprotobuf project properties and I see nothing wrong (I'm no expert though).
In particular, I checked the compilation flags used and, as suggested in the link, I was looking for /MDd , in Debug mode. It is set correctly apparently.

So I'm having a hard time understanding what other libprotobuf project file properties are causing this problem. 

Any idea?

Joel Lamotte

Klaim J. Lamotte

unread,
Oct 20, 2012, 10:38:14 AM10/20/12
to prot...@googlegroups.com
Ok I just found that my executable is using this lib file:

protobuf\vsprojects\Release\libprotobuf.lib

It's the release build. I don't know why it does that but I supposes it's related to CMake stuffs.
I'm not sure though...

Still searching.

Joel Lamotte

Klaim J. Lamotte

unread,
Oct 20, 2012, 11:29:04 AM10/20/12
to prot...@googlegroups.com
Nevermind, the problem was related to CMake scripts.
I fixed the problem.

Joel Lamotte

Klaim - Joël Lamotte

unread,
Mar 3, 2013, 7:19:10 AM3/3/13
to Nicholas Boblob, prot...@googlegroups.com



On Fri, Mar 1, 2013 at 11:17 PM, Nicholas Boblob <vvnic...@gmail.com> wrote:
WHAT WAS THE PROBLEM


As I explained, my CMake script was badly configured and it made it link with the Release .lib of protobuf in Debug, which is why I had the initial problem.
I just fixed my CMake script (which was indeed looking for the wrong file) and it worked.

Joel Lamotte

Reply all
Reply to author
Forward
0 new messages