Theldus / wsServer

wsServer - a tiny WebSocket server library written in C

Home Page:https://theldus.github.io/wsServer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Static Library undefined references on Windows

altarrok opened this issue · comments

Hi, I am a web developer and this is my first time diving into C projects, but I couldn't find any resolution to this problem after some good googleing so I thought maybe someone in this community has dealt with similar issues.

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.21)
project(server C)

set(CMAKE_C_STANDARD 99)

include_directories(include)

add_executable(server
        src/server.c)
target_link_libraries(server ${CMAKE_SOURCE_DIR}/src/wsServer/build/libws.a)

And here is the undefined resources errors:

====================[ Build | server | Debug ]==================================
"C:\Program Files\JetBrains\CLion 2021.3.4\bin\cmake\win\bin\cmake.exe" --build C:\INODev\age-of-io\c\cmake-build-debug --target server
[1/1] Linking C executable server.exe
FAILED: server.exe 
cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.4\bin\mingw\bin\gcc.exe -g  CMakeFiles/server.dir/src/server.c.obj -o server.exe -Wl,--out-implib,libserver.dll.a -Wl,--major-image-version,0,--minor-image-version,0  ../src/wsServer/build/libws.a  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x13): undefined reference to `__imp_accept'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0xcd): undefined reference to `__imp_closesocket'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x4d5): undefined reference to `__imp_closesocket'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x6b5): undefined reference to `__imp_send'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x8fd): undefined reference to `__imp_recv'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x935): undefined reference to `__imp_recv'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x975): undefined reference to `__imp_recv'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x9b5): undefined reference to `__imp_recv'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x9f5): undefined reference to `__imp_recv'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0xaeb): more undefined references to `__imp_recv' follow
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x10ad): undefined reference to `__imp_getpeername'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x10dc): undefined reference to `__imp_inet_ntop'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x148d): undefined reference to `__imp_send'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x178a): undefined reference to `__imp_recv'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x1979): undefined reference to `__imp_closesocket'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x1aaf): undefined reference to `__imp_send'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x28a4): undefined reference to `__imp_WSAStartup'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x28dd): undefined reference to `__imp_socket'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x2912): undefined reference to `__imp_setsockopt'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x2935): undefined reference to `__imp_htons'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x294f): undefined reference to `__imp_bind'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: ../src/wsServer/build/libws.a(ws.c.obj):ws.c:(.text+0x2966): undefined reference to `__imp_listen'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

I also tried -G "MinGW Makefiles"

But then I get this error:

====================[ Build | server | Debug ]==================================
"C:\Program Files\JetBrains\CLion 2021.3.4\bin\cmake\win\bin\cmake.exe" --build C:\INODev\age-of-io\c --target server -- -j 6
Consolidate compiler generated dependencies of target server
[ 50%] Linking C executable server.exe
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: CMakeFiles\server.dir/objects.a(server.c.obj): in function `onopen':
C:/INODev/age-of-io/c/src/server.c:8: undefined reference to `ws_getaddress'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: CMakeFiles\server.dir/objects.a(server.c.obj): in function `onclose':
C:/INODev/age-of-io/c/src/server.c:16: undefined reference to `ws_getaddress'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: CMakeFiles\server.dir/objects.a(server.c.obj): in function `onmessage':
C:/INODev/age-of-io/c/src/server.c:25: undefined reference to `ws_getaddress'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: C:/INODev/age-of-io/c/src/server.c:38: undefined reference to `ws_sendframe'
C:\Program Files\JetBrains\CLion 2021.3.4\bin\mingw\bin/ld.exe: CMakeFiles\server.dir/objects.a(server.c.obj): in function `main':
C:/INODev/age-of-io/c/src/server.c:48: undefined reference to `ws_socket'
collect2.exe: error: ld returned 1 exit status
mingw32-make[3]: *** [CMakeFiles\server.dir\build.make:100: server.exe] Error 1
mingw32-make[2]: *** [CMakeFiles\Makefile2:82: CMakeFiles/server.dir/all] Error 2
mingw32-make[1]: *** [CMakeFiles\Makefile2:89: CMakeFiles/server.dir/rule] Error 2
mingw32-make: *** [Makefile:123: server] Error 2

After a while, the error changed, now it is the same error with the original issue

Hi @altarrok,
are you able to compile the example file as per the instructions for Windows? If so, I believe you are having a problem with your CMakeLists.txt.

Looking at your output:

undefined reference to `__imp_closesocket'
undefined reference to `__imp_send'
undefined reference to `__imp_closesocket'
...

It seems to me that you are forgetting to link your project to the Windows 'ws2_32' library.

Try changing your CMakeLists.txt to something like:

cmake_minimum_required(VERSION 3.21)
project(server C)

set(CMAKE_C_STANDARD 99)

include_directories(include)

add_executable(server
        src/server.c)
target_link_libraries(server ${CMAKE_SOURCE_DIR}/src/wsServer/build/libws.a ws2_32)

As far as I know, pthreads is statically added to 'libws.a' but ws2_32 is not... so you need to say you want to link to it too.

I also tried -G "MinGW Makefiles"
But then I get this error:

undefined reference to `ws_getaddress'
undefined reference to `ws_socket'
...

These functions are from wsServer... it looks like CMake is not finding 'libws.a', or the functions are not present inside the .a.

You can extract and check with (I think that with the MinGW terminal it is possible):

$ ar x libws.a
$ ls *.o
base64.c.o  handshake.c.o  sha1.c.o  utf8.c.o  ws.c.o
$ nm ws.c.o | grep -E "ws_socket|ws_getaddress"
0000000000000604 T ws_getaddress
0000000000001baa T ws_socket

Thanks a lot for the support!!

Adding ws2_32 as target link libraries solved the issue! This is only for windows I believe

Adding ws2_32 as target link libraries solved the issue!

Amazing =)

This is only for windows I believe

Yes, 'ws2_32' belongs to 'WIndows Sockets 2' which implements some network routines supported by default in the libc of other OSes.