6

为什么这不起作用,文件test.c

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

然后: gcc -o test.o -c test.c运行正常,但是

链接: g++ -o test -levent test.o生产

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

所以它不能链接为C++. 如何解决这个问题?我需要将其链接为C++并编译为C.

4

1 回答 1

14

这个问题已经被问过很多次了。在 Linux 上,您应该在编译命令中将库放在目标文件和源文件之后。所以试试

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

避免调用test作为现有实用程序或内置 shell 的测试程序。

作为新手,请记住始终在编译时询问所有警告-Wall并进行调试-g并学习使用gdb

于 2011-11-18T17:07:27.617 回答