I have a simple CMake project which uses CMakePresets.json to set up common settings and Ninja as its underlying build system to compile a C++ program.
The problem that I'm facing right now is in CLion where it always picks x86 architecture by default instead of x64.
If I explicitly tell Ninja to use x64 it will throw the following error:
CMake Error at CMakeLists.txt:3 (project):
Generator
Ninja
does not support platform specification, but platform
x64
was specified.
I know that I should run vcvarsall.bat in x64 mode to define proper environment variables but I couldn't find a way to do it automatically in CLion.
So calling vcvarsall.bat isn't pleasant since I should manually run vcvarsall.bat.
Is there any way to automate this process inside CMake?
This is my CMakePresets.json if anyone curious to know:
{
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"description": "Target Windows with the Visual Studio development environment.",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/Build/${presetName}",
"installDir": "${sourceDir}/Install/${presetName}",
"architecture": {
"value": "x64",
"strategy": "external"
},
"toolset": {
"value": "host=x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
}
},
{
"name": "x64-debug",
"displayName": "Debug",
"description": "Target Windows (64-bit) with the Visual Studio development environment. (Debug)",
"inherits": "windows-base",
"cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" }
},
{
"name": "x64-release",
"displayName": "Release",
"description": "Target Windows (64-bit) with the Visual Studio development environment. (Release)",
"inherits": "windows-base",
"cacheVariables": { "CMAKE_BUILD_TYPE": "Release" }
},
{
"name": "x64-release_with_debug_information",
"displayName": "Release with Debug Information",
"description": "Target Windows (64-bit) with the Visual Studio development environment. (RelWithDebInfo)",
"inherits": "windows-base",
"cacheVariables": { "CMAKE_BUILD_TYPE": "RelWithDebInfo" }
}
],
"buildPresets": [
{
"name": "Debug Preset",
"configurePreset": "x64-debug"
},
{
"name": "Release Preset",
"configurePreset": "x64-release"
},
{
"name": "Release with Debug Information Preset",
"configurePreset": "x64-release_with_debug_information"
}
]
}
Thanks in advance.
You need to tell CLion, which toolchain you want to use.
Related
A few weeks ago, we introduced the introduce the cmakepresets.json to our cpp cross platform project, which we code with Visual Studio. The setup was really straight forward and we are able to build and debug our project without any problems.
There is just one thing which is a little bit annoying. I would like to use a variable to secifiy the directory on the remote system. It's defined over the "sourceDir"-tag of the Visual Studio Remote Settings vendor map of the CMakePresets.json. There the HOME enviroment variable is used and the special visual studio variable $ms{projectDirName} which evaluates to the name of the open folder in Visual Studio.
{
"name": "linux-default",
"displayName": "Linux Debug",
"description": "Sets Ninja generator, compilers, build and install directory, debug build type",
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}"
},
"vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [ "Linux" ]
},
"microsoft.com/VisualStudioRemoteSettings/CMake/1.0": {
"sourceDir": "$env{HOME}/.vs/$ms{projectDirName}"
}
}
}
I tried to define a enviroment variable with the CMakeLists.txt but I think that the variable isn't in the scope of the CMakePresets.json. Are there any other possibilities to variable define the source directory?
I'm very new to CMake (and new to C++ too, although that shouldn't matter here), and I am having a problem using CMake with Visual studio.
I have created a directory, let's say it's called Project, and put in it a simple project with the following structure:
Project/
build/ <empty>
src/
main.cpp
CMakeLists.txt
CMakePresets.json
Inside these files is just the most basic, default code:
CMakeLists.txt:
cmake_minimum_required (VERSION 3.8)
project (Project)
set (CMAKE_CXX_STANDARD 20)
set (CMAKE_CXX_STANDARD_REQUIRED True)
add_executable (Project src/main.cpp)
CMakePresets.json (this code is just the default that was generated):
{
"version": 3,
"configurePresets": [
{
"name": "windows-base",
"hidden": true,
"generator": "Ninja",
"binaryDir": "${sourceDir}/out/build/${presetName}",
"installDir": "${sourceDir}/out/install/${presetName}",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Windows"
}
},
{
"name": "x64-debug",
"displayName": "x64 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x64",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x64-release",
"displayName": "x64 Release",
"inherits": "x64-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
},
{
"name": "x86-debug",
"displayName": "x86 Debug",
"inherits": "windows-base",
"architecture": {
"value": "x86",
"strategy": "external"
},
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "x86-release",
"displayName": "x86 Release",
"inherits": "x86-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
]
}
src/main.cpp:
#include <iostream>
int main() {
std::cout << "Hello, world!" << std::endl;
return 0;
}
Then, I have used CMake to create a Visual Studio solution:
C:\...\Project\build> cmake ..
This has worked fine without any errors, and Visual Studio can open the solution. It can also build the project correctly...
But it cannot run the executable which it has built. After successfully building the project, it has written the executable to C:\...\Project\build\Debug\Project.exe, but it tries to open C:\...\Project\build\x64\Debug\ALL_BUILD instead, and I get an error popup.
I gather that there are two things wrong here:
The executable file should be written within the C:\...\Project\build\x64\Debug folder, not just the C:\...\Project\build\Debug folder. This is how it has worked whenever I have used Visual Studio before, and this is the folder it is trying to search in.
It should be searching for an executable called Project.exe, not one called ALL_BUILD.
When I run Project.exe manually from the command line, it works fine. But I cannot seem to make Visual Studio run it correctly.
What have I done wrong here, and how can I get this to work?
Default project is set to ALL_BUILD to change the default for the VS generators use the following CMake statement:
set_property(DIRECTORY PROPERTY VS_STARTUP_PROJECT Project)
Anywhere after the add_executable command.
You see several projects in the solution explorer. These projects are build targets. The default target is ALL_BUILD, that builds all configured targets, like cmake --build . does it.
Set required target as the startup project in the solution explorer. This will point the debugger what executable to run.
I have set up VS code as my development environment and used
MSVC build tools (cl.exe compiler) instead of g++. I tried to set up SFML for my environment. I want to know how do I set the SFML include path and library path. And also, How do I perform static Linking with cl.exe. Note: I am using only VS code and NOT Visual Studio for programming. Below are some files I've used. Tasks.json,Launch.json, C_cpp_properties.json.
Tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe:",
"${workspaceFolder}\\bin\\${fileBasenameNoExtension}.exe",
"${workspaceFolder}\\*.cpp"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "cl.exe - Build and debug active file",
"type": "cppvsdbg",
"request": "launch",
"program": "${workspaceFolder}\\bin\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"console": "externalTerminal",
"preLaunchTask": "C/C++: cl.exe build active file"
}
]
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${default}"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2022/BuildTools/VC/Tools/MSVC/14.30.30705/bin/Hostx64/x64/cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
As mentioned in the comments, you need to use a C++ build system to manage dependencies and build your project. VSCode does not come with any built-in build systems like Visual Studio does.
VSCode tasks allow you to specify a command line, which can then be invoked easily in the IDE. The task shown is just a "build active file" task, which is only really useful for trivial programs with no dependencies. It invokes cl.exe on the current source file (and passes a few other arguments).
You can specify include directories and pass arguments to the linker by adding to the "args" array in the task, e.g.:
"/I", "D:\\Code Libraries\\boost_1_77_0",
"/link", "/LIBPATH:\"D:\\Code Libraries\\boost_1_77_0\\stage\\lib\"",
which assumes that the boost headers and (statically built) libraries are at the specified locations.
You could probably work out how to build an entire project by adding command lines with VSCode tasks, but it's probably easier to use a build system (even if that system is CMake).
I have to use cygwin and cmake for the build of the release and debug.
(The cygwin package has all needed tools/libs for the build process (cmake, gcc, boost))
I've already found this
Which now allows me to run cmake and the build scripts inside the console of visual studio code and the executable is generated properly.
And I've already installed the extension C/C++ for Visual Studio Code.
But how to configure visual studio code for debugging?
After building via this, all you need for debugging is a launch.json of the following structure:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Starten",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/PATH_2_YOUR_DEBUG.exe",//This you need to define
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\cygwinPath\\bin\\gdb.exe",//This you need to define
"setupCommands": [
{
"description": "Automatische Strukturierung und Einrückung für \"gdb\" aktivieren",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
Afterwards you can set a breakpoint in the source of PATH_2_YOUR_DEBUG.exe and press F5.
After setting up VS Code, installing the build tools and going through the tutorial here: https://code.visualstudio.com/docs/cpp/config-msvc
Visual Studio Code is unable to find the cl.exe to compile C++.
I replaced the path from the tutorial with the correct one on my hard drive (cl.exe is there).
// My Config
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.22.27905/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
// The tutorial build-task
{
"version": "2.0.0",
"tasks": [
{
"label": "msvc build",
"type": "shell",
"command": "cl.exe",
"args": [
"/EHsc",
"/Zi",
"/Fe:",
"helloworld.exe",
"helloworld.cpp"
],
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"reveal":"always"
},
"problemMatcher": "$msCompile"
}
]
}
When running the build task this error shows, although the compilerPath is correct (the cl.exe is there) and helloworld.cpp exists as well. Running everything as administrator didn't help.
cl.exe : The term 'cl.exe' is not recognized as the name of a cmdlet, function, script file, or
operable program. Check the spelling of the name, or if a path was included, verify that the path
is correct and try again.
At line:1 char:1
+ cl.exe /EHsc /Zi /Fe: helloworld.exe helloworld.cpp
+ ~~~~~~
+ CategoryInfo : ObjectNotFound: (cl.exe:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
The 'c_cpp_properties.json' file only configures IntelliSense in the C/C++ extension, so the compilerPath option does not help with building.
Make sure you are launching VS Code from the Developer Command Prompt. This will set the necessary environment variables, including the location of 'cl.exe'.
In the documentation of Visual Studio Code you can see a solution to this issue.
In the section of "C++ > Microsoft C++ on Windows > Troubleshooting" they explain that you need to open your projects from the Developer Command Promp
As an example:
cd projects/yourproject
code .
I haven't found other way of doing it.
My first answer had used hardcoded paths, but this is better. You only need to pay attention to the paths since the docs use 2019 etc.
The VsDevCmd.bat sets up all the env vars you'll need and it works easily within VSCode. It also gives you compiler errors which my previous solution with the hardcoded cl.exe paths did not for some reason.
Run VS Code outside the Developer Command Prompt