0

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

[问题的第 1 部分] 这个问题参考了 stackowerflow 上的这篇文章。

mongoose web 服务器 helloworld 程序

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:~$ 

[问题的第二部分]

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

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

对 *.h 文件中声明的函数的未定义引用

但是当我在 gcc 上将 libmongoose.so 与 -L 选项链接时,出现以下错误:-(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: In function load_dll': mongoose.c:(.text+0xa955): undefined reference todlopen' mongoose.c:(.text+0xa9b4): undefined reference to `dlsym' collect2: ld returned 1 exit status

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

4

1 回答 1

4

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

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

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

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

或者

gcc -L. -o hello hello.o -lmongoose -ldl -lpthread
于 2012-12-28T08:46:03.367 回答