4

I downloaded luajit source from

http://luajit.org/git/luajit-2.0.git

and built it with its msvcbuild.bat

https://github.com/luvit/luajit-2.0/blob/master/src/msvcbuild.bat

Judging from the batch file, it uses /MD to build the lua51.lib. When I linked the library to my application, I found visual studio 2013 does not issue runtime library mismatch when I used /MDd settings for my application.

I also have built other source into libraries using visual studio, and I have to build two versions of lib to avoid the mismatch error.

My question is, is it possible to build a library that could be used both by program compiled with /MD and /MDd settings?

If the answer is yes, is it safe to do so?

If the answer is no, why there is no error when linking the lua51.lib to /MDd application?

Thanks.

Edit

Error message

Error 20 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MDd_DynamicDebug' in Logger.obj
R Sahu
  • 204,454
  • 14
  • 159
  • 270
Hsi-Hung Shih
  • 233
  • 1
  • 7

1 Answers1

4

Question 1:

is it possible to build a library that could be used both by program compiled with /MD and /MDd settings?

Answer: Yes.

Question 2:

Is it safe to do so?

Short answer: Not always.

Longer answer:

When the flag /MD is used, the compiler defines the proprocessor macros _MT, _DLL. When /MDd is used, the compiler defines the proprocessor macros _MT, _DLL, _DEBUG.

It is possible that one or more classes will have different member variables depending on whether _DEBUG is defined. When that is the case, it is most likely not safe to mix codes compiled with the different flags. If you are absolutely certain that none of the objects being passed between the two sets of code have that issue, it's probably safe to mix the two sets of code/libraries.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • Hi @R Sahu, thanks for the answer! I can see why it may be unsafe to mix the library settings, but I still don't quite understand why sometimes visual studio gives errors with mismatches while sometimes it doesn't. Could you elaborate that part a bit? – Hsi-Hung Shih Aug 14 '14 at 20:15
  • @Hsi-HungShih, can you post the error messages you are seeing? – R Sahu Aug 14 '14 at 20:26
  • Sorry I edited the wrong post! the error is LNK2038. – Hsi-Hung Shih Aug 14 '14 at 20:49
  • @Hsi-HungShih, no problem. I put the error message in your question. – R Sahu Aug 14 '14 at 20:52
  • @Hsi-HungShih, I don't see `MD_DynamicRelease` or `MDd_DynamicDebug` in my object files created using VS2010. I wonder whether they were introduced in VS2013. – R Sahu Aug 15 '14 at 18:32
  • I finally found why. The LNK2038 error arises from the use of #pragma detect_mismatch(name, value), like detect_mismatch("RuntimeLibrary", "MT_StaticRelease"). – Hsi-Hung Shih Aug 19 '14 at 04:26
  • @R Sahu For Visual Studio, this pragma is used in a file called "yvals.h" and will be included for lib compiled with cl.exe. However, those pragmas are placed in a #ifdef __cplusplus block, so it won't detect mismatch for library compiled as C language, like the Lua library. Not sure why they do it this way, because lib in C language could still have bugs from runtime library mismatch, as described in this question http://stackoverflow.com/questions/15787892/embedded-lua-does-not-print-to-allocated-console – Hsi-Hung Shih Aug 19 '14 at 04:27