在memset中包含string.h会导致错误。
memset包含在string.h中。如果我使用string.h,则会出现错误。
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <iostream>
using namespace std;
int main() {
int sock_fd ;
struct sockaddr_in serv_addr, cli_addr;
sock_fd = socket(AF_INET, SOCK_STREAM, 0);
if (sock_fd < 0)
cout << " ERROR opening socket" << endl;
memset((char*)&serv_addr, '\0', sizeof(serv_addr));
}
这是错误:
/tmp/cc88GEO4.o: In function `main':
server.C:(.text+0x3a): undefined reference to `std::cout'
server.C:(.text+0x3f): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
server.C:(.text+0x44): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
server.C:(.text+0x4c): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/tmp/cc88GEO4.o: In function `__static_initialization_and_destruction_0(int, int)':
server.C:(.text+0xa4): undefined reference to `std::ios_base::Init::Init()'
server.C:(.text+0xb3): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status