Clang++ fails to locate any stdlib headers

2019-08-06 05:05发布

问题:

From a mac os x system I am trying to cross compile a simple C++11 program but clang++ fails to find even simple headers:

test.cc

#include <string>

int main(int argc, char *argv[])
{
    return 0;
}

And here is the error

$ clang++ --std=c++11 --stdlib=libc++ test.cc -target i386-win32 -o test
clang: warning: argument unused during compilation: '--stdlib=libc++'
test.cc:1:10: fatal error: 'string' file not found
#include <string>
         ^
1 error generated.

It compiles just fine without the -target argument.

I also tried without the --stdlib=libc++ argument and that didn't help.

EDIT Turns out you can't really cross compile to windows just by adding a flag:

$ clang++ -std=c++11 -v test.cc -target i386-win32 -o test
Apple LLVM version 7.0.0 (clang-700.0.72)
Target: i386--windows-msvc
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple i386--windows-msvc -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name test.cc -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -target-cpu pentium4 -target-linker-version 253.2 -v -dwarf-column-info -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.0 -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.0/include -internal-isystem C:/Program Files/Microsoft Visual Studio 10.0/VC/include -internal-isystem C:/Program Files/Microsoft Visual Studio 9.0/VC/include -internal-isystem C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include -internal-isystem C:/Program Files/Microsoft Visual Studio 8/VC/include -internal-isystem C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/drninjabatman/Scratch -ferror-limit 19 -fmessage-length 181 -mstackrealign -fms-extensions -fms-compatibility -fms-compatibility-version=17.00 -fdelayed-template-parsing -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /var/folders/j_/56ztdh452d95t2wtk6zx0j6w0000gn/T/test-42e69a.o -x c++ test.cc
clang -cc1 version 7.0.0 based upon LLVM 3.7.0svn default target x86_64-apple-darwin14.5.0
ignoring nonexistent directory "C:/Program Files/Microsoft Visual Studio 10.0/VC/include"
ignoring nonexistent directory "C:/Program Files/Microsoft Visual Studio 9.0/VC/include"
ignoring nonexistent directory "C:/Program Files/Microsoft Visual Studio 9.0/VC/PlatformSDK/Include"
ignoring nonexistent directory "C:/Program Files/Microsoft Visual Studio 8/VC/include"
ignoring nonexistent directory "C:/Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include"
#include "..." search starts here:
#include <...> search starts here:
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/7.0.0/include
End of search list.
test.cc:1:10: fatal error: 'string' file not found
#include <string>
         ^
1 error generated.

回答1:

clang is an excellent cross compiler but as you rightly point out, a cross compiler is of little use unless you have the rest of the stuff needed. At minimum you'll need

  • header files for the target libraries
  • libraries that have been compiled for the target
  • a linker capable of linking your object files and the libraries into an executable

There are several places that you can find all of these things. If you'd like a set of tools pre-packaged, you could take a look at ELLCC. ELLCC is a cross compilation tool chain based on clang. It includes

  • ecc - the clang based compiler for ARM, Mips, PowerPC, and x86
  • Header files and pre-compiled C and C++ standard libraries for Linux and Windows
  • A set of binutils (assemblers, linker, etc.) configured to handle all the targets

This allows you to do exactly what you want:

% ecc -target x86_64-w64-mingw32 test.cc -o test.exe
% file test.exe
test.exe: MS-DOS executable PE  for MS Windows (console), Mono/.Net assembly
% ecc -target aarch64-linux-eng test.cc -o test
% file test
test: ELF 64-bit LSB executable, version 1 (SYSV), statically linked, not stripped
%

Follow the download link to get pre-compiled tools that run on Linux, Windows, or Mac OS X.