0
votes

I'm trying to write a C++ program in Code::Blocks that uses SimpleSockets. The files in my program are in my program, main.cpp, simplesocket.cpp, passivesockets.cpp, activesockets.cpp, simplesocket.h, passivesockets.h, activesockets.h. All of these files are in the same directory and they are included in main.cpp using quotes (#include "simplesocket.h"). When I try to compile it I get some of the following errors.

-------------- Build: Debug in sys32 ---------------

g++.exe -Wall -g -I"C:\Documents and Settings\Administrator\My Documents\Downloads\curl-7.27.0-devel-mingw32" -IC:\MinGW\mingw32 -IC:\MinGW\lib\gcc\mingw32 -IC:\MinGW\lib\gcc\mingw32\4.7.0 -IC:\MinGW\lib\gcc\mingw32\4.7.0\include -IC:\MinGW\lib\gcc\mingw32\4.7.0\include\c++\backward -IC:\MinGW\lib\gcc\mingw32\4.7.0\include\c++\mingw32 -c C:\system32\sys32\main.cpp -o obj\Debug\main.o In file included from C:\system32\sys32\ActiveSocket.h:46:0, from C:\system32\sys32\main.cpp:2:

undefined reference to `_htons@4

undefined reference to `_inet_addr@4

undefined reference to `_inet_addr@4

undefined reference to..............

undefined reference to..............

These errors are coming from the SimpleSockets.cpp, PassiveSockets.cpp and ActiveSockets.cpp. From what I can find on StackOverflow this type of error is a linking error. I found this post the files are added to the project and that doesn't work. There's a lot of stuff on StackOverflow but I can't seem to find how to get it to work. I'm looking for where in code::blocks I could manually link the other .cpp files. I've even tried change the compilers from migw32-g++ to g++, just to see what would happen.

I've also tried building with the -lw2_32.lib command but I still get the same errors. Below is the output after adding that command.

-------------- Build: Debug in sys32 ---------------

g++.exe -Wall  -g  -lws2_32.lib   -I"C:\Documents and Settings\Administrator\My Documents\Downloads\curl-7.27.0-devel-mingw32" -IC:\MinGW\mingw32 -IC:\MinGW\lib\gcc\mingw32 -IC:\MinGW\lib\gcc\mingw32\4.7.0 -IC:\MinGW\lib\gcc\mingw32\4.7.0\include -IC:\MinGW\lib\gcc\mingw32\4.7.0\include\c++\backward -IC:\MinGW\lib\gcc\mingw32\4.7.0\include\c++\mingw32  -c C:\system32\sys32\PassiveSocket.cpp -o obj\Debug\PassiveSocket.oIn file included from C:\system32\sys32\ActiveSocket.h:46:0,from C:\system32\sys32\PassiveSocket.h:45,from C:\system32\sys32\PassiveSocket.cpp:43:

PassiveSocket.cpp:70: undefined reference to htons@4
PassiveSocket.cpp:78: undefined reference to htonl@4
PassiveSocket.cpp:82: undefined reference to inet_addr@4

Thanks again for the help.

2

2 Answers

2
votes

I gather that you are using Code::blocks + MinGW Combo and not any other compiler.

Go to Project->Build Options
Click on the "linker settings" tab and add the following in the "Other Linker options:" multi-line textbox:-

-lwsock32
-lws2_32

Remember to put each entry on a new line.
Notice i've omitted the ".lib" file extension

0
votes

put -lws2_32.lib while compiling.