Search code examples
c++cmakemingwvcpkg

Cmake can't recognize Mingw Makefiles only when i'm using vcpkg


After following vcpkg tutorial with cmake. Cmake couldn't recognize "MinGW Makefiles" with vcpkg.

Some information about my system

I'm using windows x64 bit, and downloaded mingw from winlibs, and added it to the "PATH", and cmake (version 3.31.3) downloaded form the official site as a zip file, also added it to the "PATH", and vcpkg with the help of the tutorial adding "VCPKG_ROOT" and it's path as an environment variable, and once again in the "PATH".

helloworld dir:

  • vcpkg.json:
{
  "dependencies": [
    "fmt"
  ]
}
  • Cmakelists.txt:
cmake_minimum_required(VERSION 3.10)

project(HelloWorld)

find_package(fmt CONFIG REQUIRED)

add_executable(HelloWorld helloworld.cpp)

target_link_libraries(HelloWorld PRIVATE fmt::fmt)
  • vcpkg-configuration.json:
{
  "default-registry": {
    "kind": "git",
    "baseline": "80d54ff62d528339c626a6fbc3489a7f25956ade",
    "repository": "https://github.com/microsoft/vcpkg"
  },
  "registries": [
    {
      "kind": "artifact",
      "location": "https://github.com/microsoft/vcpkg-ce-catalog/archive/refs/heads/main.zip",
      "name": "microsoft"
    }
  ]
}
  • helloworld.cpp:
#include <fmt/core.h>

int main()
{
    fmt::print("Hello World!\n");
    return 0;
}
  • CMakePresets.json:
{
  "version": 2,
  "configurePresets": [
    {
      "name": "vcpkg",
      "generator": "MinGW Makefiles",
      "binaryDir": "${sourceDir}/build",
      "cacheVariables": {
        "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
      }
    }
  ]
}
  • CMakeUserPresets.json:
{
    "version": 2,
    "configurePresets": [
      {
        "name": "default",
        "inherits": "vcpkg",
        "environment": {
          "VCPKG_ROOT": "C:/vcpkg"
        }
      }
    ]
  }

The problem

After putting all of this in the helloworld dir. I used the command "cmake --preset=default" inside the helloworld dir, and it outputted this error:

Preset CMake variables:

  CMAKE_TOOLCHAIN_FILE="C:/vcpkg/scripts/buildsystems/vcpkg.cmake"

Preset environment variables:

  VCPKG_ROOT="C:/vcpkg"

-- Running vcpkg install
Fetching registry information from https://github.com/microsoft/vcpkg (HEAD)...
error: in triplet x64-windows: Unable to find a valid Visual Studio instance
Could not locate a complete Visual Studio instance

-- Running vcpkg install - failed
CMake Error at C:/vcpkg/scripts/buildsystems/vcpkg.cmake:904 (message):
  vcpkg install failed.  See logs for more information:
  C:/helloworld/build/vcpkg-manifest-install.log
Call Stack (most recent call first):
  C:/cmake/share/cmake-3.31/Modules/CMakeDetermineSystem.cmake:146 (include)
  CMakeLists.txt:3 (project)


CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!

I should mention that cmake can recognize "MinGW Makefiles" but without vcpkg. So I don't think it's a problem in my "PATH", or any environment variable on my system.


Solution

  • Following this question about using custom triplets with vcpkg and CMake, and vcpkg in cmake. I added triplets dir and inside it x64-mingw-dynamic and x64-mingw-static downloaded from github here, and changed CmakePresets.json to :

    {
      "version": 2,
      "configurePresets": [
        {
          "name": "vcpkg",
          "generator": "MinGW Makefiles",
          "hidden": true,
          "binaryDir": "${sourceDir}/build",
          "cacheVariables": {
            "VCPKG_OVERLAY_TRIPLETS": "${sourceDir}/triplets",
            "CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
          }
        },
        {
          "name": "mingw64-static",
          "hidden": true,
          "cacheVariables": {
            "VCPKG_TARGET_TRIPLET": "x64-mingw-static",
            "VCPKG_HOST_TRIPLET": "x64-mingw-static"
          }
        },
        {
          "name": "mingw64-dynamic",
          "hidden": true,
          "cacheVariables": {
            "VCPKG_TARGET_TRIPLET": "x64-mingw-dynamic",
            "VCPKG_HOST_TRIPLET": "x64-mingw-dynamic"
          }
        }
      ]
    }
    

    , and changed CmakeUserPresets.json to:

    {
      "version": 2,
      "configurePresets": [
        {
          "name": "default",
          "inherits": ["vcpkg", "mingw64-static"],
          "environment": {
            "VCPKG_ROOT": "C:/vcpkg"
          }
        }
      ]
    }
    

    , and I ran "cmake --preset=default" thin "cmake --build build" thin finally "./build/HelloWorld.exe", and it outputted: "Hello World!".