How to add source file in STM32CubeIDE?

Here’s how to add source files to an STM32CubeIDE project (the “right” Eclipse way), plus the common gotchas.
Add a new .c/.h file inside CubeIDE
In Project Explorer, expand your project.
Right-click the folder you want (usually Core/Src for
.cand Core/Inc for.h).New → Source File (for
.c) or New → Header File (for.h).Name it (e.g.,
my_driver.c/my_driver.h) → Finish.
✅ CubeIDE will automatically compile any .c file that’s inside the project tree (and not excluded).
Add an existing source file (copy into project)
Method A (recommended): Copy into the project
Right-click Core/Src (or your own folder like
Drivers/MyLib/Src).Import… → General → File System (or just drag & drop).
Select your file(s).
When prompted, choose Copy files into the workspace/project.
Method B: Drag & drop
Drag the
.c/.hinto Core/Src / Core/IncChoose Copy (not “Link”) unless you really want external linking.
Add a whole folder/library (clean structure)
A common structure:
Core/Inc– your headersCore/Src– your sourcesDrivers/MyLib/IncDrivers/MyLib/Src
Steps:
Right-click project → New → Folder (create
Drivers/MyLib/IncandDrivers/MyLib/Src)Import/copy files into those folders.
Add include path (next section).
Add include paths so headers are found
If you put headers somewhere other than Core/Inc, you must add an include path:
Right-click project → Properties
C/C++ Build → Settings
Tool Settings → MCU GCC Compiler → Includes
Add your include folder, e.g.:
../Drivers/MyLib/Inc
Apply and Close
Then rebuild.
Make sure the file is actually being compiled
If the code doesn’t link, check:
Right-click the
.cfile → Resource Configurations → Exclude from Build- Make sure it’s NOT excluded for Debug/Release.
The file extension is correct (
.cgets compiled;.hdoesn’t).
Common “it still won’t work” issues
Wrong folder added to includes: add the folder that contains the header, not the header file itself.
Case-sensitive paths (Linux/macOS):
mylib.h≠MyLib.hName collisions: two files with same function names cause “multiple definition” linker errors.
C++ vs C: if you add
.cpp, you may needextern "C"around STM32 HAL headers.
Quick sanity check
After building, check Console output: you should see your new file being compiled, e.g.:
arm-none-eabi-gcc ... -c my_driver.c



