Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang C++ Cross Compiler - Generating Windows Executable from Mac OS X

I have created a C++ application using Xcode on my Mac using the Clang compiler.

I want to compile my source files to create an executable that can be ran on a windows machine however I cant get Clang to generate me an executable.

Here is what I've tried:

clang++ -std=c++11 -stdlib=libc++ -arch x86_64 class1.cpp class2.cpp... -o executable.exe 

This creates an executable however this does not run (Windows gives me an error to do with the application being 16 bit -dont understand this- that cant run on 64 bit)

clang++ -std=c++11 -stdlib=libc++ -target i386-pc-win32 class1.cpp class2.cpp  

For some reason whenever I use the -target flag I get an error stating that the compiler cannot find the <iostream> header however any other time it never moans.
I have tried using -Ipath/to/iostreamfolder/ however this doesnt produce any better results

Any suggestions would be great! Thanks!

I have also tried the '-triple x86-pc-win32' flag however I get this warning clang: warning: argument unused during compilation: '-triple x86-pc-win32'

like image 917
gavlaaaaaaaa Avatar asked Apr 23 '14 15:04

gavlaaaaaaaa


People also ask

Can Clang compile for Windows?

On Windows, you can now compile with Clang, but LLVM doesn't ship with a standard library.

Can Clang cross compile?

On the other hand, Clang/LLVM is natively a cross-compiler, meaning that one set of programs can compile to all targets by setting the -target option.


1 Answers

Here are step-by-step instructions for building a Hello World .exe using llvm/clang on Mac OS X.

Cross-compile Hello World for Windows using Clang/LLVM on Mac OS X

Install llvm with homebrew. This will include the clang and the llvm linker.

brew install llvm 

You'll need access to Visual Studio C++ libraries and headers, which are available through Visual Studio 2017 on a Windows 10 Virtual Machine (VM) or on a Windows 10 Computer. Install Visual Studio on Window, and include the following 'Individual Components' through the Visual Studio Installer:

  • Windows Universal CRT SDK
  • Windows Universal C Runtime
  • Windows 10 SDK (X.X.X.X) for UWP: C++
  • VC++ 2017 vXXX toolset (x86,x64)
  • Visual C++ 2017 Redistributable Update
  • C++/CLI support

Get access to the MSVC libraries and headers from your Mac.

  • (Option 1) Use your Windows VM and create a shared folder between host and guest.
  • (Option 2) Create a remote share on your Windows computer and connect to it from your Mac.
  • (Option 3) Copy the libraries and headers to your Mac, following any licensing terms.

Find the specific directories on your llvm+MSVC install that correspond to the following:

// LLVM: INCLUDES: /usr/local/Cellar/llvm/5.0.0/include  // MSVC: INCLUDES: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\include" LIBS: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.11.25503\lib\x86"  // C Runtime Library (CRT): INCLUDES: "C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\ucrt" LIBS: "C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\ucrt"  // User-Mode Library (UM): INCLUDES: "C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\um" LIBS: "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.15063.0\um\x86"  // 'Shared' includes: INCLUDES: "C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\shared"  // WinRT includes: INCLUDES: "C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\winrt"  // Figure out your MSC 'version', e.g. Visual C++ 2012 (11.0)   -->     MSC_VER=1700 Visual C++ 2013 (12.0)   -->     MSC_VER=1800 Visual C++ 2015 (14.0)   -->     MSC_VER=1900 Visual C++ 2017 (15.0)   -->     MSC_VER=1910 

Create your Hello World src:

// hello.cc  #include <cstdio>  int main(int argc, char* argv[]) {   printf("Hello, World!\n");    return 0; } 

Compile with clang:

clang -target i686-pc-win32 \   -fms-compatibility-version=19 \   -fms-extensions \   -fdelayed-template-parsing \    -fexceptions \   -mthread-model posix \   -fno-threadsafe-statics \   -Wno-msvc-not-found \   -DWIN32 \   -D_WIN32 \   -D_MT \   -D_DLL \   -Xclang -disable-llvm-verifier \   -Xclang '--dependent-lib=msvcrt' \   -Xclang '--dependent-lib=ucrt' \   -Xclang '--dependent-lib=oldnames' \   -Xclang '--dependent-lib=vcruntime' \   -D_CRT_SECURE_NO_WARNINGS \   -D_CRT_NONSTDC_NO_DEPRECATE \   -U__GNUC__ \   -U__gnu_linux__ \   -U__GNUC_MINOR__ \   -U__GNUC_PATCHLEVEL__ \   -U__GNUC_STDC_INLINE__  \   -I/usr/local/Cellar/llvm/5.0.0/include \   -I/c/Program\ Files\ (x86)/Microsoft\ Visual\ Studio/2017/Community/VC/Tools/MSVC/14.11.25503/include \   -I/c/Program\ Files\ (x86)/Windows\ Kits/10/Include/10.0.15063.0/ucrt \   -I/c/Program\ Files\ (x86)/Windows\ Kits/10/Include/10.0.15063.0/shared \   -I/c/Program\ Files\ (x86)/Windows\ Kits/10/Include/10.0.15063.0/winrt \   -c hello.cc -o hello.o 

Link with the lld linker, driven by clang:

clang -fuse-ld=lld -target i686-pc-win32 -Wl,-machine:x86 -fmsc-version=1900 \   -o hello.exe hello.o \   -L/external/code8-cc/cc/msvctoolchain/x86/lib/msvc \   -L/external/code8-cc/cc/msvctoolchain/x86/lib/um \   -L/code8-cc/cc/msvctoolchain/x86/lib/ucrt   -nostdlib -lmsvcrt -Wno-msvc-not-found  

Copy hello.exe to your Windows computer or Windows VM and run in PowerShell:

.\hello.exe 

To build 64-bit versions, change to '-target x86_64-pc-win32', '-Wl,-machine:x64', and link to x64 libraries.

like image 159
Erik MC Avatar answered Oct 25 '22 17:10

Erik MC