Build options

build_flags

These flags/options control preprocessing, compilation, assembly and linking processes:

Format Scope Description
-D name CPPDEFINES Predefine name as a macro, with definition 1.
-D name=definition CPPDEFINES The contents of definition are tokenized and processed as if they appeared during translation phase three in a #define directive.
-U name CPPDEFINES Cancel any previous definition of name, either built in or provided with a -D option.
-Wp,option CPPFLAGS Bypass the compiler driver and pass option directly through to the preprocessor
-Wall CCFLAGS Turns on all optional warnings which are desirable for normal code.
-Werror CCFLAGS Make all warnings into hard errors. Source code which triggers warnings will be rejected.
-w CCFLAGS Suppress all warnings, including those which GNU CPP issues by default.
-include file CCFLAGS Process file as if #include "file" appeared as the first line of the primary source file.
-Idir CPPPATH Add the directory dir to the list of directories to be searched for header files.
-Wa,option ASFLAGS, CCFLAGS Pass option as an option to the assembler. If option contains commas, it is split into multiple options at the commas.
-Wl,option LINKFLAGS Pass option as an option to the linker. If option contains commas, it is split into multiple options at the commas.
-llibrary LIBS Search the library named library when linking
-Ldir LIBPATH Add directory dir to the list of directories to be searched for -l.

This option can be set by global environment variable PLATFORMIO_BUILD_FLAGS.

Example:

[env:specific_defines]
build_flags = -DFOO -DBAR=1 -DFLOAT_VALUE=1.23457e+07

[env:string_defines]
build_flags = '-DHELLO="World!"' '-DWIFI_PASS="My password"'

[env:specific_inclibs]
build_flags = -I/opt/include -L/opt/lib -lfoo

[env:specific_ld_script]
build_flags = -Wl,-T/path/to/ld_script.ld

Dynamic build flags

PlatformIO Core allows to run external command/script which outputs build flags. PIO will automatically parse the output and append flags to a build environment. You can use any shell or programming language.

This external command will be called on each platformio run command before building/uploading process.

Use Cases:

  • Macro with the latest VCS revision/tag “on-the-fly”
  • Generate dynamic headers (*.h)
  • Process media content before generating SPIFFS image
  • Make some changes to source code or related libraries

Note

If you need more advanced control and would like to apply changes to PIO Build System environment, please refer to Advanced options and use Pre/Post Extra Scripts.

Example:

[env:generate_flags_with_external_command]
build_flags = !cmd_or_path_to_script

Use Case: Get the latest GIT revision “on-the-fly”

Unix

[env:git_revision_macro]
build_flags = !echo "-DPIO_SRC_REV="$(git rev-parse HEAD)

Windows

You need to create a separate file named print_git_rev.bat and place it near platformio.ini.

platformio.ini:

[env:git_revision_macro]
build_flags = !print_git_rev.bat

print_git_rev.bat:

@echo off
FOR /F "tokens=1 delims=" %%A in ('git rev-parse HEAD') do echo -DPIO_SRC_REV=%%A

For more detailed information about available flags/options go to:

src_build_flags

An option src_build_flags has the same behavior like build_flags but will be applied only for the project source code from src_dir directory.

This option can be set by global environment variable PLATFORMIO_SRC_BUILD_FLAGS.

build_unflags

Remove base/initial flags which were set by development platform.

[env:unflags]
build_unflags = -Os -std=gnu++11
build_flags = -O2

src_filter

This option allows to specify which source files should be included/excluded from build process. Filter supports 2 templates:

  • +<PATH> include template
  • -<PATH> exclude template

PATH MAST BE related from src_dir. All patterns will be applied in theirs order. GLOB Patterns are allowed.

By default, src_filter is predefined to +<*> -<.git/> -<svn/> -<example/> -<examples/> -<test/> -<tests/>, that means “includes ALL files, then exclude .git and svn repository folders, example ... folder.

This option can be set by global environment variable PLATFORMIO_SRC_FILTER.