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

I would like to create Windows binaries linked to the Visual Studio 2013 runtime using clang for OS X, but it is not immediately obvious how to get clang to generate the correct object files and link without MSVC link.exe

. Triple triple report clang -v

on my windows machine - i686-pc-windows-msvc

but this is not accepted by clang on OS X using the parameter -target

.

What you need to cross-compile a simple example:

#include <iostream>

int main() {
    std::cout << "Hello, world!\n";
}

      

+3


source to share


1 answer


This is somewhat experimental. This example can compile and link successfully, but I wouldn't have high hopes of building a large and complex project (like Chromium) this way right now.

Requirements:

  • Installing clang. I used Apple clang (3.5) from Xcode 6.1.
  • A copy of the Visual Studio 2013 headers. A standard installation of Visual Studio 2013 will place this in C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include

    by default. This should also be included in the express edition. ( /path/to/vs2013_include

    below)
  • A copy of the Visual Studio 2013 Libraries. The default Visual Studio 2013 installation will go to C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib

    . This should also be included in the express edition. ( /path/to/vs2013_lib

    below)
  • A copy of the Windows SDK. This can be downloaded from Microsoft (e.g. version 7.1 ). You can find existing installations in C:\Program Files (x86)\Microsoft SDKs\Windows

    . ( /path/to/windows/sdk/vX.X/Lib

    below)
  • String lld

    , llvm linker. It is not yet fully completed and should be built according to the instructions on its website .

Procedure:

Save this file as test.cpp

:

#include <iostream>

int main() {
    std::cout << "Hello, world!\n";
}

      

First, start clang (with the path to where you put the VS2013 headers):



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 start 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 clang call, exceptions and RTTI are disabled. Microsoft compatibility for these features lld

    is not yet complete.
  • I have not been able to successfully complete the linking step on Ubuntu, it triggers an assertion in lld

    . This has only been tested on OS X.
+2


source







All Articles