1

Using CMake 3.14 or greater and gcc/g++, I wrote

set(GCC_COVERAGE_COMPILE_FLAGS "-Wno-comment -Werror=incompatible-pointer-types -Werror=return-type")
set(CMAKE_C_FLAGS  "${CMAKE_C_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")

in the CMakeLists.txt for a target (call it "T"). But the following C code gives me no error :

bool_T DC_CompressData(int rate)
{
    /* Compress data */
    ...

    /* Forget return statement because I am absent-minded */
}

This code is in a C-file that belongs to target A, which is linked to target B, which is linked to target T where I originally set the flags. Even if I add those lines to the CMakeLists.txt of target A, I still get no result.

CMakeLists.txt for T

project(T VERSION 4.0 LANGUAGES C CXX)

add_library(T STATIC)

target_sources(T
    PRIVATE
        t.c
        t.h
)

CMakeLists.txt for B

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(B VERSION 4.1 LANGUAGES C CXX)

add_library(B INTERFACE)

add_subdirectory(T)
target_link_libraries(B INTERFACE T)

CMakeLists.txt for A

cmake_minimum_required(VERSION 3.14 FATAL_ERROR)
project(A VERSION 2.0 LANGUAGES CXX)

set(TARGET ${PROJECT_NAME})
add_library(${TARGET} MODULE)

# COMPILE/LINK FLAGS

set(GCC_COVERAGE_COMPILE_FLAGS "-Wno-comment -Werror=incompatible-pointer-types -Werror=return-type")
set(GCC_COVERAGE_LINK_FLAGS    "-Werror=return-type")

set(CMAKE_C_FLAGS  "${CMAKE_C_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} ${GCC_COVERAGE_COMPILE_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")

target_link_libraries(${TARGET}
    PRIVATE
        A::A

install(TARGETS ${TARGET} LIBRARY DESTINATION ${INSTALLATION_PATH}
    COMPONENT ${TARGET})

I use Qt Creator 4.9.2 and use the "Deploy" command in order to build & install & whatever else CMake does.

I am mostly new to CMake, so what am I doing wrong ? Could it be that g++ is called instead of gcc, even though this is a C file ? I mention this because other files in other target (in T) are C++ files. Would I then need to set the flagsfor g++ ?

Charles
  • 988
  • 1
  • 11
  • 28
  • 1
    If you follow that answer: https://stackoverflow.com/a/11797272/3440745, then setting `GCC_COVERAGE_COMPILE_FLAGS` variable is not the only step: you need to perform other step, which uses the variable. – Tsyvarev Nov 19 '19 at 12:43
  • Thank you, I did forget those steps. Yet ... still no improvement. Edited the original post accordingly. – Charles Nov 19 '19 at 13:33
  • 1
    "But the following **C** code ..." - `CMAKE_CXX_FLAGS` sets flags for **C++** code. For C set variable `CMAKE_C_FLAGS`. – Tsyvarev Nov 19 '19 at 13:59
  • My code keeps improving thanks to all of your answers yet ... still no result =( – Charles Nov 19 '19 at 14:48
  • 1
    So, B links to A, and T links to B? How are you linking these libraries? How are you creating the targets? Have you confirmed that the libraries are actually being built? I would post the relevant parts of each `CMakeLists.txt` (include the parts where you create the libraries and link them to other targets). Also post how you are building. We must be able to reproduce the issue. – thomas_f Nov 19 '19 at 21:16
  • Improvements to original post done. – Charles Nov 20 '19 at 13:45

0 Answers0