2010-08-30 36 views
-1

我試圖在Ubuntu 10.04中使用套接字來實現一個程序。下面是代碼:在Ubuntu中使用C++進行套接字編程

#include <iostream> 
#include <sys/types.h> 
#include<netinet/in.h> 
#include <sys/socket.h> 
#include <netdb.h> 
#include <unistd.h> 
#include <errno.h> 
#include <exception> 
using namespace std; 
using std::exception; 
int main(int argc,char *argv[]){ 
    int sockethandle; 
    if ((sockethandle=socket(AF_INET,SOCK_STREAM,IPPROTO_IP))<0) 
    { 
     close(sockethandle); 
     exit(EXIT_FAILURE); 
    } 
    return 0; 
} 

但這裏有編譯錯誤:

usr/bin/make -f nbproject/Makefile-Debug.mk SUBPROJECTS= .build-conf 
make[1]: Entering directory `/home/david/NetBeansProjects/socket' 
/usr/bin/make -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux-x86/socket 
make[2]: Entering directory `/home/david/NetBeansProjects/socket' 
mkdir -p build/Debug/GNU-Linux-x86 
rm -f build/Debug/GNU-Linux-x86/main.o.d 
g++ -c -g -MMD -MP -MF build/Debug/GNU-Linux-x86/main.o.d -o build/Debug/GNU-Linux-x86/main.o main.cpp 
main.cpp: In function ‘int main()’: 
main.cpp:18: error: ‘EXIT_FAILURE’ was not declared in this scope 
main.cpp:18: error: ‘exit’ was not declared in this scope 
make[2]: *** [build/Debug/GNU-Linux-x86/main.o] Error 1 
make[2]: Leaving directory `/home/david/NetBeansProjects/socket' 
make[1]: *** [.build-conf] Error 2 
make[1]: Leaving directory `/home/david/NetBeansProjects/socket' 
make: *** [.build-impl] Error 2 
BUILD FAILED (exit value 2, total time: 929ms) 

如何這個問題能解決?

+0

你的代碼在我的Linux系統上編譯文件。它看起來像特定於你的環境或IDE的東西(netbeans?) – 2010-08-30 21:58:45

+0

是NetBeans問題是IDE? – 2010-08-30 22:00:49

回答

2

EXIT_FAILUREexit()<stdlib.h>中定義,因爲你沒有在你的模塊中包含這個,編譯器指出它不知道這些符號是什麼意思。

添加:

​​

應該整理出來你的問題。