user45189
user45189

Reputation: 169

Adding compiler "#define" flags in sub_directory CMakeLists.txt

My directory structure of project is build with multi CMakeLists.txts.

In my CMSIS_lib I build separately my dependency source files calculate.c and calculate.h with CMSIS_lib/CMakeList.txt:

set(util_source_files
  calculate.c
  calculate.h
)
add_library(util ${util_source_files})
target_include_directories(util calculate.h)

In my root CMakeLists.txt:

cmake_minimum_required(VERSION 3.4)
project(main_projct)
set(TOOLCHAIN_PREFIX /opt/gcc-arm-none-eabi)
set(CMAKE_TOOLCHAIN_FILE cmake/toolchain.cmake)

add_subdirectory(CMSIS_lib)

add_executable(main_projct main.c)
target_link_libraries(main_projct util)

Problem is that I must tell my compiler to add a #define GUCCI in my calculate.h (In MakeFile I know there is flag to tell a header define with -DGUCCI). I would like to add this flag to my compiler in my CMSIS_lib/CMakeList.txt, because when the first CMSIS_lib/CMakeList.txt is done building, he will skip everything under #ifndef GUCCI in my calculate.h, and when added in root CMakeLists.txt with target_link_libraries() I will not have all defines configuration correctly.

I am using cross-compiler and in my toolchain.cmake I use to define compiler flags with command SET_TARGET_PROPERTIES(${TARGET} PROPERTIES COMPILE_DEFINITIONS GUCCI}"), but this is to late because this only is seen by my root CMakeLists.txt and not by my sub director CMakeLists.txt.

Upvotes: 0

Views: 680

Answers (1)

gordan.sikic
gordan.sikic

Reputation: 1640

Your CMSIS_lib/CMakeLists.txt should look like this:

set(util_source_files
    calculate.c
    calculate.h
)

add_library(util ${util_source_files})
target_include_directories(util ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_definitions(util PUBLIC GUCCI)

Note target_compile_definitions line, with PUBLIC parameter: it instructs cmake to use -DGUCCI compiler option while compiling util, and all targets that are linked against util.

Also note change in target_include_directories. You placed header file as parameter, but you should place directory instead.

Upvotes: 1

Related Questions