I followed this article to learn how to use vcpkg and got this error:
C:\vcpkg\helloworld>cmake –preset=default
Preset CMake variables:
CMAKE_TOOLCHAIN_FILE=””C:\vcpkg”/scripts/buildsystems/vcpkg.cmake”
CMake Error at C:/CMake/share/cmake-3.25/Modules/CMakeDetermineSystem.cmake:130 (message):
Could not find toolchain file:
“C:\vcpkg”/scripts/buildsystems/vcpkg.cmake
Call Stack (most recent call first):
CMakeLists.txt:3 (project)
CMake Error: CMake was unable to find a build program corresponding to “Ninja”. 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!
It is weird because I’ve installed Ninja and include the path to ninja.exe into the PATH environment variable. Why does it complain it cannot find Ninja?
In fact, this is due to an error in the instruction provided by that tutorial.
In step 2, the tutorial asks you to set an variable:
Note that VCPKG_ROOT=”C:\path\to\vcpkg” is incorrect, it should be VCPKG_ROOT=C:\path\to\vcpkg(no quotation marks), because in the CMakePresets.json
created later, a CMake variable is set according to this variable:
“CMAKE_TOOLCHAIN_FILE”: “$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake”
The wrong version of VCPKG_ROOT will give a wrong CMAKE_TOOLCHAIN_FILE, thus CMake cannot find the file vcpkg.cmake leading to the ninja not found error.
The complete instructions are:
set VCPKG_ROOT=C:\path\to\vcpkg
set PATH=%VCPKG_ROOT%;%PATH%
mkdir helloworld && cd helloworld
vcpkg new --application
vcpkg add port fmt
{
"dependencies": [
"fmt"
]
}
//vcpkg.json
cmake_minimum_required(VERSION 3.10)
project(HelloWorld)
find_package(fmt CONFIG REQUIRED)
add_executable(HelloWorld main.cpp)
target_link_libraries(HelloWorld PRIVATE fmt::fmt)
#CMakeLists.txt
#include <fmt/core.h>
int main()
{
fmt::print("Hello World!\n");
return 0;
}
//main.cpp
{
"version": 2,
"configurePresets": [
{
"name": "default",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"cacheVariables": {
"CMAKE_TOOLCHAIN_FILE": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
}
}
]
}
//CMakePresets.json
cmake --preset=default
cmake --build build
The configuration step(cmake –preset=default) still failed due to errors:
— The C compiler identification is unknown
— The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:3 (project):
No CMAKE_C_COMPILER could be found.
Tell CMake where to find the compiler by setting either the environment
variable “CC” or the CMake cache entry CMAKE_C_COMPILER to the full path to
the compiler, or to the compiler name if it is in the PATH.
CMake Error at CMakeLists.txt:3 (project):
No CMAKE_CXX_COMPILER could be found.
Tell CMake where to find the compiler by setting either the environment
variable “CXX” or the CMake cache entry CMAKE_CXX_COMPILER to the full path
to the compiler, or to the compiler name if it is in the PATH.
You need to set the CMake variables CMAKE_CXX_COMPILER and CMAKE_C_COMPILER, etc. in CMakePresets.json, or just execute the “C:\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars64.bat” (or C:\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvars32.bat) to set up the build environment.
Last, correct
add_executable(HelloWorld helloworld.cpp)
in CMakeLists.txt to
add_executable(HelloWorld main.cpp)
I doubt the guy writing that tutorial never practiced his example on a real machine.