6

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:

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)

...

Basically, I created a solution for building the oscpack.lib, in the project I added the corresponding .h and .cpp files.

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.

Community
  • 1
  • 1
JohnnyAce
  • 3,569
  • 9
  • 37
  • 59

3 Answers3

10

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.

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.

You can find these kind of dependencies from the MSDN article about, say, closesocket(). It is at the bottom of the article. The Header bit tells you what you need to #include, you got that right. The Library bit tells you what you need to tell the linker to link. Not automatic, you have to take care of it yourself.

Matthew Woo
  • 1,288
  • 15
  • 28
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
3

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)

Andrey
  • 4,216
  • 1
  • 23
  • 31
0

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.

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.

Dima
  • 38,860
  • 14
  • 75
  • 115