使用CMake在Windows下针对Linux进行跨平台构建 [英] Cross-platform build under Windows targeting Linux using CMake

查看:91
本文介绍了使用CMake在Windows下针对Linux进行跨平台构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在32位Windows(使用MSVC ++)上使用C ++开发软件,但是由于我希望能够在每个平台上使用我的软件,因此我决定使用CMake作为构建生成器.

I am developing a software in C++ on windows 32-bit (using MSVC++), but since I want to be able to use my software on every platform, I have decided to use CMake as my build generator.

因此,我仍然只是CMake的初学者.从CMake教程中,我了解到为了交叉编译代码,首先应在主机平台上安装模拟目标平台的工具链.然后,使用此工具链提供的适当的目标平台C和C ++编译器,CMake将能够生成makefile等.

Therefore, I am still just a beginner in CMake. From the CMake tutorials, I understand that in order to cross compile codes, first a toolchain simulating the target platform should be installed on the host platform. Then using the appropriate target-platform C and C++ compilers provided by this toolchain, CMake would be able to generate makefiles etc.

现在,我想在Win32平台上为Linux平台(GNU/Linux)构建代码.我尝试使用CMake与Cygwin结合使用上述过程,并使用gcc和g ++作为编译器.它构建良好,创建了makefile,当我在Cygwin终端中发布"make"时,生成的makefile是"make"的.现在,我有一个可执行文件,希望可以在Linux平台上运行.但是在Linux上,我收到错误消息:bash无法执行二进制文件.

Now, I want to build my code for Linux platform(GNU/Linux) on a Win32 platform. I tried doing the above procedure using CMake combined with Cygwin and using gcc and g++ as compilers. It built fine, created makefiles, and when I issued "make" in Cygwin terminal, the generated makefiles were "made". Now I have got an executable which I was hoping would run on Linux platform. But on Linux I get the error: bash cannot execute binary file.

使用命令文件的可执行文件名,我意识到由上述过程生成的可执行文件的类型为PE32,仅适用于Windows.

Using command file executablename, I realized the executable which is made by the above procedure is of type PE32 which is only for Windows.

现在我的问题是:我对使用cmake的跨平台构建过程的理解正确吗?还是应该只在Windows下使用另一个Linux工具链来获取Linux ELF可执行文件?您会想到哪些工具链会给我我想要的东西?

Now my question is: Is my understanding of cross-platform build procedure using cmake correct?Or should I just use another Linux toolchain under windows to get a Linux ELF executable? What toolchains come to your mind which would give me what I want?

非常感谢

Setareh

推荐答案

您将要在这里查看:

You will want to look here: cmake-toolchains(7) if you do cross compiling. However, I would suggest that you install a Linux VM like virtual box on your windows machine and build naively on Linux. It will compile much faster and you will not have to worry about cross compiling. You can mount the windows disk from the linux VM so you can share the same source tree. The linux VM will compile much faster than gcc running under windows.

这篇关于使用CMake在Windows下针对Linux进行跨平台构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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