Search code examples
c++visual-studio-codecmakecross-compiling

How can I use VSCode with CMake and have different targets each with a different architecture and toolchain?


I'm working on a project with Visual Studio Code and CMake where I'm compiling / cross-compiling different targets for different platforms. Currently the project has three platforms, each of which uses a different toolchain. I'm going to simplify the names of the platforms and toolchains for the purposes of explaining:

  • Platform1 uses a toolchain defined in platform1.cmake and builds from platform1/CMakeLists.txt
  • Platform2_native uses a toolchain defined in platform2.cmake and builds from platform2/CMakeLists.txt
  • Platform2_emulated uses the system's native toolchain, and also builds from platform2/CMakeLists.txt

If I individually configure and build each of these using the command line and manually specify the correct toolchain, then they all build correctly. But what I really want to do is be able to do the building in Visual Studio Code, and specify which target I'm building.

However, I'm still new to CMake and I'm hitting a brick wall as I try to figure out how to set this up. From what I'm reading, CMake assumes that all targets will use the same toolchain, and it looks like Visual Studio Code only works with one CMake project at a time.

Is there a way to do this in a single Visual Studio Code workspace?


Solution

  • From what I'm reading, CMake assumes that all targets will use the same toolchain

    Yes. All targets in the same CMake-configured buildsystem. Boundaries like ExternalProject apply. See https://stackoverflow.com/a/76996558/11107541

    Visual Studio Code only works with one CMake project at a time

    CMake Tools works with one top-level project at a time.

    Is there a way to do this in a single Visual Studio Code workspace?

    Presets will help. In this case, I'd define a configure preset for each of those three things (see the architecture, toolset, and toolchainFile members). If there wasn't the Platform2 native vs emulation separation, then you could alternatively set this up as multiple source directories (see the cmake.sourceDirectory setting and https://stackoverflow.com/a/73698237/11107541 and https://stackoverflow.com/a/77657422/11107541).