The simple example above shows how we can create a toolchain file and tell CMake to use that toolchain file to drive the build. Cross platform builds where the user specifies a toolchain file.Normal builds where CMake is responsible for choosing the toolchain.There are two main scenarios of using CMake for builds: It uses a set of utilities called a toolchain to drive the build. Different toolchain files and compilers should allow the same CMakeLists.txt target to be created for multiple platforms.Ĭross Platform Software Development Using CMakeĬMake is a great tool for cross platform software development. What have we achieved? We have created an executable for Windows entirely working under Linux. This should create a HelloWorld.exe as specified in the CMakeLists.txt in the build/bin folder. DCMAKE_TOOLCHAIN_FILE=./cross-compilation.cmake cmake -build. Create a new folder called build and change to that directory. Save this file as cross-compilation.cmake and keep it in the same folder as CMakeLists.txt (given above) and Main.cpp. # search programs in the host environment set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) # adjust the default behavior of the find commands: # search headers and libraries in the target environment set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) # which compilers to use set(CMAKE_C_COMPILER i686-w64-mingw32-gcc) set(CMAKE_CXX_COMPILER i686-w64-mingw32-g++) # the name of the target operating system set(CMAKE_SYSTEM_NAME Windows) Let us create a toolchain file for CMake that tells some information about the cross-compilation toolchain. # define executable and its source file add_executable(HelloWorld main.cpp) #include int main(int argc, char** argv) ) Let us try to create a simple “Hello, World!” C++ program: We are interested in the Cross Tools compiler. Microsoft Visual Studio comes with the following command tools some of which are used for cross compilation: Programs compiled for Intel CPUs are not compatible with ARM CPUs and vice-versa. Windows 10 for ARM CPUs (Both ARM32 and ARM64 bit versions).Windows 10 for Intel CPUs (Both 32 Bit and 64 Bit versions).Windows 10 desktop operating system comes in two different flavors: I Have Never Attempted a Cross Compilation. Rather, docker images are setup that brings up a Linux container that hosts GNU GCC to cross compile the source. Because of the multiple dependencies, it is not advisable to attempt the build of Chromium natively. For example, take an open-source project like Chromium (base for both Microsoft Edge browser and Google Chrome browser) that is supported on multiple platforms. It is much easier to set up the cross-compilation environment on a powerful development machine and copy the binaries to Raspberry Pi than spending too much time running the compilation on such a low power machine.Īnother use case for cross compilation is the complexity of getting dependencies to build using the target machine’s native compiler. The same would be the situation if the developer is targeting a platform like Raspberry Pi. He would use the computational power of his development machine (Windows, Linux, or Mac) to cross compile the app for Android. Take for example an app that a developer is planning for Android. Can I Not Live With a Simple Compilation? This is where frameworks like Qt and standards like POSIX come into play. Source code written for windows cannot normally be compiled for Linux and vice versa. It should now be obvious where the term cross compilation comes from… Cross platform software development is not easy as each operating system has its idiosyncrasies. So cross compilation happens when the source code compiler is targeting a different operating system than the one it is currently hosted in. The source code of Microsoft Paint exists as a set of (no doubt C++) files that get compiled within Microsoft windows using (no doubt Microsoft Visual Studio) compiler that begets Microsoft Paint. Compiling the source code and linking object codes to a single executable is the job of a compiler. Before it becomes an executable, it exists as a set of source code files. Think of a well-known application like Microsoft Paint. Analogously we have CMake to make things simpler in the cross-compilation world. Similar is the difficulty in getting a C++ project cross-compiled. The parents among us would relate to the amount of nurturing and care required to bring up a child. Wondering what the Pied Piper image is doing here? Well, just imagine the Pied Piper as CMake and the C++ based projects as Children.
0 Comments
Leave a Reply. |