How to add source files / browse path to compilation

Hi, I have the following directory structure.
I can include files from /utils/segger_rtt/RTT no problem but I cannot find out how to add the source files to the compiler

I have tried to use the /lib directory, but it doesn’t work either, platformio doesn’t recognize files inside there. Also, I want to use my own project structure.

See the attached screenshot for more detail
Please let me know how to do this

All to-be-compiled files must be under src/ or in a library under lib/. The position of your utils folder is simply wrong.
Also, in your build_flags, don’t use absolute paths. These paths can be relative with respect to the project folder. lib_extra_dirs is used wrongly in your project, since this is not a library.

You might have additional problems with C/C++ linkage of your code. If you want to call a function from any of these C files, make sure there is an extern "C" declaration in the header surrounding the function declarations.



Hi, thanks for the help,

I have tried moving the files to the /lib folder. Also renamed the files to .hpp and .cpp to avoid C/C++ linkage issues

The .hpp files are included, but the source files are not found.
What is the problem now?

Still wrong folder structure. You have lib/utils/segger_rtt/ and the two subfolders with sources. As the README says structure must be something like

|  |
|  |--Bar
|  |  |--docs
|  |  |--examples
|  |  |--src
|  |     |- Bar.c
|  |     |- Bar.h
|  |  |- library.json (optional, custom build options, etc)
|  |
|  |--Foo
|  |  |- Foo.c
|  |  |- Foo.h
|  |

That might have side effects you are unaware of, I would not do that. Especiallly the rest of the C files might have problems now calling into C++ code.

Just structure it like this

 - segger_rtt/ 
   - src/
     - SEGGER_RTT_Conf.h
     - SEGGER_RTT_printf.c
     - SEGGER_RTT.c
     - SEGGER_RTT.h
     - RTT_Syscalls_GCC.c

(remove the _IAR and _KEIL files, they are ment for another compiler).

In your main.cpp file just do

extern "C" {
    #include <SEGGER_RTT.h>

If PIO doesn’t automatically recognize that the library is to be used, add a lib_deps = segger_rtt line in the platformio.ini. WIth this structure, no extra ´-I` flags are needed also.

1 Like