开发者

Using #pragma detect_mismatch to ensure a DLL uses the correct statically linked library

开发者 https://www.devze.com 2023-02-25 16:24 出处:网络
I have a static S.lib that is used by my D.dll. I\'m trying to use #pragma detect_mismatch to make sure that both were compiled under the same release or debug settings.

I have a static S.lib that is used by my D.dll.

I'm trying to use #pragma detect_mismatch to make sure that both were compiled under the same release or debug settings.

I've followed Holger Grund's instructions here http://boost.2283326.n4.nabble.com/Boost-and-Microsoft-s-SECURE-SCL-td3025203.html

dumpbin on S.lib shows:

 Linker Directives
 -----------------
 /FAILIFMISMATCH:"COMPILED_DEBUG=1"
 /INCLUDE:_dll_impl_interface_mismatch_check
 /DEFAULTLIB:"MSVCRTD"
 /DEFAULTLIB:"OLDNAMES"

I compile D.dll successfully, w开发者_运维问答hich should not happen.

dumpbin on D.dll's D.lib shows:

Linker Directives
-----------------
/FAILIFMISMATCH:"COMPILED_DEBUG=2"
/INCLUDE:_dll_impl_interface_mismatch_check
/DEFAULTLIB:"uuid.lib"
/DEFAULTLIB:"uuid.lib"
/FAILIFMISMATCH:"_MSC_VER=1600"
/FAILIFMISMATCH:"_ITERATOR_DEBUG_LEVEL=2"
/DEFAULTLIB:"msvcprtd"
/DEFAULTLIB:"MSVCRTD"
/DEFAULTLIB:"OLDNAMES"

Any help would be greatly appreciated.

EDIT:

I accidentally defined the symbol 'dll_impl_interface_mismatch_check' in BOTH my static library and my consuming DLL. This meant that the symbol was not looked for in the static library S.lib, and the mismatch directive was never found. I think.


I'm just guessing here - I'll have to experiment with this tonight.

Holger Grund's instructions are designed for objects that depend on the DLL. In your case the DLL depends on the static lib.

So, I'm guessing that you want the _dll_impl_interface_mismatch_check object to be added to the static lib rather than the DLL. So instead of:

extern "C" const char dll_impl_interface_mismatch_check=0;

cl /c /Zl foo.cpp
lib D.lib foo.obj 

try:

extern "C" const char dll_impl_interface_mismatch_check=0;

cl /c /Zl foo.cpp
lib S.lib foo.obj 


You have to build a string with the preprocessor that represents your build settings, and use that string with #pragma detect_mismatch.

E.g.

#if defined(_DEBUG)
    #define FOO_DEBUG_PART "_debug"
#else
    #define FOO_DEBUG_PART "_release"
#endif

#if defined(_MT)
    #define FOO_CRT_PART1 "_MT"
#else
    #define FOO_CRT_PART1 "_st"
#endif

#if defined(_DLL)
    #define FOO_CRT_PART2 "_DLL"
#else
    #define FOO_CRT_PART2 "_LIB"
#endif

// ...

#define FOO_BUILD_SETTINGS  FOO_DEBUG_PART  FOO_CRT_PART1  FOO_CRT_PART2  /* ... */

#pragma detect_mismatch("foo_build_settings", FOO_BUILD_SETTINGS)

The IMO better solution though is to use #pragma comment(lib) to link to your libraries, and then build a similar string and use it as part of the file name of the lib:

// build FOO_BUILD_SETTINGS like above
#pragma comment(lib, "mylib" FOO_BUILD_SETTINGS)

That way you cannot use the wrong library (unless you either change the code or the lib is created with the wrong file name ... or renamed afterwards). And of course, if you're as paranoid as I am, you can always do both :)

0

精彩评论

暂无评论...
验证码 换一张
取 消