无法将 LIBEVENT 链接为 C++

发布于 2024-12-16 19:49:22 字数 491 浏览 5 评论 0原文

为什么这不起作用,文件 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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

開玄 2024-12-23 19:49:22

这个问题已经被问过很多次了。在 Linux 上,您应该在编译命令中将库放在对象和源文件之后。因此,请尝试

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

避免调用您的测试程序 test 这是现有的实用程序或 shell 内置程序。

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文