diff --git a/add_include.py b/add_include.py
new file mode 100644
index 0000000000000000000000000000000000000000..f7ae02f8fc33b0c50ad01d84ffd85401744dd1c2
--- /dev/null
+++ b/add_include.py
@@ -0,0 +1,5 @@
+from os.path import join
+Import("env", "projenv")
+# propagate to all construction environments
+for e in env, projenv, DefaultEnvironment():
+   e.Append(CCFLAGS=[("-I", join("examples", env.subst("$PIOENV")))])
diff --git a/platformio.ini b/platformio.ini
index 08f37a185dd72f688eeabf1d8652605847b54da1..2da7e046764f462fbbd88db22cbd698f9bd912c4 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -15,7 +15,9 @@ extends = fun_base
 ; for examples that use ch32v003fun as their base
 [fun_base]
 board_build.ldscript = ch32v003fun/ch32v003fun.ld
-build_flags = -flto -Ich32v003fun -I/usr/include/newlib -lgcc -Iextralibs -I"examples/$PIOENV"
+build_flags = -flto -Ich32v003fun -I/usr/include/newlib -lgcc -Iextralibs
+; dynamically add "examples/$PIOENV" to the include path
+extra_scripts = add_include.py
 build_src_filter = +<ch32v003fun>
 extra_libs_srcs = +<extralibs>