3

I have created the following CMakelists.txt for my project which includes some files which need to be compiled with C and then will be linked with my C++ binary. I'm also using libasan.

cmake_minimum_required(VERSION 3.0)

SET(GCC_COVERAGE_COMPILE_FLAGS "-g3 -fsanitize=address -fno-omit-frame-pointer")
SET(GCC_COVERAGE_LINK_FLAGS    "-fsanitize=address -static-libasan")

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

project(ABC_PROXY VERSION 1.0.0 LANGUAGES C CXX)

add_executable(abc_proxy
src/file1.c
src/main.cpp
)

target_include_directories(abc_proxy PRIVATE /home/vishal/cpp_file/new /home/vishal/cpp_file/new/framework)
SET(CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")

In the above file I only have one target binary. But now I want to have 2 binaries. One will be compiled with libasan and the other will be compiled without it. How can I use different flag values in the 'CMAKE_EXE_LINKER_FLAGS ' , 'CMAKE_CXX_FLAGS' and 'CMAKE_CXX_FLAGS' for each binary target?

Vishal Sharma
  • 1,670
  • 20
  • 55
  • Did you read any cmake documentation? Did you follow any tutorial? What research did you do? https://stackoverflow.com/questions/25043458/does-cmake-have-something-like-target-link-options – KamilCuk Jul 07 '20 at 07:50
  • @KarmilCuk thanks for sharing the link...I had found the answer myself at about the same time you posted that link...However I don't know how I will pass different compile flags in the same target like in my case...in each target 2 compilers are being used. currently the same flags are being passed...but in future I might need to have different flags for the C compiler and different flags for the C++ compiler(although I don't know if there can ever be any such use case). – Vishal Sharma Jul 07 '20 at 08:27
  • `if(CMAKE_COMPILER_ID STREQUAL "GNU") target_compile_flags() elseif(CMAKE_COMPILER_ID STREQUAL "MSVC") target_compile_flags() etc...` or with generator experssions `target_compile_flags($<$:-flag for gnu>)` – KamilCuk Jul 07 '20 at 08:46
  • @KamilCuk I had meant gcc and g++ btw – Vishal Sharma Jul 07 '20 at 08:47
  • 1
    for that use [generator expressions](https://cmake.org/cmake/help/latest/manual/cmake-generator-expressions.7.html) `target_compile_flags($<$:-flag_for_c> $<$:-flag for cxx>)` – KamilCuk Jul 07 '20 at 08:48

1 Answers1

5

Ok, so after browsing through different answers here I've made the CMakelists.txt given below and it works in this case.

cmake_minimum_required(VERSION 3.0)

project(ABC_PROXY VERSION 1.0.0 LANGUAGES C CXX)

add_executable(abc_proxy_with_asan
src/file1.c
src/main.cpp
)

set_target_properties(abc_proxy_with_asan PROPERTIES COMPILE_FLAGS "-g3 -fsanitize=address -fno-omit-frame-pointer")
set_target_properties(abc_proxy_with_asan PROPERTIES LINK_FLAGS "-fsanitize=address -static-libasan")
target_include_directories(abc_proxy_with_asan PRIVATE /home/vishal/cpp_file/new /home/vishal/cpp_file/new/framework)

add_executable(abc_proxy
src/file1.c
src/main.cpp
)

set_target_properties(abc_proxy PROPERTIES COMPILE_FLAGS "-g3 -fno-omit-frame-pointer")
target_include_directories(abc_proxy PRIVATE /home/vishal/cpp_file/new /home/vishal/cpp_file/new/framework)
Vishal Sharma
  • 1,670
  • 20
  • 55
  • 1
    ran into 'unable to determine linker language' for multiple c libraries defined in same cmake script. used project(...LANGUAGES CXX) due to main target as c++. the targets get configured twice when a graphviz target is configured and built, and misses some cmake magic to figure out linker language. adding 'C' to the list for the LANGUAGES parameter fixed the issue, thanks for your ansser – AngryDane Sep 24 '21 at 14:29