Solaris 下 Sun Studio 10 中的链接错误
我写了一个这样的测试程序:
#include <sys/socket.h>
int main( void ) {
int sock = socket(AF_INET, SOCK_DGRAM, 0);
return 0;
}
并尝试编译它:
$ /tool/sunstudio/bin/cc test.c
Undefined first referenced
symbol in file
socket test.o
ld: fatal: Symbol referencing errors. No output written to a.out
输出是“符号套接字未被引用”。
请给我指示,以便我解决这个问题。
I wrote a test program like this:
#include <sys/socket.h>
int main( void ) {
int sock = socket(AF_INET, SOCK_DGRAM, 0);
return 0;
}
And tried to compile it:
$ /tool/sunstudio/bin/cc test.c
Undefined first referenced
symbol in file
socket test.o
ld: fatal: Symbol referencing errors. No output written to a.out
The output is "symbol socket is not referenced".
Kindly give me the direction so that I can resolve this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是问题所在。
我编写了一个这样的测试程序:
并尝试对其进行编译(这是真正有帮助的输出,您必须记住,现代编译器确实尽力帮助您解决任何问题):
现在,从输出中我们可以看到未引用符号
socket
。因此,如果您输入man socket
,您将从手册页中获得以下内容:-l
标志表示要使用此函数,您还需要链接指定的库。在这种情况下,系统会告诉您将-lsocket -lnsl
添加到cc
命令行,如下所示:Here's the question.
I wrote a test program like this:
And tried to compile it so (this is the output that really helps, you have to remember that modern compilers really try their best to help you fix any problems):
Now, from the output we can see that the symbol
socket
is not referenced. So if you typeman socket
you will get the following from the man page:The
-l
flag indicates that to use this function you need to also link the named library. In this case you are being told to add-lsocket -lnsl
to thecc
command line as follows:您必须在命令行中链接到套接字库:
you have to link in the socket library, in the command line:
您需要至少添加
-lsocket
到您的链接步骤,即针对 libsocket.so 的链接。不过,我不知道如何在 SunStudio UI 中执行此操作 - 它的项目是否基于 makefile?手册页通常是查找所需库的好地方;在本例中,套接字手册页 还推荐
-lnsl
(请参阅概要),因此可能也需要它,但我不记得它是必要的。You need to add at least
-lsocket
to your link-step, i.e. link against libsocket.so. I don't know how to do that in the SunStudio UI, though - are its projects makefile based?The man page is usually a good place to look for required libraries; in this case the man page for socket also recommends
-lnsl
(see the synopsis) so that might be required too but I don't remember it being necessary.