Cross-platform build under Windows targeting Linux using CMake

12,031

Solution 1

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.

Solution 2

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.

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:

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.

Share:
12,031
Setareh
Author by

Setareh

Updated on July 20, 2022

Comments

  • Setareh
    Setareh almost 2 years

    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.

    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.

    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.

    Using command file executablename, I realized the executable which is made by the above procedure is of type PE32 which is only for 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?

    Many thanks

    Setareh

  • Setareh
    Setareh almost 11 years
    @Stjin and SethMMorton, Thank you for your comments and the link. But the metamod website has apparently changed its binary distribution, now I only get a metamod.dll file from there, and I don't have any idea how I can install the Linux cross compilers in Cygwin with that. Any ideas?
  • Setareh
    Setareh almost 11 years
    Thanks a lot for your answer. Now I understand my options.
  • Martin Braun
    Martin Braun over 2 years
    Nowadays you would use WSL for that.