Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot link LIBEVENT as C++

Tags:

c++

c

gcc

g++

libevent

Why this does not work, file test.c:

#include <event.h>
int main(void)
{
    event_init();
    return 0;
}

Then: gcc -o test.o -c test.c runs OK, but

Link: g++ -o test -levent test.o produces

test.o: In function `main':
test.c:(.text+0x5): undefined reference to `event_init'
collect2: ld returned 1 exit status

So it cannot be linked as C++. How to solve this? I need to link it as C++ and compile as C.

like image 929
Cartesius00 Avatar asked Nov 18 '11 17:11

Cartesius00


1 Answers

This question has been asked many times. On Linux, you should put libraries after object and source files in the compilation command. So try

g++ -Wall -g -c mytest.cc 
g++ -Wall -g mytest.o -levent -o mytest

Avoid calling your test program test which is an existing utility or shell builtin.

As a newbie, remember to always compile with all warnings asked -Wall and for debugging -g and learn to use gdb

like image 59
Basile Starynkevitch Avatar answered Sep 18 '22 01:09

Basile Starynkevitch