I want to write a static library which uses functions from the winsock api (a winsock wrapper library). I include libws2_32.a and add compiler option -lws2_32. Now when I link to this library in another project, it gives me these errors:
undefined reference to `__imp_WSAStartup'|
for all kinds of winsock functions I am using in my library. I included libws2_32.a and added option -lws2_32 in the project which includes the library as well. I am using CodeBlocks IDE and MinGW 64 bit compiler. How can I fix this? Is it even possible to write such a static wrapper library?
EDIT 1 I removed the compiler option -lws2_32 and the new build log looks like this. It still has the undefined reference to winsock function errors though.
-------------- Build: Debug in LibX (compiler: MinGW 64bit + posix threads)---------------
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\error.cpp -o obj\Debug\src\error.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\filter.cpp -o obj\Debug\src\filter.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\math\group.cpp -o obj\Debug\src\math\group.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\math\matrix.cpp -o obj\Debug\src\math\matrix.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\math\ring.cpp -o obj\Debug\src\math\ring.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\math\vector.cpp -o obj\Debug\src\math\vector.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\net\http.cpp -o obj\Debug\src\net\http.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\net\tcpclient.cpp -o obj\Debug\src\net\tcpclient.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\net\wsa.cpp -o obj\Debug\src\net\wsa.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\string\stringcomparison.cpp -o obj\Debug\src\string\stringcomparison.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\string\stringx.cpp -o obj\Debug\src\string\stringx.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\thread.cpp -o obj\Debug\src\thread.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\timer.cpp -o obj\Debug\src\timer.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\xml\xml.cpp -o obj\Debug\src\xml\xml.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -g -std=c++11 -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\LibX\src\xml\xmlattribute.cpp -o obj\Debug\src\xml\xmlattribute.o
cmd /c if exist bin\Debug\libLibX.a del bin\Debug\libLibX.a
ar.exe -r -s bin\Debug\libLibX.a obj\Debug\src\error.o obj\Debug\src\filter.o obj\Debug\src\math\group.o obj\Debug\src\math\matrix.o obj\Debug\src\math\ring.o obj\Debug\src\math\vector.o obj\Debug\src\net\http.o obj\Debug\src\net\tcpclient.o obj\Debug\src\net\wsa.o obj\Debug\src\string\stringcomparison.o obj\Debug\src\string\stringx.o obj\Debug\src\thread.o obj\Debug\src\timer.o obj\Debug\src\xml\xml.o obj\Debug\src\xml\xmlattribute.o
ar.exe: creating bin\Debug\libLibX.a
Output file is bin\Debug\libLibX.a with size 700.42 KB
-------------- Build: Debug in Stock (compiler: MinGW 64bit + posix threads)---------------
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -std=c++11 -g -std=c++11 -ID:\Misc\CodeBlocksProjects\Stock\src -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\Stock\src\main.cpp -o obj\Debug\src\main.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -std=c++11 -g -std=c++11 -ID:\Misc\CodeBlocksProjects\Stock\src -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\Stock\src\stock.cpp -o obj\Debug\src\stock.o
x86_64-w64-mingw32-g++.exe -std=c++11 -Wall -fexceptions -std=c++11 -g -std=c++11 -ID:\Misc\CodeBlocksProjects\Stock\src -ID:\Misc\CodeBlocksProjects\LibX\src -c D:\Misc\CodeBlocksProjects\Stock\src\x\console.cpp -o obj\Debug\src\x\console.o
x86_64-w64-mingw32-g++.exe -LD:\Misc\CodeBlocksProjects\LibX\src -o bin\Debug\Stock.exe obj\Debug\src\main.o obj\Debug\src\stock.o obj\Debug\src\x\console.o D:\Misc\CodeBlocksProjects\LibX\bin\Debug\libLibX.a "C:\Program Files (x86)\CodeBlocks\MinGW\lib\libws2_32.a"
D:\Misc\CodeBlocksProjects\LibX\bin\Debug\libLibX.a(wsa.o): In function `startWSA()':
D:/Misc/CodeBlocksProjects/LibX/src/net/wsa.cpp:9: undefined reference to `__imp_WSAStartup'
D:\Misc\CodeBlocksProjects\LibX\bin\Debug\libLibX.a(wsa.o): In function `stopWSA()':
D:/Misc/CodeBlocksProjects/LibX/src/net/wsa.cpp:14: undefined reference to `__imp_WSACleanup'
D:\Misc\CodeBlocksProjects\LibX\bin\Debug\libLibX.a(tcpclient.o): In function `TcpClient::connect(std::string, int)':
D:/Misc/CodeBlocksProjects/LibX/src/net/tcpclient.cpp:21: undefined reference to `__imp_socket'
D:/Misc/CodeBlocksProjects/LibX/src/net/tcpclient.cpp:29: undefined reference to `__imp_inet_addr'
D:/Misc/CodeBlocksProjects/LibX/src/net/tcpclient.cpp:33: undefined reference to `__imp_gethostbyname'
D:/Misc/CodeBlocksProjects/LibX/src/net/tcpclient.cpp:48: undefined reference to `__imp_inet_addr'
D:/Misc/CodeBlocksProjects/LibX/src/net/tcpclient.cpp:51: undefined reference to `__imp_htons'
D:/Misc/CodeBlocksProjects/LibX/src/net/tcpclient.cpp:54: undefined reference to `__imp_connect'
D:\Misc\CodeBlocksProjects\LibX\bin\Debug\libLibX.a(tcpclient.o): In function `TcpClient::disconnect()':
D:/Misc/CodeBlocksProjects/LibX/src/net/tcpclient.cpp:64: undefined reference to `__imp_closesocket'
D:\Misc\CodeBlocksProjects\LibX\bin\Debug\libLibX.a(tcpclient.o): In function `TcpClient::send(char const*, unsigned int)':
D:/Misc/CodeBlocksProjects/LibX/src/net/tcpclient.cpp:74: undefined reference to `__imp_send'
D:\Misc\CodeBlocksProjects\LibX\bin\Debug\libLibX.a(tcpclient.o): In function `TcpClient::recv(char*, unsigned int)':
D:/Misc/CodeBlocksProjects/LibX/src/net/tcpclient.cpp:94: undefined reference to `__imp_recv'
collect2.exe: error: ld returned 1 exit status
EDIT 2 I tried calling WSAStartup (winsock function in ws2_32) in the project which includes my static library, and with compiler option -lws2_32 this works fine. Actually now (when I call WSAStartup in the project "Stock") the undefined reference error to the WSAStartup function isn't thrown for the included static library as well. All other winsock functions still have this error though.