Search code examples
cmakeclang-complete

print complete CFLAGS/CXXFLAGS of CMake project


Is it possible to print the complete *CFLAGS and *CXX_FLAGS of a CMake project (of all targets?)?

I tried looking in CMakeCache.txt after configuring and building the project, but all the *C*FLAGS* related variables are almost empty, and anyway do not contain my project settings, such as -D and -I flags specific to my dependencies.

Why I want to do this? In order to create a configuration file for the clang-complete vim plugin: https://vim.sourceforge.io/scripts/script.php?script_id=3302


Solution

  • You can also get cmake to produce a json file of all commands to be run with:

    -DCMAKE_EXPORT_COMPILE_COMMANDS=ON
    

    In context:

    mkdir -p release
    cd release
    cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -G Ninja .. && ninja
    

    This will give you a file in release called compile_commands.json which has a record of your commands. This is similar to Stewart's answer except that it puts the log in a file for you automatically. Another advantage is that this file is used clang-tidy which I have found to be incredibly useful.