zhaojh329 / libuwsc

A Lightweight and fully asynchronous WebSocket client library based on libev
MIT License
289 stars 55 forks source link

Need to build libuwsc example.c incorporating libev and libuwsc in single monolithic binary #22

Closed wb0gaz closed 4 years ago

wb0gaz commented 4 years ago

I need to build example (from example.c) in partially static configuration, without libev.so and without libuwsc.so, which will not be available on a target system (that is, I will copy binary "example" to target system, but the target system will not have installed libev or libuwsc.)

Host (where I will build) is Ubuntu 18.04, 64-bit. Target (where I will execute) is Ubuntu 18.04, 64-bit, but has no shared libraries for libev and libuwsc.

I tested libuwsc initially as cloned from github - it works well (I tested example to communicate OK with existing websocket echo server.)

On host (where I will build), I tried changing cmake command from cmake .. to this:

cmake .. -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF

The result build process is now:

$ make
[ 11%] Building C object src/CMakeFiles/uwsc_s.dir/uwsc.c.o
[ 22%] Building C object src/CMakeFiles/uwsc_s.dir/log.c.o
[ 33%] Building C object src/CMakeFiles/uwsc_s.dir/utils.c.o
[ 44%] Building C object src/CMakeFiles/uwsc_s.dir/buffer/buffer.c.o
[ 55%] Building C object src/CMakeFiles/uwsc_s.dir/sha1.c.o
[ 66%] Building C object src/CMakeFiles/uwsc_s.dir/ssl.c.o
[ 77%] Linking C static library libuwsc.a
[ 77%] Built target uwsc_s
Scanning dependencies of target example
[ 88%] Building C object example/CMakeFiles/example.dir/example.c.o
[100%] Linking C executable example
[100%] Built target example

Now, example (binary) grows from 13408 to 42616 bytes, but when I move example binary to target system (target does not have libev or libuwsc intalled), executing ./example results:

./example: error while loading shared libraries: libev.so.4: cannot open shared object file: No such file or directory

The source system (where I am building) has libev.a (static) and libev.so (shared), but resulting binary example still is referencing libev.so, so it cannot start.

I am not expert on cmake, I think my problem is with with cmake.

Thank you for any help!

wb0gaz commented 4 years ago

This problem turns out to be the way libev.a was generated when I retrieved it using apt.

I downloaded libev from the file repository area of

http://software.schmorp.de/pkg/libev.html

then did ./configure followed by make.

At this point in a directory .lib is the file libev.a, which is far larger than the same file installed using apt. This verison of libev.a was successfully linked using gcc command, and the resulting copy of example, while much larger (several hundred kilobytes), is indeed static as I wanted, and does run on a machine with no other installation of libev or libuwsc.

I am closing this issue as there is no remaining problem in this area.