简体   繁体   English

在 Linux 中出现函数参数 (C++) 的未声明标识符错误,但在 Windows 中没有

[英]Got undeclared identifier error for a function argument (C++) in Linux, but not in Windows

I have written these codes:我写了这些代码:

#include <iostream>
#include <limits>
#include <string>
#ifdef _WIN32
  #include "WinSock2.h"
  #include "WS2tcpip.h"

  #pragma comment(lib,"ws2_32.lib")

#elif __linux__
  #include <sys/socket.h>
  #include <arpa/inet.h>
  #define SOCKET socket
#else
  #error Compiler cannot interpret platform. Please compile this program in Windows 32/64-bit or Linux!
#endif

//...some codes here for initalization for _WIN32

int socket_create(SOCKET &socketHandler)
{
  socketHandler = socket(AF_INET, SOCK_STREAM, 0);

  if (socketHandler == INVALID_SOCKET)
  {
    #ifdef _WIN32
      return (WSAGetLastError());
    #else
      return -1;
    #endif
  }
  else
  {
    return 0;
  }
}

//...some codes for other functions
//...main function

When compiled using Clang 11.0.0 on Windows 10 x64 with arguments:在带有参数的 Windows 10 x64 上使用 Clang 11.0.0 编译时:

clang server.cpp -Wall -Wextra -std=c++17 -g -glldb -lws2_32 -fexceptions -O0 -o target\\debug\\win-amd64\\server.exe -fms-compatibility -m64 clang server.cpp -Wall -Wextra -std=c++17 -g -glldb -lws2_32 -fexceptions -O0 -o target\\debug\\win-amd64\\server.exe -fms-compatibility -m64

it was flawless.它完美无瑕。 The program compiled without even a warning and was running perfectly.该程序在没有警告的情况下编译并且运行完美。 However, bringing it straight to linux (Ubuntu 20.04 x64, with clang-11 and libc++-dev & libc++abi-dev installed), and compiled using Clang 11.0.0 in there with arguments:但是,将它直接带到 linux(Ubuntu 20.04 x64,安装了 clang-11 和 libc++-dev 和 libc++abi-dev),并在那里使用 Clang 11.0.0 编译并带有参数:

clang-11 server.cpp -Wall -Wextra -std=c++17 -g -glldb -fexceptions -O0 -o target\\debug\\deb-amd64\\server.exe -m64 clang-11 server.cpp -Wall -Wextra -std=c++17 -g -glldb -fexceptions -O0 -o target\\debug\\deb-amd64\\server.exe -m64

It gives me this error:它给了我这个错误:

error: use of undeclared identifier 'socketHandler'
int socket_create(SOCKET &socketHandler)
                          ^

and

error: expected ';' after top level declarator
int socket_create(SOCKET &socketHandler)
                                        ^

Question: Why is it different in Linux?问题:为什么在 Linux 中会有所不同? Did I miss something in declaration?我在声明中遗漏了什么吗? If so, how could the same version of clang in Windows would compile it fine, while straight up refused in Linux?如果是这样,如何在 Windows 中使用相同版本的 clang 编译它,而在 Linux 中直接拒绝? TBH, this is my first time compiling things in Linux, so I don't know if I miss something that I should do in Linux but not in Windows for C++. TBH,这是我第一次在 Linux 中编译东西,所以我不知道我是否错过了一些我应该在 Linux 中而不是在 Windows 中为 C++ 做的事情。 Thank you.谢谢你。

What socket() in Linux returns is int , not socket nor SOCKET . Linux 中的socket()返回的是int ,而不是socketSOCKET

You are using #define SOCKET socket in Linux mode and it will have it put function name where type name is expected like this:您在 Linux 模式下使用#define SOCKET socket ,它会将函数名称放在需要类型名称的位置,如下所示:

int hoge(){ return 0; }

int fuga(hoge& x) { // error
    return 0;
}

This error is not produced in Windows because the problematic line #define SOCKET socket is not used in Windows thanks to the #ifdef directives.这个错误不会在 Windows 中产生,因为有问题的行#define SOCKET socket没有在 Windows 中使用,这要归功于#ifdef指令。

In conclusion, the line #define SOCKET socket should be typedef int SOCKET;总之,# #define SOCKET socket应该是typedef int SOCKET; . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM