C/C++ Compile Run插件,使用某些头文件出现重定义的错误,解决方法为修改MinGW的路径

 

运行代码

#include <iostream>
using namespace std;

//#include <windows.h>    ///消息框   ///单独用,可以
#include <io.h> ///文件路径       ///单独用,不可以
#include <direct.h> ///文件路径  ///单独用,不可以
#include <ctime>                 ///单独用,不可以

int main()
{
    // cout<<"ok"<<endl;
    return 0;
}

出现错误:

使用某些头文件,出现重定义的错误

[some_h_file_test_1.cpp 2022-02-21 03:09:36.131]
,,In file included from some_h_file_test_1.cpp:26:
c:\\software\\mingw\\include\\io.h:342:8: error: redefinition of 'struct _wfinddata_t'
  342 | struct _wfinddata_t __struct_finddata_t (time_t, _fsize_t);
      |        ^~~~~~~~~~~~
In file included from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\cwchar:44,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\bits\\postypes.h:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iosfwd:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ios:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ostream:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iostream:39,
                 from some_h_file_test_1.cpp:21:
c:\\software\\mingw\\include\\wchar.h:348:8: note: previous definition of 'struct _wfinddata_t'
  348 | struct _wfinddata_t {
      |        ^~~~~~~~~~~~
In file included from some_h_file_test_1.cpp:26:
c:\\software\\mingw\\include\\io.h:343:8: error: redefinition of 'struct _wfinddatai64_t'
  343 | struct _wfinddatai64_t __struct_finddata_t (time_t, __int64);
      |        ^~~~~~~~~~~~~~~
In file included from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\cwchar:44,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\bits\\postypes.h:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iosfwd:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ios:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ostream:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iostream:39,
                 from some_h_file_test_1.cpp:21:
c:\\software\\mingw\\include\\wchar.h:356:8: note: previous definition of 'struct _wfinddatai64_t'
  356 | struct _wfinddatai64_t {
      |        ^~~~~~~~~~~~~~~
In file included from some_h_file_test_1.cpp:26:
c:\\software\\mingw\\include\\io.h:352:10: error: conflicting declaration of C function 'intptr_t _wfindfirst(const wchar_t*, _wfinddata_t*)'
  352 | intptr_t _wfindfirst (const wchar_t *, struct _wfinddata_t *);
      |          ^~~~~~~~~~~
In file included from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\cwchar:44,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\bits\\postypes.h:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iosfwd:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ios:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ostream:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iostream:39,
                 from some_h_file_test_1.cpp:21:
c:\\software\\mingw\\include\\wchar.h:417:38: note: previous declaration 'long int _wfindfirst(const wchar_t*, _wfinddata_t*)'
  417 | _CRTIMP long __cdecl __MINGW_NOTHROW _wfindfirst (const wchar_t*, struct _wfinddata_t *);
      |                                      ^~~~~~~~~~~
In file included from some_h_file_test_1.cpp:26:
c:\\software\\mingw\\include\\io.h:355:5: error: conflicting declaration of C function 'int _wfindnext(intptr_t, _wfinddata_t*)'
  355 | int _wfindnext (intptr_t, struct _wfinddata_t *);
      |     ^~~~~~~~~~
In file included from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\cwchar:44,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\bits\\postypes.h:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iosfwd:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ios:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ostream:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iostream:39,
                 from some_h_file_test_1.cpp:21:
c:\\software\\mingw\\include\\wchar.h:418:37: note: previous declaration 'int _wfindnext(long int, _wfinddata_t*)'
  418 | _CRTIMP int __cdecl __MINGW_NOTHROW _wfindnext (long, struct _wfinddata_t *);
      |                                     ^~~~~~~~~~
In file included from some_h_file_test_1.cpp:26:
c:\\software\\mingw\\include\\io.h:358:10: error: conflicting declaration of C function 'intptr_t _wfindfirsti64(const wchar_t*, _wfinddatai64_t*)'
  358 | intptr_t _wfindfirsti64 (const wchar_t *, struct _wfinddatai64_t *);
      |          ^~~~~~~~~~~~~~
In file included from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\cwchar:44,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\bits\\postypes.h:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iosfwd:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ios:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ostream:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iostream:39,
                 from some_h_file_test_1.cpp:21:
c:\\software\\mingw\\include\\wchar.h:425:38: note: previous declaration 'long int _wfindfirsti64(const wchar_t*, _wfinddatai64_t*)'
  425 | _CRTIMP long __cdecl __MINGW_NOTHROW _wfindfirsti64 (const wchar_t*, struct _wfinddatai64_t*);
      |                                      ^~~~~~~~~~~~~~
In file included from some_h_file_test_1.cpp:26:
c:\\software\\mingw\\include\\io.h:361:5: error: conflicting declaration of C function 'int _wfindnexti64(intptr_t, _wfinddatai64_t*)'
  361 | int _wfindnexti64 (intptr_t, struct _wfinddatai64_t *);
      |     ^~~~~~~~~~~~~
In file included from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\cwchar:44,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\bits\\postypes.h:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iosfwd:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ios:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ostream:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iostream:39,
                 from some_h_file_test_1.cpp:21:
c:\\software\\mingw\\include\\wchar.h:426:38: note: previous declaration 'int _wfindnexti64(long int, _wfinddatai64_t*)'
  426 | _CRTIMP int __cdecl __MINGW_NOTHROW  _wfindnexti64 (long, struct _wfinddatai64_t*);
      |                                      ^~~~~~~~~~~~~
In file included from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ctime:42,
                 from some_h_file_test_1.cpp:28:
c:\\software\\mingw\\include\\time.h:185:8: error: redefinition of 'struct tm'
  185 | struct tm
      |        ^~
In file included from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\cwchar:44,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\bits\\postypes.h:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iosfwd:40,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ios:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\ostream:38,
                 from c:\\software\\mingw\\lib\\gcc\\mingw32\\9.2.0\\include\\c++\\iostream:39,
                 from some_h_file_test_1.cpp:21:
c:\\software\\mingw\\include\\wchar.h:87:8: note: previous definition of 'struct tm'
   87 | struct tm {
      |        ^~

我的解决之旅如下:

  1. 先去GitHub ”C/C++ Compile Run”看一下有没有问过这个问题,搜索”io.h”、”wchar.h”、”header”、”_wfinddata_t”。结果是没有人问过

  2. google搜索”io.h redefinition of 'struct _wfinddata_t’”(无结果)、”io.h wchar.h _wfinddata_t”(无结果)、”c++ 头文件 重定义”(无结果)、”mingw 重定义”(有点启发,可能与MinGW有关系)

  3. 针对错误,去看一下代码,在”io.h”和”wchar.h”找到了struct _wfinddata_t。问题出在了”io.h”使用没有使用ifndef,而”wchar.h”使用了“ifndef WFINDDATA_T_DEFINED”,所以产生重定义

  4. 对于重定义,感觉很好奇,感觉按道理应该不会发生这样的错误,而且文件还是2020年的。但是google搜索没搜索到对应的询问。在其它的搜索中,大致了解到可能会有重定义的问题。

  5. 之前VSCode是用launch.json等配置的,运行没问题。现在用CodeBlocks尝试,结果也是没问题。看了一下CodeBlocks对应的MinGW,再对应看了”io.h”和”mingw”文件,发现它们都有”#ifndef WFINDDATAT_DEFINED”,所以没有重定义。

  6. 寻找VSCode C/C++ Compile Run 下是使用哪个MinGW,发现有配置选项, “ C-cpp-compile-run: C-compiler The C compiler path (e.g: /usr/bin/gcc or C:\MinGW\bin\gcc.exe)“和”C-cpp-compile-run: Cpp-compiler”,把对应的路径修改为我在CodeBlocks使用的MinGW的路径,就没问题了。

posted @ 2022-02-21 17:31  congmingyige  阅读(449)  评论(0编辑  收藏  举报