2012-12-28 30 views
0

简短介绍: - (GCC版本4.6.3,OS-Ubuntu 12.04,解决猫鼬网络服务器程序,所以当我运行“make”命令编译和安装猫鼬,它已经完成了任务)。我无法编译这个猫鼬网络服务器的示例代码

[问题的第一部分] 这个问题是参考这个帖子在stackowerflow。

mongoose web server helloworld program

Valenok已经通过给一个链接到Hello示例程序回答了这个职位。

基本上,我试图编译此链接中给出的示例招呼程序代码: - 。

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

,并把这个代码在猫鼬的已编译的目录(目录中有mongoose.h文件)

以下是用于编译hello程序的命令行输出。

[email protected]:~$ 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 
[email protected]:~$ 

[问题的第2部分]

现在,我发现mg_stop,mg_start,mg_printf和mongoose.c文件mg_get_request_info的实现,所以我编译-c选项为mongoose.c文件: 的gcc -o -c mongoose.o mongoose.c

我想我的问题是相似的: -

undefined reference to function declared in *.h file

但后来当我在GCC libmongoose.so与-L选项链接我收到以下错误: - (libmongoose.so存在于同一个目录,我的CWD)

[email protected]:~/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 to的dlopen” mongoose.c :(.text + 0xa9b4):未定义的引用'dlsym' collect2:ld返回1退出状态

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

回答

5

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

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

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

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

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

@ H2CO3非常感谢!你提到的第一个命令工作正常。 但对于第二个命令出现以下错误 当我通过./hello运行它: - “错误而载入共享库:libmongoose.so:无法打开共享对象文件:没有这样的文件或目录‘’ –

+0

@AkshayPatil是,这就是它的工作原理,请阅读一下动态加载。 – 2012-12-28 10:28:52

+0

好的!再次感谢 –