I would like to generate Windows binaries linked against the Visual Studio 2013 runtime using clang on OS X, but it isn't immediately obvious how to make clang generate the correct object files and link without MSVC's link.exe
. The target triple clang -v
reports on my Windows machine is i686-pc-windows-msvc
but this isn't accepted by clang on OS X using the -target
option.
What does it take to cross-compile a simple example like this:
#include <iostream>
int main() {
std::cout << "Hello, world!\n";
}
This is somewhat experimental. This example can be compiled and linked successfully, but I wouldn't have high hopes for building a large and complicated project (eg Chromium) this way right now.
C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\include
by default. This should also be included with the express edition. ( /path/to/vs2013_include
below) C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\lib
by default. This should also be included with the express edition. ( /path/to/vs2013_lib
below) C:\\Program Files (x86)\\Microsoft SDKs\\Windows
. ( /path/to/windows/sdk/vX.X/Lib
below) lld
, the llvm linker. It is not yet entirely complete and must be built per the instructions on its site . Save this file as test.cpp
:
#include <iostream>
int main() {
std::cout << "Hello, world!\n";
}
First, run clang (with the path to wherever you've put the VS2013 headers substituted):
clang++ -target i386-pc-win32 -isystem /path/to/vs2013_include -std=c++11 -fms-extensions -fms-compatibility -fdelayed-template-parsing -fno-rtti -fno-exceptions -D_HAS_EXCEPTIONS=0 -D_ITERATOR_DEBUG_LEVEL=0 -fmsc-version=1800 -c test.cpp
This should generate test.o
.
Then, run the linker (with the appropriate VS2013 lib and WinSDK directories):
lld -flavor link /libpath:/path/to/vs2013_lib /libpath:/path/to/windows/sdk/vX.X/Lib kernel32.lib user32.lib shell32.lib /subsystem:console libcmt.lib test.o
This should produce test.exe
which can be run on Windows.
lld
is not yet complete. lld
. This has only been tested on OS X.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.