C++ 将静态库链接到我在 Visual Studio 2010 上的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10049640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linking a static library to my project on Visual Studio 2010
提问by JohnnyAce
I want to use oscpack (http://code.google.com/p/oscpack/) as a static library for my project but when I try to add it to an example, I get linking errors, for example:
我想使用 oscpack ( http://code.google.com/p/oscpack/) 作为我项目的静态库,但是当我尝试将它添加到示例中时,出现链接错误,例如:
1>oscpackd.lib(UdpSocket.obj) : error LNK2019: unresolved external symbol __imp__socket@12 referenced in function "public: __thiscall UdpSocket::Implementation::Implementation(void)" (??0Implementation@UdpSocket@@QAE@XZ)
1>oscpackd.lib(UdpSocket.obj) : error LNK2019: unresolved external symbol __imp__closesocket@4 referenced in function "public: __thiscall UdpSocket::Implementation::~Implementation(void)" (??1Implementation@UdpSocket@@QAE@XZ)
...
1>oscpackd.lib(UdpSocket.obj):错误LNK2019:函数“public: __thiscall UdpSocket::Implementation::Implementation(void)”中引用的未解析的外部符号__imp__socket@12 (??0Implementation@UdpSocket@@QAE@XZ)
1>oscpackd.lib(UdpSocket.obj):错误LNK2019:函数“public: __thiscall UdpSocket::Implementation::~Implementation(void)”中引用的未解析的外部符号__imp__closesocket@4 (??1Implementation@UdpSocket@@QAE@XZ )
...
Basically, I created a solution for building the oscpack.lib, in the project I added the corresponding .h and .cpp files.
基本上,我创建了一个用于构建 oscpack.lib 的解决方案,在项目中我添加了相应的 .h 和 .cpp 文件。
Then on the example solution, I added my main.cpp and then I included (properties>C/C++>Additional Include Directories) the folder of the oscpack library, then on the Linker tab I added the folder location of the libs and the name of the libs.
然后在示例解决方案中,我添加了我的 main.cpp,然后我包含了 (properties>C/C++>Additional Include Directories) oscpack 库的文件夹,然后在 Linker 选项卡上我添加了 libs 的文件夹位置和名称的库。
回答by Hans Passant
Right-click your project in the Solution Explorer window and click Properties > Linker > Input > Additional Dependencies setting. You'll have to add ws2_32.lib.
在解决方案资源管理器窗口中右键单击您的项目,然后单击属性 > 链接器 > 输入 > 附加依赖项设置。您必须添加 ws2_32.lib。
The VS project templates take care of telling the linker to link the most common Windows libraries. Like kernel32.lib, you cannot write a Windows program without it. But not winsock, not every program is going to want to create a socket. That has to be added explicitly.
VS 项目模板负责告诉链接器链接最常见的 Windows 库。像 kernel32.lib 一样,没有它你就不能编写 Windows 程序。但不是 winsock,不是每个程序都想创建一个套接字。必须明确添加。
You can find these kind of dependencies from the MSDN article about, say, closesocket(). It is at the bottom of the article. The Headerbit tells you what you need to #include, you got that right. The Librarybit tells you what you need to tell the linker to link. Not automatic, you have to take care of it yourself.
您可以从 MSDN 文章中找到这些依赖项,例如 closesocket()。它在文章的底部。在头位告诉你,你需要的#include什么,你说对了。该库位告诉你,你需要告诉链接器链接的内容。不是自动的,你必须自己照顾它。
回答by Andrey
There are correct answers already - you need to specify a winsock library to link with.
But this is my favorite way to do so for small projects:
#pragma comment(lib, "ws2_32.lib")
Just put it in your main.cpp (NOTE: MSVC specific)
已经有正确的答案 - 您需要指定要链接的 winsock 库。但对于小型项目,这是我最喜欢的方法:
#pragma comment(lib, "ws2_32.lib")
只需将其放在 main.cpp 中(注意:MSVC 特定)
回答by Dima
Are you saying you put the library and main.cpp into separate solutions? Try putting them into the same solution, and setting project references appropriately.
你是说你把库和 main.cpp 放在单独的解决方案中?尝试将它们放入同一个解决方案中,并适当设置项目引用。
Also, make sure that you make the changes to the right build configuration. I. e. if you are building in Debug mode, make sure that you have added the lib name and the header directories to the Debug configuration.
此外,请确保对正确的构建配置进行更改。IE。如果您在 Debug 模式下构建,请确保已将 lib 名称和头目录添加到 Debug 配置中。