c++ - C和C++的编译区别

标签 c++ c compilation

我有一段代码看起来像

#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>


int main(int argc, char *argv[])
{
    struct hostent *server;
    server = gethostbyname(argv[1]);
    struct in_addr * address=(in_addr * )server->h_addr;
        fprintf(stderr,"[%d] [%s] server [%s] \n",__LINE__,__func__,inet_ntoa(*address));
}

当我使用 g++ 编译器时,它得到了编译,但同样给出了 gcc 编译器的以下错误

myclient.c: In function ‘main’:
myclient.c:10:31: error: ‘in_addr’ undeclared (first use in this function)
myclient.c:10:31: note: each undeclared identifier is reported only once for each function it appears in
myclient.c:10:41: error: expected expression before ‘)’ token

我是不是漏掉了什么?

最佳答案

您应该在 c 代码中使用 struct in_addr,而不仅仅是 in_addr(在转换时也是如此)。

关于c++ - C和C++的编译区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8986122/

相关文章:

c++ - 将数据从 const void *data 转换为 double

c++ - 用多个文件编译c++程序

c++ - 尝试在 CentOS 上编译 32 位应用程序导致错误

c++ - 适用于 iPhone 的 C 与 C++(Objective-C 与 Objective-C++)

c - 在 C 中使用整型全局变量指针初始化全局变量

c++ - 优化 : Inline or Macrofunction?

arrays - C语言如何在不指定数组大小的情况下将数字读入数组

c++ - 尝试使用 crypto++ 编译我的项目时出现错误 C2711

c++ - 如何正确删除那些指针?

python - 在 C++ 中嵌入 Python。传递接收列表列表的字符串 vector