2

I'm using CMake 3.12 with linux and I try to extract all flags, includes and defines programatically from a target. I found some help online, but it was always limited to the default settings induced by CMake.

For example with the variables :

CMAKE_CXX_FLAGS
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_RELEASE
...

Here I'm missing external flags. I' stumbled upon the file "flags.make" located in the build folder of the project:

# CMAKE generated file: DO NOT EDIT!
# Generated by "Unix Makefiles" Generator, CMake Version 3.12

# compile C with /usr/bin/gcc-4.8
C_FLAGS = -O3 -DNDEBUG -fPIC  

C_DEFINES = -DLINUX -DLINUX64 -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE

C_INCLUDES = 

Which is exactly what I need. Does anybody know how to access these variables within CMake?

Thank you!

Best regards Fabian

Fabian
  • 35
  • 4
  • 1
    "I try to extract all flags, includes and defines programatically from a project." - Single project may have several **targets** (executables and/or libraries) which has their **own set of flags**. Moreover, a single **target** may have several **source files**, each is *compiled* with its **own flags**. So it is not clear what do you mean by "flags from a **project**". If you want to see which flags are actually used for compile one or another source file, or when link one or another executable or library, then pass `V=1` parameter to `make`. – Tsyvarev Jul 06 '20 at 13:05
  • You are right, **target** is the better wording. My question is, is there any way to access the variables from the Unix Makefiles Generator? – Fabian Jul 06 '20 at 14:35
  • There is no single CMake variable which contains e.g. all compiler flags for a given sources. These flags are composed from CMake **variables** - `CMAKE_CXX_FLAGS`, `CMAKE_CXX_FLAGS_DEBUG` - and several **target's properties**. Moreover, some part of flags is taken from properties to **other targets**, which a linked (via `target_link_libraries`) into given target. – Tsyvarev Jul 06 '20 at 15:09
  • CMake generates `compile_commands.json` file after successful configuration. You can try using that. – arrowd Jul 06 '20 at 19:48

0 Answers0