Is there a way to generate a customized CMakeLists.txt from CMake Tools' `CMake: Quick Start` command?
Asked Answered
L

1

0

When starting a new C++ project on VS Code with CMake Tools extension, we can run the "CMake: Quick Start" command to generate a basic CMakeLists.txt file with basic project configuration. I would like to know if I can customize this generation so it includes some additional configuration on every new project. I like to use address sanitizer on even simple programs i create so i want to have it setup automatically.

Standard config:

cmake_minimum_required(VERSION 3.0.0)
project(filas VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(filas main.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})

include(CPack)

The additional config i want:

if(CMAKE_DEFAULT_BUILD_TYPE MATCHES "Debug")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fsanitize=undefined -fsanitize=address")
  target_link_options(filas
    BEFORE PUBLIC -fsanitize=undefined PUBLIC -fsanitize=address
  )
endif()

I'm a beginner at C++ and CMake and couldn't find this specific info so far.

Lauraine answered 10/11, 2022 at 21:24 Comment(3)
I believe there is documentation for the cmake extension online. Did you try looking that up?Remillard
If you don't find what you're looking for, you can consider extracting those common things into reusable cmake helper scripts and using them in your projects via file(DOWNLOAD) and then include(), or FetchContent.Radu
You can get all the customization you want if you simply pick up a simple text editor and just write your CMakeLists.txt file from scratch.Hillier
R
0

If you want arbitrary customization, no. Not through the CMake: Quick Start command / mechanism.

At least at the time of this writing, it's hardcoded. You can see for yourself in https://github.com/microsoft/vscode-cmake-tools/blob/15ad70769f62b5ce902b6fd1ae27986bb958d3d0/src/cmakeProject.ts#L2903.

You can make something custom through custom user snippets, but it'll be nowhere near as nice of a workflow as CMake: Quick Start.

Ex. this file template snippet, while you could put in a cmake.json file (run Snippets: Configure User Snippets and select "cmake", assuming you have a language support extension for CMake that contributes a language with that ID):

{
    "Project CMakeLists.txt": {
        "prefix": "CMake Project CMakeLists.txt",
        "isFileTemplate": true,
        "description": "For filling a new project's top-level CMakeLists.txt file",
        "body": "cmake_minimum_required(VERSION ${1:3.29})\nproject(${2:${WORKSPACE_NAME}}\n\tVERSION ${3:0.0.1}\n\tDESCRIPTION [[${4}]]\n\tHOMEPAGE_URL [[${5}]]\n\tLANGUAGES ${6:CXX}\n)\nadd_${7|executable,library|}(${8:${2}}\n\t${9/([^\\n]+\\.c[px]?[px]?\\n)|([^\\n]+\\n)/\t$1/mgsu}\n)\ntarget_compile_features(${8} PUBLIC ${10:cxx_std_20})\n${LINE_COMMENT} set_target_properties(${8} PROPERTIES )\ntarget_include_directories(${8} PUBLIC ${11:include})\n${LINE_COMMENT} ${12|enable_testing(),include(CTest)|}",
    },
}

It's a little gnarly since I insisted upon not requiring any utility extensions.

For the section for populating source files, you can use this keybinding to copy relative paths for all files from the Explorer View, then paste them into there:

{
    "key": "ctrl+b", // TODO
    "command": "runCommands",
    "args": { "commands": [
        "workbench.files.action.focusFilesExplorer",
        "list.closeFind",
        "list.selectAll",
        "copyRelativeFilePath",
        "list.clear",
        "workbench.action.focusActiveEditorGroup"
    ]},
    "when": "editorLangId == cmake && inSnippetMode && editorFocus",
},

And then when you tab out of that snippet placeholder, the snippet logic will remove any files that don't have certain filename extensions.


For the specific code you showed you want in the question post, note that you probably instead want to use CMAKE_<LANG>_FLAGS_<CONFIG>, and note that CMAKE_<LANG>_FLAGS typically affects the link command as well by default (see the first line of the docs). And for this stuff, it's probably more appropriate to specify in cmake.configureSettings, or a preset file, or a file specified by CMAKE_PROJECT_TOP_LEVEL_INCLUDES.

Radu answered 15/7, 2024 at 4:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.