-
Notifications
You must be signed in to change notification settings - Fork 19
Multi‐file compilation
The IAR C/C++ Compiler offers the possibility for multi-file compiling to gather a file set into a single compilation unit. From the compiler's perspective, the file set must be passed as additional parameters from a single invocation, with the --mfc option:
iccarm file1.c file2.c file3.c --mfc ...
As per CMake 3.28, target_sources() doesn't provide a clear way for specifying file sets for such purpose.
- CMake targets using 2 or more source files.
Consider one of the following approaches for when building a target using multi-file compilation:
Changing the source code to include the file set from a single compilation unit wil effectively mimic the effect provided by --mfc:
/* File: file1.c */
#include "file2.c"
#include "file3.c"
/* ... */
void main() {
/* ... */Sometimes changing the sources might be cumbersome depending on how large the project was partitioned. A workaround for using --mfc from a CMake project could be:
cmake_minimum_required(VERSION 3.20)
project(MyProject)
add_executable(MyProject)
target_sources(MyProject PRIVATE file1.c)
set(MFC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/file2.c ${CMAKE_CURRENT_SOURCE_DIR}/file3.c)
target_compile_options(MyProject PRIVATE ${MFC_FILES} --mfc --discard_unused_publics)
target_link_options(MyProject PRIVATE --semihosting)where the output from cmake --build . --clean-first --verbose would be:
[1/2] /opt/iarsystems/bxarm/arm/bin/iccarm --silent /myproject/file1.c /myproject/file2.c /myproject/file3.c --mfc --discard_unused_publics --dependencies=ns CMakeFiles/myproject.dir/file1.o.d -o CMakeFiles/myproject.dir/file1.o [2/2] : && /opt/iarsystems/bxarm/arm/bin/ilinkarm CMakeFiles/myproject.dir/file1.o --silent --semihosting -o MyProject.elf && :
Tip
According to the User Guide, the option --discard_unused_publics might be helpful in scenarios where --mfc is used within a project-wide scope, where the compiler can have full visibility of the source code from a single compilation unit.
This is the cmake-tutorial wiki. Back to Wiki Home
- IAR Compiler options in a CMake project
- IAR ILINK options in a CMake project
- Language-specific target options
- Selecting build types
- Using Ninja Multi-Config
- Filing a build log
- Multi-file compilation
- Invoking IAR binary utilities
- Use the IAR ELF Tool to convert executable targets to their binary formats
- Using IAR Build Tools with CMake Presets