在Linux上与mingw-w 64交叉编译时的声明

wsewodh2  于 2023-10-16  发布在  Linux
关注(0)|答案(3)|浏览(102)

我一直在试图解决在Windows的Linux上交叉编译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-w 64使用的是Linux库,而不是为windows编译的库,但在搜索时,我似乎无法找出如何解决这个问题。我使用的是Archlinux和来自官方仓库的mingw-w 64包组。我试着重新安装mingw-w 64包组,认为可能库没有正确编译,但我仍然收到同样的错误。
为了清楚起见,我可以用以下代码编译这段代码:

g++ main.cpp

任何帮助或任何东西,以指出我在正确的方向将不胜感激。谢谢

q3qa4bjr

q3qa4bjr1#

您似乎允许mingw-w 64编译器搜索本机linux-gnu编译器的头文件树。这是完全错误的。这些头文件只在编译本机代码时使用;你必须 * 永远不要 * 允许交叉编译器看到它们。每一个独立的编译器,无论是本机的还是交叉的,都有自己特定的系统头文件集;每个人都应该被允许只看到属于自己的东西。
您似乎还对库和头文件各自的角色感到困惑。库只在链接时发挥作用;他们在汇编过程中没有任何作用。标题描述了库提供的功能;编译器使用的正是这些描述。它是使用库的链接器;链接器是一个单独的程序,在编译过程本身完成之后,它通常由编译器 * 驱动程序 * 调用。

hkmswyz6

hkmswyz62#

问题是mingw-w 64使用一个环境变量集来查找Linux头文件。具体地说,我之前在.bashrc中设置了CPLUS_INCLUDE_PATH,但是忘记了。这个变量一般不需要设置,除非有特殊情况需要设置。我个人并不依赖它。我注解掉了导出,编译器现在似乎找到了它需要的所有头。

k2arahey

k2arahey3#

我遇到了同样的问题,因为一个makefile在命令行上错误地将-I/usr/include传递给x86_64-w64-mingw32-g++。删除它为我解决了这个问题。

相关问题