iamscottmoyers / simple-libwebsockets-example

Simple libwebsockets Example
66 stars 24 forks source link

Linker errors with current versions of libwebsockets #2

Closed timonsku closed 7 years ago

timonsku commented 7 years ago

Getting a lot of reference errors during linking. f.e.: server.c:10: undefined reference to 'lws_serve_http_file' server.c:33: undefined reference to 'lws_callback_on_writable_all_protocol'

iamscottmoyers commented 7 years ago

Hello! I just tried it out using the latest from the warmcat/libwebsockets project and it links fine for me. The functions you mentioned do exist in the latest libwebsockets library. The only difference I noticed was that lws_client_connect is now marked as deprecated so I've replaced it with lws_client_connect_via_info.

Try running pkg-config libwebsockets --libs --cflags and see what you get. I get -I/usr/local/include -L/usr/local/lib -lwebsockets. Maybe pkg-config isn't pointing to your libwebsockets install?

The Makefile is pretty simple, if you've installed libwebsockets somewhere pkg-config doesn't know about you can compile the client / server with: gcc -I/usr/local/include -L/usr/local/lib -lwebsockets client.c -o client gcc -I/usr/local/include -L/usr/local/lib -lwebsockets server.c -o server ...replacing the /usr/local paths with your libwebsockets install paths.

timonsku commented 7 years ago

Weird, I also tried the latest git version as well as 2.3 stable, same issue on two different platforms. pkgconfig resolves to these same paths for me. Maybe I need libwebsockets compiled with specific flags? It does seem to find it, I also got the deprecation message for lws_client_connect. It seems there is an issue with library binary itself.

iamscottmoyers commented 7 years ago

If it's compiling but not linking then yeah it sounds like your headers are installed ok but the library binary isn't. My libwebsockets build has ssl disabled, but I doubt that's the problem. I'm build on a mac, the commands I ran to install libwebsockets were: git clone https://github.com/warmcat/libwebsockets.git cd libwebsockets mkdir build cd build cmake -DLWS_WITH_SSL=0 .. make sudo make install

I guess you can check the library has been updated by looking at the timestamp of /usr/local/lib/libwebsockets.a. Also nm /usr/local/lib/libwebsockets.a | grep lws_serve_http_file should output something only if the symbol exists in the library.

timonsku commented 7 years ago

Right, I totally forgot about nm. That does output the symbol. My build procedure was exactly the same. I tried again without SSL but same outcome if I run gcc -I/usr/local/include -L/usr/local/lib -lwebsockets client.c -o client

Output: `` /tmp/ccCR0TMo.o: In functioncallback_example': client.c:(.text+0x5c): undefined reference to lws_callback_on_writable' client.c:(.text+0xa8): undefined reference tolws_write' /tmp/ccCR0TMo.o: In function main': client.c:(.text+0x154): undefined reference tolws_create_context' client.c:(.text+0x210): undefined reference to lws_canonical_hostname' client.c:(.text+0x238): undefined reference tolws_client_connect_via_info' client.c:(.text+0x268): undefined reference to lws_callback_on_writable' client.c:(.text+0x27c): undefined reference tolws_service' collect2: error: ld returned 1 exit status

timonsku commented 7 years ago

Ugh ok this seems to be a difference in Linux and MacOS. Reading this https://stackoverflow.com/questions/11112318/linked-cannot-find-symbols-but-libraries-are-read-and-symbols-exist I tried gcc -o client client.c -I/usr/local/include -L/usr/local/lib -lwebsockets and compiled without any issues, seems to be a Linux thing. Maybe you could try if that works as well on Mac? Might be worthwhile to update the make file if that works on both platforms.

iamscottmoyers commented 7 years ago

I just tried it in my Linux VM and I can reproduce the same problem as you. Your fix works on Mac and Linux. I'll update the Makefile! Thanks for debugging it! I've never run into that GCC quirk before.

timonsku commented 7 years ago

Great! Also thanks for these examples, they help a lot.