已鎖定 Linking statically to standard libraries

  • Friday, July 13, 2007 11:48 AM
     
     
    Hi!
    I have a simple application. It is linked against msvcrt.dll and works fine on my computer. Putting it into other computer causes error saying that the application couldn't be run because of improper configuration. I am trying to link it statically (project proporties->C/C++->Code Generation->Runtime Library set to Multi threaded /MT). But it causes linking errors:
    Code Snippet

    msvcprt.lib(MSVCP80.dll) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::operator=(char const *)" (??4?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@PBD@Z) already defined in libcpmt.lib(locale0.obj)
    msvcprt.lib(MSVCP80.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(unsigned int,char)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ID@Z) already defined in libcpmt.lib(cout.obj)
    MSVCRT.lib(MSVCR80.dll) : error LNK2005: _free already defined in LIBCMT.lib(free.obj)
    MSVCRT.lib(MSVCR80.dll) : error LNK2005: _atoi already defined in LIBCMT.lib(atox.obj)
    MSVCRT.lib(MSVCR80.dll) : error LNK2005: _sprintf already defined in LIBCMT.lib(sprintf.obj)
    MSVCRT.lib(MSVCR80.dll) : error LNK2005: _malloc already defined in LIBCMT.lib(malloc.obj)
    MSVCRT.lib(MSVCR80.dll) : error LNK2005: _strncmp already defined in LIBCMT.lib(strncmp.obj)
    MSVCRT.lib(MSVCR80.dll) : error LNK2005: _isdigit already defined in LIBCMT.lib(_ctype.obj)
    MSVCRT.lib(MSVCR80.dll) : error LNK2005: _calloc already defined in LIBCMT.lib(calloc.obj)
    MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in LIBCMT.lib(typinfo.obj)
    MSVCRT.lib(ti_inst.obj) : error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in LIBCMT.lib(typinfo.obj)
    LINK : warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library

    Any ideas what is wrong?

All Replies

  • Friday, July 13, 2007 1:20 PM
     
     Answered
    Make sure that all other CRT-dependant libraries you are linking also use the statically linked version of the CRT, otherwise you will get a bunch of "already defined"-errors.
  • Friday, July 13, 2007 2:09 PM
     
     
    Right, thanks Smile.
  • Tuesday, August 30, 2011 1:13 PM
     
     

    Hi!

    It seems a litte "late" to reply, but i have the same question now. you said that we can "Make sure that all other CRT-dependant libraries use the statically linked version of the CRT", how exactely? I don't well understand....I have set the Runtime Library to Multi threaded /MT for Rlease mode, and MTD for Debug mode. There is anything else we can do?

    If you can see this, please help. thanks!

  • Saturday, April 14, 2012 4:05 PM
     
     
    You have to do the same when building wxWidgets for Release and Debug mode. For example with Visual Studio open wx/build/msw/wx.dsw, select all components from adv to xrc, open Properties / Configuration Properties / C/C++ / Code Generation, and modify the Runtime Library command line option before building a wxWidgets configuration.