問題描述
我使用 Clang 編譯器在我的 Mac 上使用 Xcode 創建了一個 C++ 應用程序.
我想編譯我的源文件以創建一個可以在 Windows 機器上運行的可執行文件,但是我無法讓 Clang 為我生成一個可執行文件.
這是我嘗試過的:
clang++ -std=c++11 -stdlib=libc++ -arch x86_64 class1.cpp class2.cpp... -o executable.exe
這會創建一個可執行文件,但是它不會運行(Windows 給我一個錯誤,與應用程序是 16 位有關 - 不明白這一點 - 不能在 64 位上運行)
clang++ -std=c++11 -stdlib=libc++ -target i386-pc-win32 class1.cpp class2.cpp
出于某種原因,每當我使用 -target
標志時,我都會收到一條錯誤消息,指出編譯器找不到 <iostream>
標頭,但在其他任何時候它都不會呻吟.
我曾嘗試使用 -Ipath/to/iostreamfolder/
但這并沒有產生任何更好的結果
任何建議都會很棒!謝謝!
我也嘗試過 '-triple x86-pc-win32'
標志,但是我收到了這個警告 clang: 警告:編譯期間未使用的參數:'-triple x86-pc-win32'
Clang 原則上可以用作交叉編譯器:與大多數編譯器不同,clang/LLVM 包含針對不同平臺的組件(例如代碼生成器、匯編器和鏈接器)在同一個二進制文件中.
但是,嘗試在生產能力中使用它時會遇到許多問題:
您需要平臺庫和頭文件.要生成可在 Windows 上運行的可執行文件,您需要要鏈接到的 Windows 標頭和 Windows 庫,如果您是動態鏈接,則導入庫或用于靜態鏈接的靜態庫.您應該可以通過安裝 Visual Studio 獲得這些.
許多 C++ 功能(例如名稱修改和 RTTI 支持)在 Windows 上并不完整.使用 Clang 在 Windows 上編譯 Windows 時也會遇到同樣的問題.Windows C++ 支持是 這些天已經基本完成了.LLVM 項目包括 lld 鏈接器,它顯然已經足夠遠,它可以在 x86 Windows 上自托管,因此可能適合您作為跨平臺鏈接器,但是 lld 還不是標準的一部分叮當分布.默認情況下,OS X 上的 Clang 仍然使用 OS X 平臺鏈接器
ld
,就像 Windows 上的 Clang (link.exe
).您需要獲取 lld 并弄清楚如何與之鏈接,或者找到其他一些跨平臺鏈接器.clang 驅動程序不是作為跨平臺編譯器驅動程序編寫的.您可能需要做更多的實踐工作才能運行跨平臺編譯.看看
clang -###
的輸出:clang 驅動程序為您構造了該命令,但您可能需要手動完成與 clang 驅動程序相同的大部分工作.而且由于 clang 在跨平臺編譯時得到的測試要少得多,因此您很可能會遇到更多錯誤.Xcode 不會幫助您解決這些問題.它可以配置 clang 以針對 OS X 或 iOS 進行構建,但您必須手動將跨平臺構建配置到 Windows.
我相對有信心,可以拼湊出一個基于 LLVM 的環境,在 OS X 或 Linux 上構建一個 CHello, World"Windows exe,但是 Xcode 還沒有準備好將Windows"項添加到可能的目標平臺列表.
<小時>如果您不是編譯器開發人員,那么最好將源代碼復制到 Windows 機器上并使用 Visual Studio 進行構建.如果您是或想成為編譯器開發人員,那么請務必幫助推動 Clang 的交叉編譯能力向前發展.我認為 Clang 通用驅動程序 項目令人興奮,我真的很希望看到繼續取得進展.><小時>
我已經成功地完成了相反的交叉編譯:在 Windows 上編譯 Mac OS X 可執行文件.事實證明,在小程序上手動執行此操作非常容易,即直接編譯 .cpp 文件.
首先,Mac OS X 開發工具帶有SDK",其中包含特定操作系統的所有系統庫和頭文件.這里最大的挑戰是弄清楚如何將 SDK 傳輸到 Windows,同時保留 SDK 中的所有符號鏈接.(出于某種原因,在 Windows 上創建符號鏈接需要提升權限,因此在 OS X 上使用符號鏈接生成 tar.gz 后,我必須以管理員身份在 Windows 上運行 7zip 才能正確展開存檔.)
一旦 SDK 在 Windows 上可用,就會有一個標志告訴 clang 從哪里獲取所有系統依賴項:-isysroot
.結合 -target
標志,我需要告訴 clang 如何為 OS X 生成完整的目標文件.
對于鏈接,我手動使用了 lld,因為編譯器驅動程序似乎不支持使用與 lld 的交叉鏈接.lld 支持用于確定目標系統庫的類似標志.
最后一步是簡單地將生成的可執行文件復制到 OS X 機器,啟用執行權限(Windows 不支持相同的文件權限,因此在構建時不會設置執行位)并運行結果.
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.
這篇關于Clang C++ 交叉編譯器 - 從 Mac OS X 生成 Windows 可執行文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!