CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    2

    Linking againts libraries compiled with different flags

    Greetings,

    I need to link against two proprietary, closed source libraries. Each of these libraries have been compield with different code generation flags. One was compiled using /MD while the other with /MT. I am getting all kinds of linker errors about msvcprt and libcpmt redeclaring identifiers.

    How can I get my code to link against both of these libraries at the same time?

    Thanks for all your help.

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Linking againts libraries compiled with different flags

    First, if the /MD library (let's call "Lib-A") uses any of the CRT or Standard C++ Library, you'll probably have to use the same compiler used to create Lib-A in the first place.

    The /MT library (let's call "Lib-B") contains a static copy the CRT and C++ Library that it linked against. So if you link directly against this lib, then your module (exe or dll) will have to use that static copy as well. Which means you'll want to use the exact same compiler used to create Lib-B also.

    You can't safely and reliably link these two libraries together into the same module. It may be in the realm of possibility *if* both libs were compiled with the exact same compiler and CRT/C++ libs - but it's highly recommended that you don't.

    What you can do is create a DLL wrapper for each of the libs. A DLL wrapper for Lib-B could be created using any recent MSVC compiler. A DLL wrapper for Lib-A would require the same compiler used to the create Lib-A.

    gg

  3. #3
    Join Date
    Jan 2009
    Posts
    2

    Re: Linking againts libraries compiled with different flags

    Thanks for the explaination and your suggestions.

    However, writing a DLL wrapper would be a very time consming operation as each of the libraries export over 10000 functions/objects. I know that both libraries have been built with some version of VC8 (which I am also using), however I don't know which versions of the crt/c++ were libraries linked against.

    As the code is a proof of concept, I am tempted to try and force link the 2 libs.

    How would I go about doing that?

    Thanks again for your help.

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Linking againts libraries compiled with different flags

    It's worth noting that the wrapper need only support the interfaces to the library that you actually use. I suspect that's rather fewer than 1000.

  5. #5
    Join Date
    Nov 2003
    Posts
    1,902

    Re: Linking againts libraries compiled with different flags

    Instead of creating wrappers for the libs themselves, modularize your own code into 2 separate DLL's. Your own DLL-A will link against Lib-A with /MD, and your own DLL-B will link against Lib-B with /MT.

    Most of the risk lies with Lib-A. You'll want to use the exact same compiler (and possibly service-pack level) used to create Lib-A.

    gg

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Linking againts libraries compiled with different flags

    I'm pretty sure all of this "exact same compiler" stuff only applies if the library used C++ code, incidentally. If it was C, then I believe it's fine regardless.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured