在Solaris下链接Sun Studio 10中的错误

Sid*_*qui 2 c c++ solaris network-programming sunstudio

我写了一个这样的测试程序:

#include <sys/socket.h>
int main( void ) {
    int  sock = socket(AF_INET, SOCK_DGRAM, 0);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

并试图编译它:

$ /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
Run Code Online (Sandbox Code Playgroud)

输出是"未引用符号套接字".

请给我指示,以便我可以解决这个问题.

PP.*_*PP. 6

这是问题所在.

我写了一个这样的测试程序:

#include <sys/socket.h>
int main( void ) {
    int  sock = socket(AF_INET, SOCK_DGRAM, 0);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

并尝试编译它(这是真正有用的输出,你必须记住,现代编译器真的尽力帮助你解决任何问题):

$ /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
Run Code Online (Sandbox Code Playgroud)

现在,从输出中我们可以看到符号socket未被引用.因此,如果您键入,man socket您将从手册页中获得以下内容:

SYNOPSIS
     cc [ flag ... ] file ... -lsocket  -lnsl  [ library ... ]
Run Code Online (Sandbox Code Playgroud)

-l标志表示要使用此功能,您还需要链接指定的库.在这种情况下,您被告知要添加-lsocket -lnslcc命令行,如下所示:

$ /tool/sunstudio/bin/cc test.c -lsocket -lnsl
Run Code Online (Sandbox Code Playgroud)