简体   繁体   中英

Cross-compiling from OS X to Windows using clang and the Visual Studio 2013 runtime

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.

Requirements:

  • An installation of clang. I used Apple's clang (3.5) from Xcode 6.1.
  • A copy of the Visual Studio 2013 headers. A standard Visual Studio 2013 installation will put this in 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)
  • A copy of the Visual Studio 2013 libraries. A standard Visual Studio 2013 installation will put this in 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)
  • A copy of the Windows SDK. This can be downloaded from Microsoft (for example, version 7.1 ). You can find existing installations in C:\\Program Files (x86)\\Microsoft SDKs\\Windows . ( /path/to/windows/sdk/vX.X/Lib below)
  • A build of lld , the llvm linker. It is not yet entirely complete and must be built per the instructions on its site .

Procedure:

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.

Notes:

  • As you can see in the invocation of clang, exceptions and RTTI are disabled. Microsoft compatibility for these features of lld is not yet complete.
  • I have been unable to execute the linking step successfully on Ubuntu, it triggers an assertion in 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM