6

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.

Cartesius00
  • 23,584
  • 43
  • 124
  • 195

1 Answers1

14

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

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547