CMake command not working in VS Code task - cmake

I have created a task in my tasks.json file in Visual Studio Code:
{
"label": "cmake",
"command": "cmake",
"args": [
"-G",
"\"Unix Makefiles\"",
"-DCMAKE_BUILD_TYPE=Debug",
".."
],
"options": {
"cwd": "${workspaceRoot}/build"
},
"problemMatcher": []
},
When I run it from within VSC (Tasks: run task > cmake), I get the following error:
> Executing task: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug .. <
CMake Error: Could not create named generator "Unix Makefiles"
Generators
Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files.
Xcode = Generate Xcode project files.
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
CodeLite - Ninja = Generates CodeLite project files.
CodeLite - Unix Makefiles = Generates CodeLite project files.
Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
Sublime Text 2 - Unix Makefiles
= Generates Sublime Text 2 project files.
Kate - Ninja = Generates Kate project files.
Kate - Unix Makefiles = Generates Kate project files.
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
The terminal process terminated with exit code: 1
However if I execute the exact same command in terminal it works as expected:
oyvinds-MacBook:build oyvindhauge$ cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ..-- Configuring done
-- Generating done
-- Build files have been written to: /Users/oyvindhauge/Documents/Development/[PROJECT]/build
oyvinds-MacBook:build oyvindhauge$
Any idea what's going on?

Like stated in the comments, removing the extra quotes around generator param "Unix Makefiles" fixed the problem.

Related

How to perform verbose builds of a CMake project with Visual Studio 2019 generator

I am using Clion to work on a CMake project which needs to be built with a Visual Studio 16 2019 generator. When I run a build, Clion performs the following command:
$ cmake.exe --build C:\<PATH_TO_PROJECT>\cmake-build-release --target FooTarget --config Release
With this workflow now I would like to get verbose builds in order to troubleshoot which commands and command line arguments are being used by each build.
Is it possible to get cmake to run verbose builds while using a Visual Studio 2019 generator?
CMake supports passing generator-specific compiler flags as build tool options.
If you're using a Visual Studio generator, you can pass MsBuild command line options such as -verbosity:level with a command like:
cmake.exe --build C:\<PROJECTDIR>\cmake-build-debug --target <BUILDTARGET> --config Debug -- -verbosity:diagnostic
In Clion, just open the project's CMake settings and add -- -verbosity:diagnostic to the "build options" line edit.

CMake build types make no difference

I've worked in c++ for many years but I am new to CMake.
I build my app with
cmake --build build_dir --config Debug --target all -- -j 1
This works fine and builds the Debug version.
If I change the --config to anything else, for example Release with the following command:
cmake --build build_dir --config Release --target all -- -j 1
ninja says "no work to do" and exits. Running the compiled app it is clearly not optimised. There are two other options in this Cmake Project, RelWithDebug and MinSizeRel, and they act the same.
In CmakeLists.txt there is:
set(CMAKE_CONFIGURATION_TYPES "Release;Debug;MinSizeRel;RelWithDebInfo")
There are a lot of other mentions of the various types, but it's all slightly greek to me at this time.
What can I do to work out where the issue is?
I'm going to give you the full in depth answer. Just in case someone else has this confusion.
What is a generator?
Essentially a generator is the build system CMake creates. CMake doesn't directly build your project. Because CMake is a meta-build system. IE CMake is a build system that 'generates' your true build system. That's why it is called a 'generator'.
"Visual Studio 16 2019" was designed to handles multiple configurations in 1 build.
Which is why when you open a Visual Studio project created by CMake you can easily change between Debug, Release, etc.
Where as "Unix Makefiles" and "Ninja" can only handle 1 config type at a time.
This difference in build system abilities leads to slightly different CLI when running CMake.
Visual Studio 16 2019 (Multi-Config)
As mentioned before Visual Studio supports multiple config types in the build.
cmake -S . -B build/vs -G "Visual Studio 16 2019"
cmake --build build/vs --config Debug
cmake --build build/vs --config Release
In the first command you are creating the Visual Studio 2019 project.
In the second command you are actually building the binaries. And since Visual Studio projects are multi-config you don't need a different build folder for each type. Since Visual Studio handles it for you!
Ninja (Single Config)
cmake -S . -B build/ninja/ -G "Ninja" -D CMAKE_BUILD_TYPE=Debug
cmake --build build/ninja/
# Now I've updated the project to make Release binaries
cmake -S . -B build/ninja/ -D CMAKE_BUILD_TYPE=Release
cmake --build build/ninja/
In the example above you create a Ninja project for a debug build. Then you build it.
Then you create a Ninja project for a release build. Then you build it.
Notice how you have to manually specify 2 build folders for each type yourself.
Ninja Multi-Config (new in CMake 3.17)
Thanks to advances in Ninja and CMake you can create avoid the hassle of specifying the build type at project creation time. So now it's just like Visual Studio.
So now you can create a "Ninja Multi-Config" project instead of just a "Ninja" project.
cmake -S . -B build/nin -G "Ninja Multi-Config"
cmake --build build/nin --config Debug
cmake --build build/nin --config Release
Further Elaboration
Single Config Vs Multi-Config
How can you tell if your generator is Multi vs Single?
Read the docs. Or if you need to in your scripts you can check it programmatically.
You can query the global property GENERATOR_IS_MULTI_CONFIG
CMAKE_BUILD_TYPE
A minor thing to mention is that CMAKE_BUILD_TYPE doesn't do anything on multi-config generators. So avoid using it in your CMake code.
See this answer: CMAKE_BUILD_TYPE is not being used in CMakeLists.txt

Trying to configure CMake tool chain on Ubuntu

The software will eventually run on an embedded Linux platform. To start, we wrote a library, with all C files in the same folder, and C++ files in a test folder under it.
MyLib
a.c
b.c
MyLib/test
test.cpp
This works on Windows 10, using CMake, Ninja and CLang plus doctest as a test framework. All of these tools are installed under vcpkg.
I am now trying to get everything configured and running on Ubuntu 16.04. All of the tools and source code have been retrieved from our repository. I have tried to build the library and test with Qt, cmake-gui and the command line without success.
Using CMake-gui, after browsing to the source and destination, clicking the Configure button causes the error
CMake Error: CMake was unable to find a build program corresponding to "Ninja".
CMAKE_MAKE_PROGRAM was not set.
The ninja program is in ~/../vcpkg/downloads/tools/ninja-1.8.2-linux/ folder.
I had different errors when trying to build from the command line.
Are there any examples of configurations that work with controlled tools instead of those in the environment?
Edit: added
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
project(PSOC_LIB)
option(BUILD_TESTS "bulid tests project" OFF)
set(psoc_lib_VERSION_MAJOR 0)
set(psoc_lib_VERSION_MINOR 1)
set(psoc_lib_VERSION_PATCH 0)
set(psoc_lib_VERSION "${psoc_lib_VERSION_MAJOR}.${psoc_lib_VERSION_MINOR}.${psoc_lib_VERSION_PATCH}")
set(PROJ_NAME "\"PSOC Library\"") # PROJECT_NAME is a predefined variable
set(PROJECT_DESCRIPTION "\"Crossplatform library for products\"")
configure_file(include/psoc/config.h.in
${CMAKE_BINARY_DIR}/include/psoc/config.h
)
set(SOURCES
# 5 *.c files
)
add_library(PSOC_LIB ${SOURCES})
target_include_directories(PSOC_LIB PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_definitions(PSOC_LIB PRIVATE _CRT_SECURE_NO_WARNINGS=1)
if (BUILD_TESTS)
include(CTest)
enable_testing()
add_subdirectory(test)
endif (BUILD_TESTS)
test/CmakeLists.txt
cmake_minimum_required(VERSION 3.10)
set(TEST_SOURCES
test.cpp
)
find_package(doctest CONFIG REQUIRED)
add_executable(test_runner test_runner.cpp ${TEST_SOURCES})
target_compile_definitions(test_runner PRIVATE _SILENCE_CXX17_ALLOCATOR_VOID_DEPRECATION_WARNING) #silence warnings about allocator<void> deprecation
target_compile_definitions(test_runner PRIVATE _WIN32_WINNT=0x0601) #target windows 7
target_link_libraries(test_runner PSOC_LIB doctest::doctest)
add_test(all_tests test_runner)
Under Windows, the files in the test folder are build even when BUILD_TESTS is OFF. Under Linux, the test and Testing folders are created only when BUILD_TESTS is ON.
I created a shell script that builds an executable test file.
#!/bin/bash
# Build a debug version of the PSOC_LIB and related tests
mkdir build
cd build
../../../vcpkg_pml/vcpkg/downloads/tools/cmake-3.14.0-linux/cmake-3.14.0-Linux-x86_64/bin/cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -S ../
ninja

Use LLVM in a cmake build

I'm trying to build my own project that use LLVM. I downloaded the source code and the precompiled package on the official web site (last version).
http://releases.llvm.org/download.html
I downloaded :
LLVM source code
Clang for Windows (64-bit)
FYI, I don't build LLVM... only want to use it !
I followed the instruction here :
http://llvm.org/docs/CMake.html#embedding-llvm-in-your-project
in the section : "Embedding LLVM in your project"
So, I added this code :
find_package(LLVM REQUIRED CONFIG)
message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message("LLVM_INCLUDE_DIRS=${LLVM_INCLUDE_DIRS}")
message("LLVM_DEFINITIONS=${LLVM_DEFINITIONS}")
But I got several cmake error messages, here is my output :
-- Using LLVMConfig.cmake in: C:\\Luciad_src\\libs\\LLVM\\cmake\\modules
LLVM_INCLUDE_DIRS=
LLVM_DEFINITIONS=
CMake Error at C:/Luciad_src/libs/LLVM/cmake/modules/LLVM-Config.cmake:31 (list):
list sub-command REMOVE_ITEM requires two or more arguments.
Call Stack (most recent call first):
C:/Luciad_src/libs/LLVM/cmake/modules/LLVM-Config.cmake:256 (is_llvm_target_library)
components/query/CMakeLists.txt:15 (llvm_map_components_to_libnames)
Is there a problem with my script, or the packages I use ? Any idea ?
Thanks for your help
You can have the LLVM as binary or source package.
The binary package does not include CMake support. If you compare both installations (binary on the left, source after building and installing it on the right) you can see the difference:
LLVM-5.0.0-win64.exe
<=> llvm-5.0.1.src.tar.xz (build and installed)
So you need to build and install the source package first to get CMake support. On my Windows machine I needed a cmd shell with administrator rights, a Visual Studio installation, go to the downloaded and extracted sources and do:
> mkdir mybuilddir
> cd mybuilddir
> cmake ..
> cmake --build . --config Release --target INSTALL
If I now use your CMake example I get:
-- Found LLVM 5.0.1
-- Using LLVMConfig.cmake in: C:/Program Files (x86)/LLVM/lib/cmake/llvm
LLVM_INCLUDE_DIRS=C:/Program Files (x86)/LLVM/include
LLVM_DEFINITIONS=-DLLVM_BUILD_GLOBAL_ISEL -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -D_CRT_SECURE_NO_DEPRECATE -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_DEPRECATE -D_CRT_NONSTDC_NO_WARNINGS -D_SCL_SECURE_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS -DUNICODE -D_UNICODE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
-- Configuring done

Verbose NMake Makefiles using CMake

I have been following instructions from the FAQ: Is there an option to produce more 'verbose' compiling?. I have now:
$ cat C:/Program Files/CMake 2.8/share/cmake-2.8/Modules/Platform/Windows.cmake
[...]
# uncomment these out to debug nmake and borland makefiles
set(CMAKE_START_TEMP_FILE "")
set(CMAKE_END_TEMP_FILE "")
set(CMAKE_VERBOSE_MAKEFILE 1)
I then regenerate my project, and run nmake:
> cmake --version
cmake version 2.8.12.2
> cmake -G"NMake Makefiles" ..\project
> nmake VERBOSE=1
[...]
[ 73%] Building CXX object apps/foobar/CMakeFiles/bla.dir/my.cpp.obj
cd C:\Dashboards\MyTests\project-build-debug-vista32-nightly\apps\foobar
C:\PROGRA~1\MICROS~1.0\VC\bin\cl.exe #C:\Users\voxxl\AppData\Local\Temp\nm1362.tmp
Is there a way to have access to the full cl command line ?
On Windows (XP or greater), Command lines are limited in length to 8,191 bytes.
Windows compiler toolsets get around this by allowing the #filepath syntax on the command line.
So VERBOSE=1 is less useful on Windows. But you can always check command line in generated *.make files
For example (path used from question):
apps/foobar/CMakeFiles/bla.dir/build.make
apps/foobar/CMakeFiles/bla.dir/flags.make