Question

I have been trying to use an included libevent in a program, however I have been receiving the error.

gcc lumberd.c Network.c ../Config.c -o lumberd.x 
Undefined symbols for architecture x86_64:
  "_event_base_new", referenced from:
      _main in lumberd-c86954.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I thought it might be failing to link so I added the -L option and that gave me the same error. I know I have libevent installed in my /usr/local/include directory.

Was it helpful?

Solution

The two flags are:

  • -L to add a search path for libraries. If the include path is /usr/local/include, chances are the library path should be -L/usr/local/lib.
  • -l to link a library. If the library is called libevent.{a,so}, then use -levent.

You can also simply add the fullpath to a specific library (static or dynamic) to your compile flags.

OTHER TIPS

An include file often accompanies a library, but the #include <...> statement does not link that library in! The include file makes certain information available to the compiler, but the linker is after compilation (and often misunderstood since the compiler normally calls the linker for you).

You need to include the linker's -l option... perhaps -l event (where event implies that libevent.a will exist in your library path).

You may also be able to use pkg-config to generate the cflags for you. A example can be found at http://en.wikipedia.org/wiki/Pkg-config. I search for librarys installed on the system with

pkg-config --list-all |grep libevent

and then show the cflags with

pkg-config --cflags --libs libevent

you can then compile with something like(or like in the wiki)

gcc `pkg-config --cflags --libs libevent` blub.c -o blub

so you also dont run into trouble if anything like paths or your system is changing

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