【问题标题】:I can't compile this code of mongoose web server's sample example我无法编译 mongoose Web 服务器的示例代码
【发布时间】:2012-12-28 08:28:34
【问题描述】:

简短介绍:-(GCC 版本 4.6.3,OS-Ubuntu 12.04,围绕 mongoose Web 服务器程序工作,所以当我运行“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 上将 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 进行编译时,我继续遇到上述 ^^ 错误

[EDIT]:在 gcc 上添加了 -pthread 选项,仍然显示错误:- 猫鼬.o:在函数load_dll': mongoose.c:(.text+0xa955): undefined reference todlopen' mongoose.c:(.text+0xa9b4): undefined reference to `dlsym' collect2: ld 返回 1 个退出状态

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

【问题讨论】:

    标签: c linux mongoose-web-server


    【解决方案1】:

    -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
    

    【讨论】:

    • @H2CO3 非常感谢!您提到的第一个命令工作正常。但是对于第二个命令,当我通过 ./hello 运行它时发生以下错误:--“加载共享库时出错:libmongoose.so:无法打开共享对象文件:没有这样的文件或目录”“
    • @AkshayPatil 是的,它就是这样工作的。请详细了解动态加载。
    猜你喜欢
    • 2015-08-13
    • 2015-07-03
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多