pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

Clang C++ 交叉編譯器 - 從 Mac OS X 生成 Windows 可執行

Clang C++ Cross Compiler - Generating Windows Executable from Mac OS X(Clang C++ 交叉編譯器 - 從 Mac OS X 生成 Windows 可執行文件)
本文介紹了Clang C++ 交叉編譯器 - 從 Mac OS X 生成 Windows 可執行文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我使用 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How do I set the icon for my application in visual studio 2008?(如何在 Visual Studio 2008 中為我的應用程序設置圖標?)
Convert CString to const char*(將 CString 轉換為 const char*)
Remove secure warnings (_CRT_SECURE_NO_WARNINGS) from projects by default in Visual Studio(默認情況下,在 Visual Studio 中從項目中刪除安全警告 (_CRT_SECURE_NO_WARNINGS))
How do I start a new CUDA project in Visual Studio 2008?(如何在 Visual Studio 2008 中啟動新的 CUDA 項目?)
Exporting classes containing `std::` objects (vector, map etc.) from a DLL(從 DLL 導出包含 `std::` 對象(向量、映射等)的類)
What are some reasons a Release build would run differently than a Debug build(發布版本與調試版本的運行方式不同的一些原因是什么)
主站蜘蛛池模板: 苏州注册公司_苏州代理记账_苏州工商注册_苏州代办公司-恒佳财税 | 北京律师事务所_房屋拆迁律师_24小时免费法律咨询_云合专业律师网 | 智能案卷柜_卷宗柜_钥匙柜_文件流转柜_装备柜_浙江福源智能科技有限公司 | 单机除尘器 骨架-脉冲除尘器设备生产厂家-润天环保设备 | 北钻固控设备|石油钻采设备-石油固控设备厂家 | 移动机器人产业联盟官网 | 碳化硅,氮化硅,冰晶石,绢云母,氟化铝,白刚玉,棕刚玉,石墨,铝粉,铁粉,金属硅粉,金属铝粉,氧化铝粉,硅微粉,蓝晶石,红柱石,莫来石,粉煤灰,三聚磷酸钠,六偏磷酸钠,硫酸镁-皓泉新材料 | 悬浮拼装地板_幼儿园_篮球场_悬浮拼接地板-山东悬浮拼装地板厂家 | 垃圾清运公司_环卫保洁公司_市政道路保洁公司-华富环境 | 紧急切断阀_气动切断阀_不锈钢阀门_截止阀_球阀_蝶阀_闸阀-上海上兆阀门制造有限公司 | 合肥卓创建筑装饰,专业办公室装饰、商业空间装修与设计。 | 花纹铝板,合金铝卷板,阴极铝板-济南恒诚铝业有限公司 | 液压油缸生产厂家-山东液压站-济南捷兴液压机电设备有限公司 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 贵州成人高考网_贵州成考网| 巨野电机维修-水泵维修-巨野县飞宇机电维修有限公司 | LED太阳能中国结|发光红灯笼|灯杆造型灯|节日灯|太阳能灯笼|LED路灯杆装饰造型灯-北京中海轩光电 | 北京印刷厂_北京印刷_北京印刷公司_北京印刷厂家_北京东爵盛世印刷有限公司 | BESWICK球阀,BESWICK接头,BURKERT膜片阀,美国SEL继电器-东莞市广联自动化科技有限公司 | 绿萝净除甲醛|深圳除甲醛公司|测甲醛怎么收费|培训机构|电影院|办公室|车内|室内除甲醛案例|原理|方法|价格立马咨询 | 自动螺旋上料机厂家价格-斗式提升机定制-螺杆绞龙输送机-杰凯上料机 | 上海质量认证办理中心 | 国产离子色谱仪,红外分光测油仪,自动烟尘烟气测试仪-青岛埃仑通用科技有限公司 | HV全空气系统_杭州暖通公司—杭州斯培尔冷暖设备有限公司 | 天津散热器_天津暖气片_天津安尼威尔散热器制造有限公司 | 粉末冶金注射成型厂家|MIM厂家|粉末冶金齿轮|MIM零件-深圳市新泰兴精密科技 | 开云(中国)Kaiyun·官方网站-登录入口 | 重庆波纹管|重庆钢带管|重庆塑钢管|重庆联进管道有限公司 | 蒸压釜-陶粒板隔墙板蒸压釜-山东鑫泰鑫智能装备有限公司 | 高楼航空障碍灯厂家哪家好_航空障碍灯厂家_广州北斗星障碍灯有限公司 | 全国国际化学校_国际高中招生_一站式升学择校服务-国际学校网 | 餐饮加盟网_特色餐饮连锁加盟店-餐饮加盟官网 | 吸音板,隔音板,吸音材料,吸音板价格,声学材料 - 佛山诺声吸音板厂家 | 百方网-百方电气网,电工电气行业专业的B2B电子商务平台 | 奥因-光触媒除甲醛公司-除甲醛加盟公司十大品牌 | 超声波清洗机_超声波清洗机设备_超声波清洗机厂家_鼎泰恒胜 | 山东led显示屏,山东led全彩显示屏,山东LED小间距屏,临沂全彩电子屏-山东亚泰视讯传媒有限公司 | 上海赞永| 合肥风管加工厂-安徽螺旋/不锈钢风管-通风管道加工厂家-安徽风之范 | 棕刚玉_白刚玉_铝酸钙-锐石新材料 | WTB5光栅尺-JIE WILL磁栅尺-B60数显表-常州中崴机电科技有限公司 |