0

I have a project that perfectly compiles in VS and from command line. However, in Eclipse (C++) I get the following error:

C:\Users\Lukas\AppData\Local\Temp\ccHNeQ9u.o: In function `main':
C:\Users\Lukas\eclipse-ws\TCPClient\Debug/../src/TCPClient.cpp:36: undefined reference to `WSAStartup@8'
C:\Users\Lukas\eclipse-ws\TCPClient\Debug/../src/TCPClient.cpp:44: undefined reference to `socket@12'
C:\Users\Lukas\eclipse-ws\TCPClient\Debug/../src/TCPClient.cpp:47: undefined reference to `WSAGetLastError@0'
C:\Users\Lukas\eclipse-ws\TCPClient\Debug/../src/TCPClient.cpp:48: undefined reference to `WSACleanup@0'

g++ "-IC:\\MinGW\\include\\" -Iws2_32 -O0 -g3 -Wall -o "src\\TCPClient.o" "..\\src\\TCPClient.cpp" 
..\src\TCPClient.cpp:24:0: warning: ignoring #pragma comment  [-Wunknown-pragmas]
 #pragma comment(lib, "ws2_32.lib")

I already found this solution (C - Undefined Reference to WSAStartup@8') , which basically says:

Build your project/cpp-file from command line using this syntax: gcc prog.c -o prog -lws2_32 (such that winsock library (-lws2_32) is added at the end of the compiling call)

As already mentioned before: This compiles fine in command line, the generated application works as expected ! I also got it running in VS.

However, not in the eclipse world where I need it. I added "ws2_32" in Properties --> C/C++ Build/Settings already to MinGW C++ Linker --> Libraries and GCC C++ Compiler --> libraries but if I understand the "g++" call there's no adding of "-lws2_32" in Eclipse at the end but directly after "g++". I don't know whether this is relevant. But I know the code does not compile in eclipse.

I also added C:\MinGW\bin and ..\include to the Eclipse-Includes folder of my project.

Does anyone have an idea how to solve this within eclipse?

EDIT: Also tried this, but does not change my error messages:

Menu - Project - Properties - c/c++Build - Settings: Register "Tool Settings" - MinGW C++ Linker - Miscellaneous: Lower Part "other objects"

Add: libwsock32.a from bin folder of MinGW.

(Source: MinGW linker error: winsock)

Best regards, Lukas

howlger
  • 31,050
  • 11
  • 59
  • 99
  • _there's no adding of "-lws2_32" in Eclipse at the end but directly after "g++". I don't know whether this is relevant_ It probably is. I believe that `g++` (or, rather, the linker it invokes after it has compiled everything) is sensitive to the order in which libraries are specified on the command line. – Paul Sanders Apr 27 '22 at 14:29
  • You could test this by putting it there when you compile from the command line. If you get the same error, at least you will now what the problem is. And after that, there are probably better resources out there to figure out how to persuade Eclipse to do what you want than SO. I'm not sure if it's a big thing here. – Paul Sanders Apr 27 '22 at 14:29
  • @PaulSanders: Checked what you proposed... And found out what you (and I) expected: Compiling in this order from cmd-line: g++ -lws2_32 TCPClient.cpp -o TCPClient results in the same error message as in my question (...undefined reference to 'WSAStartup@8'...) . So yes,, it IS the position of the argument. But how can I get this to the back in eclipse...wasting so much time with such a bulls*** :D – user18944383 Apr 27 '22 at 14:41
  • Don't know sorry, I don't use it. You'll have to cast around, see my previous comment. – Paul Sanders Apr 27 '22 at 14:42
  • 1
    btw...If anybody reads this: I did not manage to ad MinGW compiler to latest Eclipse version 2022-3. Had to get back to a 2021 version. Only then MinGW worked as expected in my Eclipse C++ environment (Windows). – user18944383 Apr 27 '22 at 14:45

0 Answers0