Linux-based cross-platform build on Windows using CMake

I develop C ++ software on Windows 32-bit (using MSVC ++), but since I want to be able to use my software on each platform, I decided to use CMake as a build generator.

Therefore, I am still new to CMake. From the CMake tutorials, I understand that in order to cross-compile the codes, an instrument chain must first be installed on the host platform that simulates the target platform. Then, using the appropriate C and C ++ compilers of the target platform provided by this toolchain, CMake will be able to create make files, etc.

Now I want to create my own code for the Linux platform (GNU / Linux) on the Win32 platform. I tried to perform the above procedure using CMake in combination with Cygwin and use gcc and g ++ as compilers. He created beautiful, created makefiles, and when I released "make" on the Cygwin terminal, the generated makefiles were "made". Now I have an executable that I was hoping to run on the Linux platform. But on Linux, I get an error: bash cannot execute the binary.

Using the batch file executablename, I realized that the executable created by the above procedure is of type PE32, which is designed only for Windows.

Now my question is: do I understand the cross-platform build procedure using cmake correctly? Or do I just need to use another Linux toolchain under the windows to get the Linux ELF executable? What tips come to mind that will give me what I want?

Thank you very much

Setareh

+6
source share
2 answers

You want to look here: http://www.cmake.org/Wiki/CMake_Cross_Compiling if you are doing cross-compilation. However, I would suggest that you install the Linux virtual machine as a virtual box on your Windows machine and naively build Linux. It will compile much faster and you don’t have to worry about cross-compiling. You can install a Windows disk from a Linux virtual machine so that you can share the same source tree. Linux VM will compile much faster than gcc running under windows.

+4
source

Your understanding of CMake is correct ... it will determine how to create the build system you request (or by default for the platform you are currently on) based on the rules in your CMakeLists.txt file. However, this does not necessarily help you compile linux on a Windows computer if you do not have an application designed for Linux installed.

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

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

This will tell CMake to find special target Linux compilers. Hopefuly, after compiling with these compilers, you can run on linux.

0
source

Source: https://habr.com/ru/post/946934/


All Articles