【问题标题】:Conflicting declaration when cross compiling with mingw-w64 on Linux在 Linux 上使用 mingw-w64 交叉编译时声明冲突
【发布时间】:2015-05-23 21:41:32
【问题描述】:

我一直试图找出在 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 主机的标头,这绝对是错误的.但是,我们能否说是mingw在做这件事? mingw-w64 不是 mingw,并且绝不与拥有“mingw”商标的 MinGW.org 相关联(以任何大小写形式)。
  • 我很抱歉。我已经更新了我的问题以修复我犯的错误。我认为只要编译器查看正确的库目录,拉入 linux 头文件就不会成为问题。这是不正确的评估吗?

标签: c++ linux mingw-w64


【解决方案1】:

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

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

【讨论】:

  • 但我仍然不知道它为什么会出现在那里。编译器配置了查找相关头文件所需的所有搜索目录。或者它可能正在使用我不知道的环境变量。
【解决方案2】:

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

【讨论】:

  • 使用类似于declare -p | grep -i /usr/include 的东西可能有助于找到这些。这将打印 /usr/include 的所有 shell 变量和过滤器(不区分大小写)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 2013-10-03
  • 2015-05-13
  • 1970-01-01
  • 2013-04-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多