gpt4 book ai didi

c - 我无法编译 mongoose web 服务器的示例代码

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:53:12 26 4
gpt4 key购买 nike

简短介绍:-(GCC 版本 4.6.3,OS-Ubuntu 12.04,围绕 mongoose 网络服务器程序工作,所以当我运行“make”命令编译和安装 mongoose 时,它​​完成了任务)。

[问题的第 1 部分]这个问题引用了这篇关于 stackowerflow 的帖子。

mongoose web server helloworld program

Valenok 已通过提供 hello 示例程序的链接来回答此帖子。

基本上,我正在尝试编译此链接上给出的示例 hello 程序代码:-

http://code.google.com/p/mongoose/source/browse/examples/hello.c

并将这段代码放在已经编译好的mongoose目录中。(目录中有mongoose.h文件)

下面是我编译hello程序的命令行输出。

akshay@akshay-Inspiron-N5010:~$ gcc mongoose/hello.c -o mongoose/hello
/tmp/ccroC5Z6.o: In function `callback':
hello.c:(.text+0x32): undefined reference to `mg_get_request_info'
hello.c:(.text+0x96): undefined reference to `mg_printf'
/tmp/ccroC5Z6.o: In function `main':
hello.c:(.text+0xee): undefined reference to `mg_start'
hello.c:(.text+0x103): undefined reference to `mg_stop'
collect2: ld returned 1 exit status
akshay@akshay-Inspiron-N5010:~$

[问题的第 2 部分]

现在,我在 mongoose.c 文件中找到了 mg_stop、mg_start、mg_printf 和 mg_get_request_info 的实现,所以我使用 -c 选项编译 mongoose.c 文件: gcc -c -o mongoose.o mongoose.c

我认为我的问题类似于:-

undefined reference to function declared in *.h file

但是当我在 gcc 上使用 -L 选项链接 libmongoose.so 时,出现以下错误:-(libmongoose.so 存在于同一目录中,我的 cwd)

akshay@akshay-Inspiron-N5010:~/mongoose$ gcc -L libmongoose.so -o hello hello.o mongoose.o
mongoose.o: In function `mg_start_thread':
mongoose.c:(.text+0x1369): undefined reference to `pthread_create'
mongoose.o: In function `load_dll':
mongoose.c:(.text+0xa955): undefined reference to `dlopen'
mongoose.c:(.text+0xa9b4): undefined reference to `dlsym'
collect2: ld returned 1 exit status

此外,当我在不使用 libmongoose.so 的情况下进行编译时,我继续遇到上述 ^^ 错误

[编辑]:在 gcc 上添加了 -pthread 选项,仍然显示错误:-mongoose.o:在函数 load_dll' 中:
mongoose.c:(.text+0xa955): undefined reference
dlopen'mongoose.c:(.text+0xa9b4): 未定义对“dlsym”的引用collect2: ld 返回 1 个退出状态

对于我的问题的第 1 部分和第 2 部分:我想摆脱这些错误并成功运行 hello.c 程序示例。提前致谢。

最佳答案

-L 选项不用于链接库,它用于指定动态库的搜索路径。要链接到特定库,请使用 -l。但是,您不需要同时链接 mongoose.olibmongoose.so,其中一个就足够了。

在 Linux 上,您还必须链接 pthread 和动态加载库,因为尽管它们是 C 标准库的一部分,但它们不存在于 libc.so 中。需要注意的另一件事是,最新版本的 binutils(特别是 ld)要求按照符号相互依赖的顺序指定库和目标文件,即。 e.库必须转到命令行的末尾。

总而言之,使用以下命令之一:

gcc -o hello hello.o mongoose.o -ldl -lpthread

gcc -L. -o hello hello.o -lmongoose -ldl -lpthread

关于c - 我无法编译 mongoose web 服务器的示例代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14066726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com