使用CMake在Windows下针对Linux进行跨平台构建

时间:2021-09-03 12:08:24

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.

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

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.

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

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.

现在,我想在Win32平台上为Linux平台(GNU / Linux)构建我的代码。我尝试使用CMake结合Cygwin并使用gcc和g ++作为编译器来执行上述过程。它构建了精细的,创建的makefile,当我在Cygwin终端发出“make”时,生成的makefile被“制作”。现在我有了一个我希望可以在Linux平台上运行的可执行文件。但在Linux上我得到错误:bash无法执行二进制文件。

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

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

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?

现在我的问题是:我对使用cmake的跨平台构建过程的理解是否正确?或者我应该在Windows下使用另一个Linux工具链来获取Linux ELF可执行文件?什么工具链出现在你的脑海里会给我我想要的东西?

Many thanks

Setareh

2 个解决方案

#1


4  

You will want to look here: http://www.cmake.org/Wiki/CMake_Cross_Compiling 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.

您将在这里查看:http://www.cmake.org/Wiki/CMake_Cross_Compiling,如果您进行交叉编译。但是,我建议您在Windows机器上安装类似虚拟机的Linux VM,并在Linux上进行天真的构建。它编译速度更快,您不必担心交叉编译。您可以从Linux VM安装Windows磁盘,以便共享相同的源树。 linux VM的编译速度比在windows下运行的gcc快得多。

#2


0  

Your understanding of CMake is correct... it will determine how to create the build system you request (or is default for the platform you are currently on) based on rules in your CMakeLists.txt file. However, this won't necessarily help you compile for linux on a windows machine if you don't have something installed that can target linux.

您对CMake的理解是正确的......它将根据CMakeLists.txt文件中的规则确定如何创建您请求的构建系统(或者是您当前所在平台的默认构建系统)。但是,如果您没有安装可以针对Linux的东西,这不一定会帮助您在Windows机器上编译Linux。

To compile targeting linux, you will need to use a linux compiler. The link posted by @stjin tells you how to install one on cygwin. Then, to set up your CMake build, do this in the terminal:

要编译目标linux,您需要使用linux编译器。 @stjin发布的链接告诉你如何在cygwin上安装一个。然后,要设置CMake构建,请在终端中执行此操作:

CC=gcc-linux CXX=g++-linux cmake . [options]

This will tell CMake to locate the special linux targeted compilers. Hopefuly, after compiling with these compilers you will be able to run on linux.

这将告诉CMake找到特殊的linux目标编译器。 Hopefuly,在使用这些编译器编译后,您将能够在linux上运行。

#1


4  

You will want to look here: http://www.cmake.org/Wiki/CMake_Cross_Compiling 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.

您将在这里查看:http://www.cmake.org/Wiki/CMake_Cross_Compiling,如果您进行交叉编译。但是,我建议您在Windows机器上安装类似虚拟机的Linux VM,并在Linux上进行天真的构建。它编译速度更快,您不必担心交叉编译。您可以从Linux VM安装Windows磁盘,以便共享相同的源树。 linux VM的编译速度比在windows下运行的gcc快得多。

#2


0  

Your understanding of CMake is correct... it will determine how to create the build system you request (or is default for the platform you are currently on) based on rules in your CMakeLists.txt file. However, this won't necessarily help you compile for linux on a windows machine if you don't have something installed that can target linux.

您对CMake的理解是正确的......它将根据CMakeLists.txt文件中的规则确定如何创建您请求的构建系统(或者是您当前所在平台的默认构建系统)。但是,如果您没有安装可以针对Linux的东西,这不一定会帮助您在Windows机器上编译Linux。

To compile targeting linux, you will need to use a linux compiler. The link posted by @stjin tells you how to install one on cygwin. Then, to set up your CMake build, do this in the terminal:

要编译目标linux,您需要使用linux编译器。 @stjin发布的链接告诉你如何在cygwin上安装一个。然后,要设置CMake构建,请在终端中执行此操作:

CC=gcc-linux CXX=g++-linux cmake . [options]

This will tell CMake to locate the special linux targeted compilers. Hopefuly, after compiling with these compilers you will be able to run on linux.

这将告诉CMake找到特殊的linux目标编译器。 Hopefuly,在使用这些编译器编译后,您将能够在linux上运行。