0

I am getting into cross-compiling (raspberry Pi stuff), and although I've been reading a bunch of tutorials, I still don't get it.

Here is a typical toolchain.cmake file, as described in most of these examples:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_SYSROOT /home/devel/rasp-pi-rootfs)
set(CMAKE_STAGING_PREFIX /home/devel/stage)

set(tools /home/devel/gcc-4.7-linaro-rpi-gnueabihf)
set(CMAKE_C_COMPILER ${tools}/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/arm-linux-gnueabihf-g++)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

My first question: why do I even need a toolchain in the first place? Reading some other threads, cross-compilers are just compilers that can target a different platform (e.g. I have a specific x86-64 machine, but I am asking my compiler to generate arm instructions for a different machine with a different set of registers). Can't I just pass -DCMAKE_CXX_COMPILER=/../arm-linux-gnueabihf-g++ and some -march options to CMake and call it a day?

Assuming I really need the toolchain file shown above, why would I need all these CMAKE_FIND_ROOT_PATH_MODE_ commands? I thought the whole point of cross-compiling was that the entire compilation process would take place on my host computer, and I would only need to copy the executable to the target machine. I understand it will need to find some .dlls on the target system (which I assume is what the CMAKE_FIND_ROOT_PATH_MODE_LIBRARY option is for), but why would it need to find headers there?

Touloudou
  • 2,079
  • 1
  • 17
  • 28
  • Most of what the extra instructions do is prevent CMake from picking up headers and libraries from your host system (because they are probably the wrong architecture but definitely **not** under control of the build process). Imagine the fireworks if CMake statically linked your ARM program with a `libz.a` for your host system (probably x86_64 since you're cross-compiling). – Botje Jul 15 '20 at 14:40
  • But what I don't understand is that everything will need to be on my host system, no? the headers/libraries might be special arm versions, but they will still have to be there. You mean that it will prevent CMake from looking at default locations like /usr/lib/, etc? – Touloudou Jul 15 '20 at 14:48
  • 1
    Yes. the CMAKE_SYSROOT is the **only** place where CMake (and your compiler) will look for headers and libraries. – Botje Jul 15 '20 at 14:50
  • These toolchain files look very CMake-2-ishy. I understand how you would want to tie yourself to a given target, but this feels like tying myself to a given host, hence suppressing most of the good things that CMake brings in the first place. What's your take on this? – Touloudou Jul 16 '20 at 06:57
  • Toolchain files are supposed to be generated by whatever cross sdk you install so it is okay that they are fairly rigid. See crosstool-NG for example. – Botje Jul 16 '20 at 07:19
  • so you mean I should not write this toolchain file myself, but instead it should be provided as part of `arm-linux-gnueabihf`? (in my case) – Touloudou Jul 16 '20 at 07:21
  • Ideally, yes. Writing it yourself was a good reflex, it insulates the code you compile from any cross compilation concerns. – Botje Jul 16 '20 at 07:48
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217946/discussion-between-touloudou-and-botje). – Touloudou Jul 16 '20 at 08:08

0 Answers0