PlatformIO Community

Include header file from /include folder via build_flags

Hello,

I am quite new to PlatformIO and a beginner/basic programmer and currently writing an Arduino - project.
Even after reaseaching i could not find a understandable or working solution for my problem.

I have a main.cpp file and want to include a header file into it. --> Therefore i placed it (header.h and header.cpp) into the /include folder of my project (since it is made to put header-files there) and tried to include it via #include “header.h” in the main.cpp file.
When building, I get the error “undefined reference to” my fuction in the header.h file.

After reaseaching i found out, that the include folder is not gobal and my main file cannot “see” the header but with the build-option “build_flags” in the platformio.ini file, i can include the folder.

But i simply cannot get it to work, since i don’t know what to type exactly.
The syntax should look something like this as far i found out.

[something here]
build_flags = -I include

What do i put in the brackets [] ?
Do i put in a path instead of include? Is this then computer specific?

Thanks for the help in advance.

Don’t put neither main.cpp nor header.cpp into include, put them into src. Only headerfiles belong into include. Then you don’t need

build_flags = -I include

Please see also Use headers give me an error

2 Likes

Files in include/ will not be compiled. Only place header .h files there, not implementation (.cpp/.c) files there, but in source.

Having the declaration of a function but not the implementation (stemming from the .cpp file), will give you

so that’s right from the compiler’s side.

Files in src/ are also automatically in the include path so you can also just put all files into src/.

Or, if the code is fully self contained and indepentend from everything else, consider creating a library, aka creating a new folder in the lib/ folder of the project, say myLib and putting the source code files (.h, .cpp, …) in that folder.

1 Like

Thank you very much.
I will simply store it in the /src than.

Have a nice day!

-Marked it as solution, that means topic closed for me.

Best wishes!