-
Notifications
You must be signed in to change notification settings - Fork 19
Specifying language standards in CMake projects
This interactive example describes how to specify language standards in a CMake project when using IAR Compilers.
In CMake, it is possible to select in which language standard each target will be produced. This can be performed globally or per-target. As for example:
- Specifying the project's C++ Standard to C++20, globally
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED OFF)
set(CMAKE_CXX_EXTENSIONS ON)- Specifying the same standard, but for a single target
set_target_properties(app-cpp20 PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED OFF
CXX_EXTENSIONS ON)As a rule of thumb, each IAR Compiler conforms to IAR's latest language standard available at the time of their release. However, given the constrained nature of embedded systems development, there are exceptions:
- In C, there is typically C90 (a.k.a. C89) as a backward compatibility option.
- For C++, a dual runtime library stack might be offered as a user choice between IAR DLIB (up to C++14) or IAR Libc++ (for C++17 or later). This design choice provides flexibility between legacy code using DLIB and new applications being designed using the latest standard available.
Availability, naturally, depends on the product version. As an example, let's outline it for Arm:
| Version | C Standards | C++ Standards | C++ Runtime Libs |
|---|---|---|---|
| 10.10.1 | C90/C17 | C++20 | DLIB/Libc++ |
| 9.30.1 | C90/C17 | C++17 | DLIB/Libc++ |
| 8.40.1 | C90/C17 | C++17 | DLIB only |
| 8.32.1 | C90/C11 | C++14 | DLIB only |
The C++ library choice should be made based on the runtime library features in use. Say you want to #include <concepts> in your C++20 source file:
- It will work with
--c++ --libc++in iccarm-10.10.1 or later because it bringsarm/inc/libcpp/concepts. - It will not compile with
--c++ --dlib_config ...since there is no concepts header implemented underarm/inc/cpp/.
CMake will automatically add the --c++ option for all C++ files in the project.
For strict conformance on C++20, CMake v4.4.0 or later will automatically add the --libc++ option.
For C++17, the option --libc++ needs to be manually added to target_compile_options() whenever available or required.
A CMake project example is provided at examples/standards:
| Project files |
|---|
CMakeLists.txt |
main.cpp |
main.c |
This interactive project example shows how you can select a language standard for its specific targets. It was created to demonstrate the behavior of enforcing strict language standard conformance in a CMake project when using the IAR Compilers.
- Build the project with the IAR Compiler a first time.
- Perform the following tasks on
CMakeLists.txt(click to show/hide answers):
TODO 1: Enable strict conformance for the app-cpp20 application
set_target_properties(app-cpp20 PROPERTIES
CXX_STANDARD 20
CXX_STANDARD_REQUIRED ON
CXX_EXTENSIONS ON)Now rebuild the project. Using a legacy compiler with no C++20 capabilities after enforcing the standard will result in a CMake fatal error when configuring the project:
CMake Error in CMakeLists.txt:
Target "app-cpp20" requires the language dialect "CXX20" (with compiler
extensions). But the current compiler "IAR" does not support this, or
CMake does not know the flags to enable it.
This means the compiler version used for building app-cpp20 did not offer C++20 support. Looking forward, the two options from here would be to:
- a) upgrade the compiler to a version supporting C++20 or
- b) use strict conformance only when making use of the language features provided by the compiler currently in use.
TODO 2: Enable strict conformance for the app-c90 application
set_target_properties(app-c90 PROPERTIES
C_STANDARD 90
C_STANDARD_REQUIRED ON
C_EXTENSIONS ON)Now rebuild the project with --verbose. Whenever required by the compiler, CMake will automatically add the --c89 option:
[3/16] [...]/arm/bin/iccarm --silent [...]/main.c --c89 -e --dependencies=ns [...] -o [...]/main.o
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