V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
woshichuanqilz
V2EX  ›  C

cpp socket sockaddr: :"struct "重定义的问题

  •  
  •   woshichuanqilz · 2019-02-17 18:27:16 +08:00 · 2130 次点击
    这是一个创建于 1884 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现在会有一个这个问题 :

    error

    我已经照着 一个 Stack Overflow 的连接修改了, https://stackoverflow.com/questions/1372480/c-redefinition-header-files-winsock2-h

    修改的方法就是:

    1. 提前 windows.h
    2. 添加定义 WINSOCKAPI WIN32_LEAN_AND_MEAN

    header 文件 cpp_socket.h 文件里面的 include :

    #ifndef CPP_SOCKET_H
    #define CPP_SOCKET_H
    #pragma once
    
    #define WIN32_LEAN_AND_MEAN
    
    #include <string>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    // Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
    #pragma comment (lib, "Ws2_32.lib")
    #pragma comment (lib, "Mswsock.lib")
    #pragma comment (lib, "AdvApi32.lib")
    

    cpp 文件里面的 include :

    #define _WINSOCKAPI_  
    #define WIN32_LEAN_AND_MEAN
    #include "stdafx.h"
    #include <Windows.h>
    #include <time.h>
    #include "image_tool.h"
    #include <map>
    #include <fmt/format.h>
    #include <cstdlib>
    #include <stdio.h>
    #include <string.h>
    #include "cpp_socket.h"
    

    Stack Overflow 里面的方法都试过了, 不知道现在的问题出在哪里?

    3 条回复    2019-02-17 20:49:05 +08:00
    woshichuanqilz
        1
    woshichuanqilz  
    OP
       2019-02-17 18:43:22 +08:00
    对还有一个 image_tool.h 的 include :


    #ifndef IMAGETOOL_H
    #define IMAGETOOL_H
    #include "stdafx.h"
    #include <windows.h>
    #include <nlohmann/json.hpp>
    #include <algorithm>
    #include <typeinfo>

    #include <string>
    #include <fstream>
    #include <streambuf>
    #include "opencv2/imgproc.hpp"
    #include "opencv2/highgui.hpp"
    #include <iostream>


    这个是 Stack Overflow 上高票问题的解法, 我就是照这个做的。
    https://i.loli.net/2019/02/17/5c693a2020d64.png

    那个 h 文件如果下面写一个 main, 当做 cpp 运行的话是成功的。
    woshichuanqilz
        2
    woshichuanqilz  
    OP
       2019-02-17 19:09:52 +08:00
    有了, 应该把所有的 windows.h 都放到 winsock2.h 之后
    ysc3839
        3
    ysc3839  
       2019-02-17 20:49:05 +08:00 via Android
    看上去你有使用预编译头,这些外部 header 的 include 不应该全都放到预编译头里面吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5107 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 09:45 · PVG 17:45 · LAX 02:45 · JFK 05:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.