Question

I have a program which use libevent library

when compile the program, the compiling command is like:

 gcc -o myprogram mysource.c mysource.h -levent 

so it is dynamic linking.

now I want to run this program on a computer where there is no libevent, I need static linking so that my program can be run on that computer, are there any easy steps?

I tried -static, but I got the following error:

    [root@kitty relay]# gcc  -o relay -static mysource.c mysource.h -levent -lpcap
    /usr/bin/ld: cannot find -lpcap
    /usr/bin/ld: cannot find -lc
    collect2: ld returned 1 exit status

why?

Was it helpful?

Solution

You should have libevent.a. Then you can just gcc -o myprogram mysource.c libevent.a.

Or try gcc -o myprogram -static mysource.c -levent.

(And you probably shouldn't specify mysource.h to gcc as it's most likely included into mysource.c with #include "mysource.h".)

OTHER TIPS

From the GCC documentation:

-static

On systems that support dynamic linking, this prevents linking with the shared libraries. On other systems, this option has no effect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top