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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With