Kammeot
Kammeot

Reputation: 469

Conflicting declaration when cross compiling with mingw-w64 on Linux

I've been trying to figure out an issue I've been having with cross-compiling C++ code on Linux for Windows. The code I'm trying to compile is:

#include <iostream>

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

I'm trying to compile for a 64 bit windows installation, and so I run:

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

but it produces the following errors:

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;
                        ^

The error line:

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

suggests to me that mingw-w64 is using the linux libraries instead of the ones compiled for windows, but upon searching I cannot seem to figure out how to resolve this. I'm using Archlinux and the mingw-w64 package group from the official repository. I've tried reinstalling the mingw-w64 package group thinking that maybe the libraries were not compiled correctly but I'm still receiving the same errors.

To be clear, I am able to compile this code with:

g++ main.cpp

Any help or anything to point me in the right direction will be greatly appreciated. Thank you.

Upvotes: 1

Views: 7034

Answers (3)

Tom 7
Tom 7

Reputation: 527

I ran into this same issue because of a makefile that erroneously passed -I/usr/include on the command line to x86_64-w64-mingw32-g++. Removing it solved the issue for me.

Upvotes: 0

Kammeot
Kammeot

Reputation: 469

The issue was that there was an environment variable set that mingw-w64 was using to find the linux header files. Specifically, I had set CPLUS_INCLUDE_PATH in my .bashrc a while ago and had forgotten about it. This variable does not generally need to be set unless there is some special circumstance that requires it. I do not personally rely on it. I commented out the export and the compiler seems to be finding all the headers it needs now.

Upvotes: 2

Keith Marshall
Keith Marshall

Reputation: 2034

You appear to be allowing your mingw-w64 compiler to search the native linux-gnu compiler's header file tree. This is completely wrong. Those headers are for use exclusively when compiling native code; you must never allow a cross-compiler to see them. Each individual compiler, both native and cross, will have its own specific set of system headers; each should be allowed to see only those which belong to itself.

You also seem to be confused about the respective roles of libraries and headers. Libraries come into play only at link time; they play no part in the compiling process. Headers describe the features provided by the libraries; it is these descriptions which are used by the compiler. It is the linker which uses the libraries; the linker is a separate program, which is normally invoked by the compiler driver, after completion of the compilation process itself.

Upvotes: 3

Related Questions