I want to add a custom .h file,but But there are some strange problems.

To facilitate project management,I always I often create some files myself, but when I add .c and .h file in Clion and this project using espressif32 platform, I meet some question.My steps are as follows:

  • I created a folder under the project path named USER and this path includes oledself.cpp and oledself.h files.
    in oledself.cpp and oledself.h I define a function.
  • I created a file named CMakeListsUser.txt in the project directory and modified its contents as follows
  • I add the oledself.h file in the main.cpp and call oled_init() in the main function.
    Then the following error occurred,
Compiling .pio\build\pico32\src\main.cpp.o
src\main.cpp:5:22: fatal error: oledself.h: No such file or directory

* Looking for oledself.h dependency? Check our library registry!
* CLI  > platformio lib search "header:oledself.h"
* Web  > https://platformio.org/lib/search?query=header:oledself.h

compilation terminated.
*** [.pio\build\pico32\src\main.cpp.o] Error 1
========================== [FAILED] Took 4.51 seconds ==========================
CMakeFiles\Debug.dir\build.make:56: recipe for target 'CMakeFiles/Debug' failed
CMakeFiles\Makefile2:105: recipe for target 'CMakeFiles/Debug.dir/all' failed
CMakeFiles\Makefile2:112: recipe for target 'CMakeFiles/Debug.dir/rule' failed
Makefile:130: recipe for target 'Debug' failed
mingw32-make.exe[3]: *** [CMakeFiles/Debug] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/Debug.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Debug.dir/rule] Error 2
mingw32-make.exe: *** [Debug] Error 2

It is worth noting that I can open the oledself.h and oledself.c file in main.cpp.
I want to know where is the problem. Thank you.

A folder structure like that is not supported. Place the USER folder in libs/. No CMake modifications needed.

Thank you.It means the platformIO don’t supported the folder structure? I must make all file into libs/.?

Either src/ or in a folder in lib/. (Project-)External libraries can also be added via lib_deps.

Thank you,but I want to understand why we must do this, I want to manage my projects at will, Should I attempt to revise the CMakeLists.txtfile?

Well ESP-IDF is special because it uses the CMake system on top of the PlatformIO’s regular standardized build system (where your sources are in src/ or lib/`), but if you have a CMakeLists.txt that builds sources outside of this folder, it should also work in PlatformIO.

Sorry, I don’t quite understand what you mean, does this mean that for ESP32 project I can’t create a folder like USER by myself, but for other projects like drduino nuo project I can do it? Thanks again for your patient answer.

To add, I place the USER folder in libs/. as you said, but this error is still prompted.
In addition, as shown in the figure, although the third-party library ArduinoJson I introduced prompted that the file could not be found, it did not report an error during construction. Why?

Please see README, you need to create a new folder in lib/ for your library (e.g. name “user”) and put the .cpp and .h files in that folder. Also don’t forget to do a tools → platformio → re-init in CLion after that to update Intellisense.

No the reverse. For an Arduino Uno project you’re bound to PlatformIO folder structure, for ESP32 + ESP-IDF you have the CMakeLists.txt to possibly do things like adding sources outside of PlatformIO’s regular source folder