gpt4 book ai didi

c++ - 在 Linux 上使用 mingw-w64 交叉编译时声明冲突

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:36:19 24 4
gpt4 key购买 nike

我一直在努力解决在 Linux for 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 在编译 native 代码时独占使用;您必须绝不允许交叉编译器看到它们。每个单独的编译器,无论是本地编译器还是交叉编译器,都有自己特定的系统头文件集;每个人都应该被允许只看到属于自己的那些。

您似乎也对库和 header 的各自作用感到困惑。库仅在链接时起作用;他们在编译过程中没有任何作用。 header 描述库提供的功能;编译器使用的正是这些描述。它是使用库的链接器;链接器是一个单独的程序,通常在编译过程本身完成后由编译器驱动程序调用。

关于c++ - 在 Linux 上使用 mingw-w64 交叉编译时声明冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30417871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com