error: expected constructor, destructor, or type conversion before ‘(’ token WSAStartup (versionWanted, &wsaData);

error: expected constructor, destructor, or type conversion before ‘(’ token WSAStartup (versionWanted, &wsaData);

#pragma once

#include <iostream>
#include <string>

#include <cstring>
#include <cstdio>
#include <cstdlib>

#ifdef __linux__
#include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #include <unistd.h>

    #define WSACleanup()
    #define closeConnection(fd) ::close(fd)
#elif _WIN32
    #define WIN32_LEAN_AND_MEAN

    #include <windows.h>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdlib.h>
    #include <stdio.h>

    WORD versionWanted = MAKEWORD(1, 1);
    WSADATA wsaData;
    WSAStartup (versionWanted, &wsaData);

    #define closeConnection(fd) closesocket(fd);
#endif
#include <openssl/ssl.h>
using namespace std;
...

使用此命令编译:

x86_64-w64-mingw32-g++ main.cpp -I include -o main.exe -lssl -lcrypto -lws2_32 -static

编译错误:
<b>错误:'(' token WSAStartup (versionWanted, &wsaData);</b> 之前需要构造函数、析构函数或类型转换

主机:Kubuntu 18.04 LTS

Q_1:为什么给我这个错误?
Q_2: 在使用 mingw64 时(或仅在 msvc 上时)我是否应该使用注释...进行链接?

除了初始化和声明,所有代码都必须在函数或方法中。您需要将以下代码放入函数中:

WORD versionWanted = MAKEWORD(1, 1);
WSADATA wsaData;
WSAStartup (versionWanted, &wsaData);