如何使用link.exe进行静态链接 [英] How to statically link using link.exe

查看:189
本文介绍了如何使用link.exe进行静态链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Visual Studio 2008命令在Windows上静态链接到名为 Poco 的C ++库.线工具.

I've been trying to statically link against a C++ library called Poco on Windows using the Visual Studio 2008 command line tools.

我用以下程序构建程序:

I build my program with:

cl /I..\poco\lib /c myapp.cpp
link /libpath:..\poco\lib myapp.obj PocoNet.lib

这将导致在运行时需要PocoNet.dll和PocoFoundation.dll的exe.

This results in an exe that at runtime requires PocoNet.dll and PocoFoundation.dll.

我花了一些时间阅读Windows中的链接,并了解到cl /MT静态链接到标准库,而cl /MD动态链接.

I spent some time reading up on linking in Windows, and learned that cl /MT statically links against the standard library, while cl /MD links dynamically.

我试图指定/MT,但这似乎并没有改变.我的应用程序仍然需要Poco DLL. (我也怀疑/MT是默认行为.)

I tried to specify /MT, but that didn't seem to change anything; my app still requires the Poco DLLs. (I also suspect that /MT is the default behavior.)

..\poco\lib下,我发现还有一个PocoNetmt.lib,但是指定它而不是PocoNet.lib会导致一堆LNK2005错误(已定义"):

Looking under ..\poco\lib, I found there was also a PocoNetmt.lib, but specifying that instead of PocoNet.lib resulted in a bunch of LNK2005 errors ("already defined"):

msvcprt.lib(MSVCP90.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> >(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in exp.obj

然后我尝试堆叠更多的标志:

I then tried stacking on more flags:

  • /verbose:lib:对于查看正在发生的事情很有用

  • /verbose:lib: useful for seeing what's happening

/Zl:与以前相同的结果

/nodefaultlib:libcmt.lib /nodefaultlib:msvcprt.lib:出现此错误:

PocoFoundationmt.lib(Exception.obj) : warning LNK4217: locally defined symbol ??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ (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> >(void)) imported in function __ehhandler$??0Exception@Poco@@QAE@ABV01@@Z

  • 完全丢弃.lib

  • dropping the .lib altogether, as suggested here: same error as above

    我也尝试了上述方法的一些组合,但都无济于事.

    I also tried some combinations of the above, all to no avail.

    任何线索将不胜感激.但是,对于调试(或了解)这些类型的问题有用的资源的指针也同样有用.

    Any clues would be greatly appreciated. But just as useful would be any pointers to resources that are useful for debugging (or learning about) these types of issues.

    推荐答案

    您必须在命令行上定义POCO_STATIC并同时链接PocoFoundationmt和PocoNetmt.lib:

    You have to define POCO_STATIC on the command line and link with both PocoFoundationmt and PocoNetmt.lib:

    C:\test>cl /MD /WX /nologo /EHsc /DPOCO_STATIC /DUNICODE /D_UNICODE /I..\poco\Foundation\include /I ..\poco\Net\include /c exp.cpp
    
    exp.cpp
    
    C:\test>link /libpath:..\poco\lib /WX /nologo exp.obj PocoNetmt.lib PocoFoundationmt.lib
    

    [更新] 如果使用/DPOCO_STATIC进行编译,则无需在链接器命令行上指定POCO库.头文件包含#pragma comment(lib, "PocoXXXmt.lib")语句,这些语句应确保将所有必需的库链接到其中.

    [UPDATE] If you compile with /DPOCO_STATIC, then it isn't necessary to specify the POCO libraries on the linker command line. The header files contain #pragma comment(lib, "PocoXXXmt.lib") statements that should ensure that all the necessary libraries will be linked in.

    如果不使用/DPOCO_STATIC进行编译,则DLL导入库将自动链接. [/UPDATE]

    If you don't compile with /DPOCO_STATIC, then the DLL import libraries will be automatically linked instead. [/UPDATE]

    这篇关于如何使用link.exe进行静态链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆