問題描述
我使用 Clang 編譯器在我的 Mac 上使用 Xcode 創(chuàng)建了一個(gè) C++ 應(yīng)用程序.
我想編譯我的源文件以創(chuàng)建一個(gè)可以在 Windows 機(jī)器上運(yùn)行的可執(zhí)行文件,但是我無法讓 Clang 為我生成一個(gè)可執(zhí)行文件.
這是我嘗試過的:
clang++ -std=c++11 -stdlib=libc++ -arch x86_64 class1.cpp class2.cpp... -o executable.exe
這會(huì)創(chuàng)建一個(gè)可執(zhí)行文件,但是它不會(huì)運(yùn)行(Windows 給我一個(gè)錯(cuò)誤,與應(yīng)用程序是 16 位有關(guān) - 不明白這一點(diǎn) - 不能在 64 位上運(yùn)行)
clang++ -std=c++11 -stdlib=libc++ -target i386-pc-win32 class1.cpp class2.cpp
出于某種原因,每當(dāng)我使用 -target
標(biāo)志時(shí),我都會(huì)收到一條錯(cuò)誤消息,指出編譯器找不到 <iostream>
標(biāo)頭,但在其他任何時(shí)候它都不會(huì)呻吟.
我曾嘗試使用 -Ipath/to/iostreamfolder/
但這并沒有產(chǎn)生任何更好的結(jié)果
任何建議都會(huì)很棒!謝謝!
我也嘗試過 '-triple x86-pc-win32'
標(biāo)志,但是我收到了這個(gè)警告 clang: 警告:編譯期間未使用的參數(shù):'-triple x86-pc-win32'
Clang 原則上可以用作交叉編譯器:與大多數(shù)編譯器不同,clang/LLVM 包含針對(duì)不同平臺(tái)的組件(例如代碼生成器、匯編器和鏈接器)在同一個(gè)二進(jìn)制文件中.
但是,嘗試在生產(chǎn)能力中使用它時(shí)會(huì)遇到許多問題:
您需要平臺(tái)庫和頭文件.要生成可在 Windows 上運(yùn)行的可執(zhí)行文件,您需要要鏈接到的 Windows 標(biāo)頭和 Windows 庫,如果您是動(dòng)態(tài)鏈接,則導(dǎo)入庫或用于靜態(tài)鏈接的靜態(tài)庫.您應(yīng)該可以通過安裝 Visual Studio 獲得這些.
許多 C++ 功能(例如名稱修改和 RTTI 支持)在 Windows 上并不完整.使用 Clang 在 Windows 上編譯 Windows 時(shí)也會(huì)遇到同樣的問題.Windows C++ 支持是 這些天已經(jīng)基本完成了.LLVM 項(xiàng)目包括 lld 鏈接器,它顯然已經(jīng)足夠遠(yuǎn),它可以在 x86 Windows 上自托管,因此可能適合您作為跨平臺(tái)鏈接器,但是 lld 還不是標(biāo)準(zhǔn)的一部分叮當(dāng)分布.默認(rèn)情況下,OS X 上的 Clang 仍然使用 OS X 平臺(tái)鏈接器
ld
,就像 Windows 上的 Clang (link.exe
).您需要獲取 lld 并弄清楚如何與之鏈接,或者找到其他一些跨平臺(tái)鏈接器.clang 驅(qū)動(dòng)程序不是作為跨平臺(tái)編譯器驅(qū)動(dòng)程序編寫的.您可能需要做更多的實(shí)踐工作才能運(yùn)行跨平臺(tái)編譯.看看
clang -###
的輸出:clang 驅(qū)動(dòng)程序?yàn)槟鷺?gòu)造了該命令,但您可能需要手動(dòng)完成與 clang 驅(qū)動(dòng)程序相同的大部分工作.而且由于 clang 在跨平臺(tái)編譯時(shí)得到的測(cè)試要少得多,因此您很可能會(huì)遇到更多錯(cuò)誤.Xcode 不會(huì)幫助您解決這些問題.它可以配置 clang 以針對(duì) OS X 或 iOS 進(jìn)行構(gòu)建,但您必須手動(dòng)將跨平臺(tái)構(gòu)建配置到 Windows.
我相對(duì)有信心,可以拼湊出一個(gè)基于 LLVM 的環(huán)境,在 OS X 或 Linux 上構(gòu)建一個(gè) CHello, World"Windows exe,但是 Xcode 還沒有準(zhǔn)備好將Windows"項(xiàng)添加到可能的目標(biāo)平臺(tái)列表.
<小時(shí)>如果您不是編譯器開發(fā)人員,那么最好將源代碼復(fù)制到 Windows 機(jī)器上并使用 Visual Studio 進(jìn)行構(gòu)建.如果您是或想成為編譯器開發(fā)人員,那么請(qǐng)務(wù)必幫助推動(dòng) Clang 的交叉編譯能力向前發(fā)展.我認(rèn)為 Clang 通用驅(qū)動(dòng)程序 項(xiàng)目令人興奮,我真的很希望看到繼續(xù)取得進(jìn)展.><小時(shí)>
我已經(jīng)成功地完成了相反的交叉編譯:在 Windows 上編譯 Mac OS X 可執(zhí)行文件.事實(shí)證明,在小程序上手動(dòng)執(zhí)行此操作非常容易,即直接編譯 .cpp 文件.
首先,Mac OS X 開發(fā)工具帶有SDK",其中包含特定操作系統(tǒng)的所有系統(tǒng)庫和頭文件.這里最大的挑戰(zhàn)是弄清楚如何將 SDK 傳輸?shù)?Windows,同時(shí)保留 SDK 中的所有符號(hào)鏈接.(出于某種原因,在 Windows 上創(chuàng)建符號(hào)鏈接需要提升權(quán)限,因此在 OS X 上使用符號(hào)鏈接生成 tar.gz 后,我必須以管理員身份在 Windows 上運(yùn)行 7zip 才能正確展開存檔.)
一旦 SDK 在 Windows 上可用,就會(huì)有一個(gè)標(biāo)志告訴 clang 從哪里獲取所有系統(tǒng)依賴項(xiàng):-isysroot
.結(jié)合 -target
標(biāo)志,我需要告訴 clang 如何為 OS X 生成完整的目標(biāo)文件.
對(duì)于鏈接,我手動(dòng)使用了 lld,因?yàn)榫幾g器驅(qū)動(dòng)程序似乎不支持使用與 lld 的交叉鏈接.lld 支持用于確定目標(biāo)系統(tǒng)庫的類似標(biāo)志.
最后一步是簡(jiǎn)單地將生成的可執(zhí)行文件復(fù)制到 OS X 機(jī)器,啟用執(zhí)行權(quán)限(Windows 不支持相同的文件權(quán)限,因此在構(gòu)建時(shí)不會(huì)設(shè)置執(zhí)行位)并運(yùn)行結(jié)果.
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'
Clang can in principle be used as a cross compiler: unlike most compilers clang/LLVM includes components (such as the codegen, assembler, and linker) for different platforms in the same binary.
However you'll run into a number of problems trying to use it as such in a production capacity:
You need platform libraries and headers. To generate an executable that will work on Windows you need Windows headers and Windows libraries you want to link to, either import libs if you're dynamically linking or static libs for static linking. You should be able to get these from an installation of Visual Studio.
Many C++ features such as name mangling and RTTI support are not complete on Windows. You'd have these same problems compiling for Windows on Windows with Clang.Windows C++ support is pretty much complete these days.The LLVM project includes the lld linker, which is apparently far enough along that it can self host on x86 Windows and so might work for you as a cross-platform linker, however lld is not yet a standard part of clang distributions. Clang on OS X still uses the OS X platform linker
ld
by default as does Clang on Windows (link.exe
). You'll need to get lld and figure out how to link with it, or find some other cross-platform linker.The clang driver isn't written as a cross-platform compiler driver. You'll likely have to do a lot more hands-on work to run a cross-platform compilation. Take a look at the output of
clang -###
: the clang driver constructs that command for you, but you may have to do much of the same work as the clang driver by hand. And since clang gets much less testing at cross-platform compilation you're likely to run into more bugs.Xcode is not going to help you with any of this. It can configure clang to build for OS X or iOS, but you'll have to manually configure cross-platform builds to Windows.
I'm relatively confident that one could cobble together an LLVM based environment to build a C "Hello, World" Windows exe on OS X or Linux, but it's not quite ready for Xcode to add a "Windows" item to the list of possible target platforms.
If you're not a compiler developer you're probably best off just copying your source code to a Windows machine and building with Visual Studio. If you are, or want to be, a compiler developer then by all means, help push Clang's cross-compilation abilities forward. I think the Clang universal driver project is exciting and I would really like to see progress continue.
I've successfully done the opposite cross-compilation: compiling a Mac OS X executable on Windows. This turned out to be quite easy to do manually on a small program, i.e. directly compiling a .cpp file.
First, Mac OS X development tools come with "SDKs" which contain all the system libraries and headers for a particular OS. The largest challenge here was figuring out how to transfer the SDK to Windows while preserving all the symbolic links in the SDK. (For some reason creating symbolic links on Windows requires elevated privileges, so after producing a tar.gz on OS X with the symbolic links I had to run 7zip on Windows as an administrator to correctly expand the archive.)
Once the SDK is available on Windows there's a single flag to tell clang where to get all the system dependencies: -isysroot
. This combined with the -target
flag were all that I needed to tell clang how to produce complete object files for OS X.
For linking I manually used lld, as the compiler driver didn't seem support using cross linking with lld. lld supports similar flags for determining the target system libraries.
The final step was simply copying the produced executable to an OS X machine, enabling the execute permission (Windows doesn't support the same file permissions so the execute bit doesn't get set when building) and running the result.
這篇關(guān)于Clang C++ 交叉編譯器 - 從 Mac OS X 生成 Windows 可執(zhí)行文件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!