在 Linux 上与 mingw-w64 交叉编译时声明冲突

Conflicting declaration when cross compiling with mingw-w64 on Linux

我一直在努力解决在 Linux 上为 Windows 交叉编译 C++ 代码时遇到的问题。我要编译的代码是:

#include <iostream>

int main(int argc, char** argv){
    std::cout<<"Hello World!\n";
    return 0;
}

我正在尝试编译 64 位 windows 安装,所以我 运行:

x86_64-w64-mingw32-g++ main.cpp

但它会产生以下错误:

In file included from /usr/include/sched.h:34:0,
                 from /usr/include/pthread.h:23,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/x86_64-w64-mingw32/bits/gthr-default.h:35,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/x86_64-w64-mingw32/bits/gthr.h:148,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/ext/atomicity.h:35,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/bits/ios_base.h:39,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/ios:42,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/ostream:38,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/iostream:39,
                 from main.cpp:1:
/usr/include/time.h:75:18: error: conflicting declaration ‘typedef __time_t time_t’
 typedef __time_t time_t;
                  ^
In file included from /usr/x86_64-w64-mingw32/include/stddef.h:7:0,
                 from /usr/lib/gcc/x86_64-w64-mingw32/4.9.2/include/stddef.h:1,
                 from /usr/include/wchar.h:51,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/cwchar:44,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/bits/postypes.h:40,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/iosfwd:40,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/ios:38,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/ostream:38,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/iostream:39,
                 from main.cpp:1:
/usr/x86_64-w64-mingw32/include/crtdefs.h:138:20: note: previous declaration as ‘typedef __time64_t time_t’
 typedef __time64_t time_t;
                    ^
In file included from /usr/x86_64-w64-mingw32/include/c++/4.9.2/cwctype:50:0,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/bits/locale_facets.h:39,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/bits/basic_ios.h:37,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/ios:44,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/ostream:38,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/iostream:39,
                 from main.cpp:1:
/usr/include/wctype.h:52:27: error: conflicting declaration ‘typedef long unsigned int wctype_t’
 typedef unsigned long int wctype_t;
                           ^
In file included from /usr/x86_64-w64-mingw32/include/stddef.h:7:0,
                 from /usr/lib/gcc/x86_64-w64-mingw32/4.9.2/include/stddef.h:1,
                 from /usr/include/wchar.h:51,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/cwchar:44,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/bits/postypes.h:40,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/iosfwd:40,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/ios:38,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/ostream:38,
                 from /usr/x86_64-w64-mingw32/include/c++/4.9.2/iostream:39,
                 from main.cpp:1:
/usr/x86_64-w64-mingw32/include/crtdefs.h:107:24: note: previous declaration as ‘typedef short unsigned int wctype_t’
 typedef unsigned short wctype_t;
                        ^

错误行:

/usr/include/time.h:75:18: error: conflicting declaration ‘typedef __time_t time_t’
 typedef __time_t time_t;

向我建议 mingw-w64 使用 linux 库而不是为 windows 编译的库,但在搜索时我似乎无法弄清楚如何解决这个问题。我正在使用官方存储库中的 Archlinux 和 mingw-w64 包组。我尝试重新安装 mingw-w64 包组,认为可能库未正确编译,但我仍然收到相同的错误。

明确地说,我可以用以下代码编译这段代码:

g++ main.cpp

任何能为我指明正确方向的帮助或任何事情都将不胜感激。谢谢。

您似乎允许您的 mingw-w64 编译器搜索本机 linux-gnu 编译器的 header 文件树。这是完全错误的。那些 header 在编译本机代码时 独占 使用;你必须永远允许cross-compiler看到它们。每个单独的编译器,无论是本地编译器还是交叉编译器,都将拥有自己特定的系统集 header;应该允许每个人只看到属于自己的那些。

您似乎还对图书馆和 header 各自的角色感到困惑。图书馆仅在 link 时间发挥作用;他们在编译过程中没有任何作用。 Headers 描述图书馆提供的功能;编译器使用的正是这些描述。使用这些库的是linker; linker 是一个单独的程序,通常由编译器 driver 在编译过程本身完成后调用。

问题是 mingw-w64 用来查找 linux header 文件的环境变量集。具体来说,我刚才在我的 .bashrc 中设置了 CPLUS_INCLUDE_PATH 并且忘记了它。这个变量一般不需要设置,除非有特殊情况需要。我个人并不依赖它。我注释掉了导出,编译器似乎找到了它现在需要的所有 headers。