Skip to content

CMake build automation utility

Zeioth edited this page Nov 11, 2023 · 7 revisions

When compiler.nvim detects a CMakeLists.txt file in the current working directory, Telescope will be populated with all its add_executable and add_custom_target entries.

Hello world

Create a CMakeLists.txt file in your working directory and copy/paste this:

cmake_minimum_required(VERSION 3.5)

# Example of a target
project(HelloWorld)
add_executable(hello_world main.cpp)

# Example of custom target to run all tasks
add_custom_target(build_all)
add_dependencies(build_all hello_world)

Now let's create the file we are building on the example above: Create main.cpp in the same directory.

#include <iostream>

int main() {
    std::cout << "Hello, World!" << std::endl;
    return 0;
}

When you open the compiler you will see the option to run it screenshot_2023-11-02_00-00-12_901709390

And this is the result screenshot_2023-11-02_01-40-02_298225055

In the same way you can run hello world, you can execute any command necessary to build your program.

More info

(optionally) You can define the next globals

global defaut value
CMAKE_BUILD_DIR ./build
CMAKE_BUILD_TYPE Debug
CMAKE_CLEAN_FIRST false

Example:

let g:CMAKE_BUILD_TYPE='Release'

Globals persist between Neovim sessions.