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

前端 未结 3 1160
刺人心
刺人心 2020-12-01 01:48

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 wind

3条回答
  •  不思量自难忘°
    2020-12-01 02:19

    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 
    
    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.

提交回复
热议问题