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

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

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

問題描述

我使用 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)!

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

相關(guān)文檔推薦

How do I set the icon for my application in visual studio 2008?(如何在 Visual Studio 2008 中為我的應(yīng)用程序設(shè)置圖標(biāo)?)
Convert CString to const char*(將 CString 轉(zhuǎn)換為 const char*)
Remove secure warnings (_CRT_SECURE_NO_WARNINGS) from projects by default in Visual Studio(默認(rèn)情況下,在 Visual Studio 中從項(xiàng)目中刪除安全警告 (_CRT_SECURE_NO_WARNINGS))
How do I start a new CUDA project in Visual Studio 2008?(如何在 Visual Studio 2008 中啟動(dòng)新的 CUDA 項(xiàng)目?)
Exporting classes containing `std::` objects (vector, map etc.) from a DLL(從 DLL 導(dǎo)出包含 `std::` 對(duì)象(向量、映射等)的類)
What are some reasons a Release build would run differently than a Debug build(發(fā)布版本與調(diào)試版本的運(yùn)行方式不同的一些原因是什么)
主站蜘蛛池模板: 【灵硕展览集团】展台展会设计_展览会展台搭建_展览展示设计一站式服务公司 | 万濠投影仪_瑞士TRIMOS高度仪_尼康投影仪V12BDC|量子仪器 | 钛合金标准件-钛合金螺丝-钛管件-钛合金棒-钛合金板-钛合金锻件-宝鸡远航钛业有限公司 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | 杭州双螺杆挤出机-百科 | 六维力传感器_六分量力传感器_模腔压力传感器-南京数智微传感科技有限公司 | 今日扫码_溯源二维码_产品防伪一物一码_红包墙营销方案 | 铝单板_铝窗花_铝单板厂家_氟碳包柱铝单板批发价格-佛山科阳金属 | 岩棉板|岩棉复合板|聚氨酯夹芯板|岩棉夹芯板|彩钢夹芯板-江苏恒海钢结构 | 济南办公室装修-厂房装修-商铺装修-工装公司-山东鲁工装饰设计 | 阻垢剂,反渗透阻垢剂,缓蚀阻垢剂-山东普尼奥水处理科技有限公司 真空粉体取样阀,电动楔式闸阀,电动针型阀-耐苛尔(上海)自动化仪表有限公司 | 不锈钢轴流风机,不锈钢电机-许昌光维防爆电机有限公司(原许昌光维特种电机技术有限公司) | 干粉砂浆设备-干粉砂浆生产线-干混-石膏-保温砂浆设备生产线-腻子粉设备厂家-国恒机械 | 杭州实验室尾气处理_实验台_实验室家具_杭州秋叶实验设备有限公司 | 比亚迪叉车-比亚迪电动叉车堆垛车托盘车仓储叉车价格多少钱报价 磁力去毛刺机_去毛刺磁力抛光机_磁力光饰机_磁力滚抛机_精密金属零件去毛刺机厂家-冠古科技 | 冷油器-冷油器换管改造-连云港灵动列管式冷油器生产厂家 | 一体化净水器_一体化净水设备_一体化水处理设备-江苏旭浩鑫环保科技有限公司 | 胶泥瓷砖胶,轻质粉刷石膏,嵌缝石膏厂家,腻子粉批发,永康家德兴,永康市家德兴建材厂 | 高光谱相机-近红外高光谱相机厂家-高光谱成像仪-SINESPEC 赛斯拜克 | 无纺布包装机|径向缠绕包装机|缠绕膜打包机-上海晏陵智能设备有限公司 | 电力电子产业网| 防爆大气采样器-防爆粉尘采样器-金属粉尘及其化合物采样器-首页|盐城银河科技有限公司 | 上海公众号开发-公众号代运营公司-做公众号的公司企业服务商-咏熠软件 | UV固化机_UVLED光固化机_UV干燥机生产厂家-上海冠顶公司专业生产UV固化机设备 | 楼梯定制_楼梯设计施工厂家_楼梯扶手安装制作-北京凌步楼梯 | 沈阳液压泵_沈阳液压阀_沈阳液压站-沈阳海德太科液压设备有限公司 | 照相馆预约系统,微信公众号摄影门店系统,影楼管理软件-盟百网络 | 英超直播_英超免费在线高清直播_英超视频在线观看无插件-24直播网 | 北京西风东韵品牌与包装设计公司,创造视觉销售力! | 恒温恒湿试验箱厂家-高低温试验箱维修价格_东莞环仪仪器_东莞环仪仪器 | 车间除尘设备,VOCs废气处理,工业涂装流水线,伸缩式喷漆房,自动喷砂房,沸石转轮浓缩吸附,机器人喷粉线-山东创杰智慧 | 青岛美佳乐清洁工程有限公司|青岛油烟管道清洗|酒店|企事业单位|学校工厂厨房|青岛油烟管道清洗 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 南京交通事故律师-专打交通事故的南京律师 | 京马网,京马建站,网站定制,营销型网站建设,东莞建站,东莞网站建设-首页-京马网 | SDI车窗夹力测试仪-KEMKRAFT方向盘测试仪-上海爱泽工业设备有限公司 | 英思科GTD-3000EX(美国英思科气体检测仪MX4MX6)百科-北京嘉华众信科技有限公司 | 避光流动池-带盖荧光比色皿-生化流动比色皿-宜兴市晶科光学仪器 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | 高铝砖-高铝耐火球-高铝耐火砖生产厂家-价格【荣盛耐材】 | 青岛空压机,青岛空压机维修/保养,青岛空压机销售/出租公司,青岛空压机厂家电话 | ERP企业管理系统永久免费版_在线ERP系统_OA办公_云版软件官网 | 火锅加盟_四川成都火锅店加盟_中国火锅连锁品牌十强_朝天门火锅【官网】 |