I have a c-project which I cross compile in Windows for STM32.
The build works (with clang and gcc) and runs. I want to add specific compile definitions such as -DFOO=1
to one file such as build_info.c
.
Here is my CMakeLists.txt
file:
cmake_minimum_required(VERSION 3.22)
project(stm32cubemx)
add_library(stm32cubemx INTERFACE)
# Enable CMake support for ASM and C languages
enable_language(C ASM)
target_compile_definitions(stm32cubemx INTERFACE
USE_HAL_DRIVER
STM32L431xx
$<$<CONFIG:Debug>:DEBUG>
)
target_include_directories(stm32cubemx INTERFACE
../../Core/Inc
...
)
target_sources(stm32cubemx INTERFACE
../../Core/Src/main.c
../../Core/Src/build_info.c
...
../../Core/Startup/startup_stm32l431rctx.s
)
target_link_directories(stm32cubemx INTERFACE
)
target_link_libraries(stm32cubemx INTERFACE
)
# Validate that STM32CubeMX code is compatible with C standard
if(CMAKE_C_STANDARD LESS 11)
message(ERROR "Generated code requires C11 or higher")
endif()
which is included by this CMakeLists.txt
file:
cmake_minimum_required(VERSION 3.22)
# Setup compiler settings
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
# Define the build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
endif()
# Set the project name
set(CMAKE_PROJECT_NAME stm32_project)
# Include toolchain file
if(CMAKE_C_COMPILER MATCHES "clang")
message(STATUS "Using Clang toolchain")
include("cmake/clang.cmake")
elseif(CMAKE_C_COMPILER MATCHES "gcc")
message(STATUS "Using GCC toolchain")
include("cmake/gcc-arm-none-eabi.cmake")
else()
message(FATAL_ERROR "Unsupported compiler! Please use GCC or Clang.")
endif()
# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
# Enable CMake support for ASM and C languages
enable_language(C ASM)
# Core project settings
project(${CMAKE_PROJECT_NAME})
message("Build type: " ${CMAKE_BUILD_TYPE})
# Create an executable object type
add_executable(${CMAKE_PROJECT_NAME})
# Add STM32CubeMX generated sources
add_subdirectory(cmake/stm32cubemx)
# Link directories setup
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined library search paths
)
# Add sources to executable
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
# Add user sources here
)
# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined include paths
)
# Add project symbols (macros)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined symbols
)
# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
stm32cubemx
# Add user defined libraries
)
I've tried multiple things:
set_property(SOURCE ../../Core/Src/build_info.c APPEND PROPERTY COMPILE_OPTIONS FOO=1)
set_source_files_properties(../../Core/Src/build_info.c PROPERTIES COMPILE_DEFINITIONS FOO=1)
target_compile_options(stm32cubemx INTERFACE
$<$<MATCH_FILE:../../Core/Src/build_info.c>:-DFOO=1>
)
How do I add a compile definition to one specific file only? Do I have to move the file to a separate folder with a separate CMakeLists.txt
file? Or can I split the sources in groups in the same CMakeLists.txt
file?
I have a c-project which I cross compile in Windows for STM32.
The build works (with clang and gcc) and runs. I want to add specific compile definitions such as -DFOO=1
to one file such as build_info.c
.
Here is my CMakeLists.txt
file:
cmake_minimum_required(VERSION 3.22)
project(stm32cubemx)
add_library(stm32cubemx INTERFACE)
# Enable CMake support for ASM and C languages
enable_language(C ASM)
target_compile_definitions(stm32cubemx INTERFACE
USE_HAL_DRIVER
STM32L431xx
$<$<CONFIG:Debug>:DEBUG>
)
target_include_directories(stm32cubemx INTERFACE
../../Core/Inc
...
)
target_sources(stm32cubemx INTERFACE
../../Core/Src/main.c
../../Core/Src/build_info.c
...
../../Core/Startup/startup_stm32l431rctx.s
)
target_link_directories(stm32cubemx INTERFACE
)
target_link_libraries(stm32cubemx INTERFACE
)
# Validate that STM32CubeMX code is compatible with C standard
if(CMAKE_C_STANDARD LESS 11)
message(ERROR "Generated code requires C11 or higher")
endif()
which is included by this CMakeLists.txt
file:
cmake_minimum_required(VERSION 3.22)
# Setup compiler settings
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
# Define the build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
endif()
# Set the project name
set(CMAKE_PROJECT_NAME stm32_project)
# Include toolchain file
if(CMAKE_C_COMPILER MATCHES "clang")
message(STATUS "Using Clang toolchain")
include("cmake/clang.cmake")
elseif(CMAKE_C_COMPILER MATCHES "gcc")
message(STATUS "Using GCC toolchain")
include("cmake/gcc-arm-none-eabi.cmake")
else()
message(FATAL_ERROR "Unsupported compiler! Please use GCC or Clang.")
endif()
# Enable compile command to ease indexing with e.g. clangd
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
# Enable CMake support for ASM and C languages
enable_language(C ASM)
# Core project settings
project(${CMAKE_PROJECT_NAME})
message("Build type: " ${CMAKE_BUILD_TYPE})
# Create an executable object type
add_executable(${CMAKE_PROJECT_NAME})
# Add STM32CubeMX generated sources
add_subdirectory(cmake/stm32cubemx)
# Link directories setup
target_link_directories(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined library search paths
)
# Add sources to executable
target_sources(${CMAKE_PROJECT_NAME} PRIVATE
# Add user sources here
)
# Add include paths
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined include paths
)
# Add project symbols (macros)
target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE
# Add user defined symbols
)
# Add linked libraries
target_link_libraries(${CMAKE_PROJECT_NAME}
stm32cubemx
# Add user defined libraries
)
I've tried multiple things:
set_property(SOURCE ../../Core/Src/build_info.c APPEND PROPERTY COMPILE_OPTIONS FOO=1)
set_source_files_properties(../../Core/Src/build_info.c PROPERTIES COMPILE_DEFINITIONS FOO=1)
target_compile_options(stm32cubemx INTERFACE
$<$<MATCH_FILE:../../Core/Src/build_info.c>:-DFOO=1>
)
How do I add a compile definition to one specific file only? Do I have to move the file to a separate folder with a separate CMakeLists.txt
file? Or can I split the sources in groups in the same CMakeLists.txt
file?
1 Answer
Reset to default 0I don't have a way to test it (not currently using cmake in my projects at work), but
https://www.google/search?q=cmake+set+compile+option+for+single+source+file&rlz=1C1GCEB_enUS1071US1071&oq=cmake+set+compile+option+for+single+source+file&gs_lcrp=EgZjaHJvbWUyCQgAEEUYORigATIHCAEQIRigATIHCAIQIRigATIHCAMQIRigATIHCAQQIRigATIHCAUQIRifBdIBCDgzNDJqMGo3qAIAsAIA&sourceid=chrome&ie=UTF-8
Says
add_executable(my_target my_file.cpp other_file.cpp)
set_source_files_properties(my_file.cpp PROPERTIES
COMPILE_FLAGS "-Wno-unused-variable")