Clang C ++ Cross Compiler - 从Mac OS X生成Windows可执行文件 [英] Clang C++ Cross Compiler - Generating Windows Executable from Mac OS X

查看:1122
本文介绍了Clang C ++ Cross Compiler - 从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:warning:编译期间未使用的参数:'-triple x86-pc-win32'

解决方案

Clang原则上可以用作交叉编译器:与大多数编译器不同,clang / LLVM在同一个二进制文件中包含不同平台的组件(如codegen,assembler和linker)。



但是,您会遇到一些问题,试图在生产能力中使用它:




  • 您需要平台库和标头。要生成将在Windows上工作的可执行文件,您需要Windows标题和要链接的Windows库,如果您动态链接或导入静态链接的静态库,则导入dll。


  • 许多C ++功能(例如名称管理和RTTI支持)在Windows上不完整。在Windows上使用Clang来编译Windows时会遇到同样的问题。


  • LLVM项目包含lld链接器,在x86上的自主机Windows等因此可能为您工作作为跨平台链接器,但lld不是clang发行版的标准部分。 Clang在OS X上仍然使用OS X平台链接器 ld 默认情况下,Windows上的Clang( link.exe ) 。您需要获取lld,并找出如何链接它,或找到一些其他跨平台链接器。


  • clang驱动程序没有写作为跨平台编译器驱动程序。你可能需要做更多的实际工作来运行跨平台编译。看看 clang - ### 的输出:clang驱动程序为您构建该命令,但您可能需要做与clang驱动程序相同的工作用手。因为clang在跨平台编译中得到的测试少得多,你可能会遇到更多的错误。


  • Xcode不会帮助你这个。它可以配置clang为OS X或iOS构建,但是你必须手动配置到Windows的跨平台构建。




我相信我们可以将基于LLVM的环境拼凑在一起,在OS X或Linux上构建一个CHello,WorldWindows exe,但它还没有准备好让Xcode添加一个Windows可能的目标平台列表。






如果你不是一个编译器开发者,源代码到Windows机器并使用Visual Studio构建。如果你是,或想成为一个编译器开发人员,那么通过一切手段,帮助推动Clang的交叉编译能力向前。我认为Clang的通用驱动程序项目是令人兴奋的,我真的希望看到进展继续。 p>




我已经成功地完成了相反的交叉编译:在Windows上编译Mac OS X可执行文件。在一个小程序上,即直接编译一个.cpp文件,这是很容易做到的。



首先,Mac OS X开发工具附带了SDKs其中包含特定操作系统的所有系统库和标头。这里最大的挑战是如何将SDK转移到Windows,同时保留SDK中的所有符号链接。 (由于某些原因在Windows上创建符号链接需要提升的权限,所以在OS X上使用符号链接生成tar.gz后,我不得不在Windows上以管理员身份运行7zip,以正确扩展归档。)



一旦SDK在Windows上可用,就有一个标志告诉clang在哪里获得所有的系统依赖: -isysroot 。这与 -target 标志的结合是我需要告诉ang如何为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 dlls 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.

  • 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 ++ Cross Compiler - 从Mac OS X生成Windows可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆