简体   繁体   English

将 static 库链接到我在 Visual Studio 2010 上的项目

[英]Linking a static library to my project on Visual Studio 2010

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/ ) 用作我项目的 static 库,但是当我尝试将它添加到示例中时,出现链接错误,例如:

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: function 中引用的未解析的外部符号 __imp__socket@12 “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): error LNK2019: function 中引用的未解析的外部符号 __imp__closesocket@4 "public: __thiscall UdpSocket::Implementation::~Implementation(void)" (??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,然后我包含了(属性>C/C++>附加包含目录)oscpack 库的文件夹,然后在 Linker 选项卡上我添加了库的文件夹位置和名称库的。

Right-click your project in the Solution Explorer window and click Properties > Linker > Input > Additional Dependencies setting.在解决方案资源管理器 window 中右键单击您的项目,然后单击属性 > Linker > 输入 > 附加依赖项设置。 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. VS 项目模板负责告诉 linker 链接最常见的 Windows 库。 Like kernel32.lib, you cannot write a Windows program without it.像 kernel32.lib 一样,没有它就无法编写 Windows 程序。 But not winsock, not every program is going to want to create a socket.但不是 winsock,不是每个程序都想要创建套接字。 That has to be added explicitly.必须明确添加。

You can find these kind of dependencies from the MSDN article about, say, closesocket().您可以从有关 closesocket() 的 MSDN 文章中找到此类依赖项。 It is at the bottom of the article.它在文章的底部。 The Header bit tells you what you need to #include, you got that right. Header位告诉您#include 需要什么,您说对了。 The Library bit tells you what you need to tell the linker to link.位告诉您需要告诉 linker 链接什么。 Not automatic, you have to take care of it yourself.不是自动的,你必须自己处理。

There are correct answers already - you need to specify a winsock library to link with.已经有正确的答案 - 您需要指定要链接的 winsock 库。 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)但对于小型项目,这是我最喜欢的方式: #pragma comment(lib, "ws2_32.lib")只需将其放入您的 main.cpp(注意:特定于 MSVC)

Are you saying you put the library and main.cpp into separate solutions?你是说你把库和 main.cpp 放到单独的解决方案中? Try putting them into the same solution, and setting project references appropriately.尝试将它们放入同一个解决方案中,并适当地设置项目引用。

Also, make sure that you make the changes to the right build configuration.此外,请确保对正确的构建配置进行更改。 I. e. IE。 if you are building in Debug mode, make sure that you have added the lib name and the header directories to the Debug configuration.如果您在调试模式下构建,请确保您已将库名称和 header 目录添加到调试配置中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM