44

Very new to CMake, and so far I'm finding it to be extremely helpful. I have a set of custom libraries that I would like to build for multiple platforms using cross-compilation. The toolchains are installed, and I can hand-create the Makefiles that I need to do so, but I would like to be able to make use of CMake.

Is there a way to tell cmake which toolchain to use, either at the command line or in the CMakeLists.txt file?

Will
  • 3,500
  • 4
  • 30
  • 38

2 Answers2

53

Have a look here: basically, you define a "toolchain file" that sets things like the system name, paths to compilers and so on. You then call cmake like so:

cmake /path/to/src -DCMAKE_TOOLCHAIN_FILE=/path/to/toolchain/foo-bar-baz.cmake
Mike T
  • 41,085
  • 18
  • 152
  • 203
Jack Kelly
  • 18,264
  • 2
  • 56
  • 81
  • 1
    Looks like what I was looking for; will give that a try. – Will Feb 24 '11 at 14:22
  • 1
    worked perfectly, thanks. this will save me a lot of headaches as I start migrating my source trees to use `cmake` instead of my hand-crafted Makefiles. – Will Feb 24 '11 at 21:34
  • 4
    Currently I am in the process of setting up a project, which will be developed exclusively with the Emscripten SDK. Is there a way to specify the toolchain file inside the CMakeLists.txt-file? –  Mar 07 '18 at 15:39
  • 3
    Where are the toolchain files that correspond to CMake's out-of-the-box options? – detly May 30 '18 at 00:59
  • 31
    is there any way to configure cmake to use a specific toolchain file from the project configuration file (i.e., avoid specifying `-DCMAKE_TOOLCHAIN_FILE` every time i need to configure a build?) – pqnet Feb 13 '19 at 16:23
  • CMake Warning: Manually-specified variables were not used by the project: CMAKE_TOOLCHAIN_FILE – thang Mar 01 '19 at 02:20
  • 1
    @thang Was that in a clean build directory (e.g rm -rf build; mkdir build; cd build; cmake ...)? The error you saw could mean you need to start from scratch. If it is not from this cause perhaps you have started your own question? – cardiff space man Mar 11 '19 at 07:26
  • 2
    @cardiffspaceman is correct. Cleaning the project fixed the issue for me. I also had to do this: `set(CMAKE_TOOLCHAIN_FILE $ENV{CMAKE_TOOLCHAIN_FILE})` – Evan Moran Jul 21 '19 at 21:41
  • I found that while the path-to-src can be a relative path (i.e. `..`), the `/path/to/toolchain` must be an absolute path. – user550701 Sep 16 '21 at 20:06
  • using the same approach cmake /path/to/src -DCMAKE_TOOLCHAIN_FILE=${NDK_HOME}/build/cmake/android.toolchain.cmake it generates a binary file, but how can i launch or run it in an andorid device or simulator ? – Ashkanxy Nov 29 '22 at 08:28
3

To customize the build settings so you don't have to specify the parameter every time:

For Visual Studio - here

For Vusual Studio Code :

Install the Cmake Tools extension if haven't done so already. In the .vscode/settings.json file set the parameter cmake.configureArgs. You can also set it from Settings -> CMake Tools configuration -> Add Item. Mine looks like this:

{
    "cmake.configureArgs": [
        "-DCMAKE_TOOLCHAIN_FILE=C:/Users/.../vcpkg/scripts/buildsystems/vcpkg.cmake"
    ]
}
Tsenko Tsenkov
  • 101
  • 1
  • 2