How to include a file in an stm32cubeide project?
In STM32CubeIDE, “including a file” usually means adding it to the project so it gets built, and/or making headers discoverable by the compiler. Here are the common cases.
1) Add an existing .c/.h file into your project (copy into project)
-
In Project Explorer, choose the target folder:
-
C files →
Core/Src -
Header files →
Core/Inc
-
-
Right-click the folder → Import…
-
Choose General → File System → Next
-
Browse to your file(s) → check them
-
Make sure “Copy files into project” is enabled
-
Finish
Then in your code:
2) Add a new source/header file (create inside the project)
-
Right-click
Core/Src→ New → Source File -
Name it
myfile.c→ Finish -
Right-click
Core/Inc→ New → Header File -
Name it
myfile.h→ Finish
3) Use a file that lives outside the project (link, don’t copy)
Useful if you share code across multiple projects.
-
Right-click your target folder (e.g.,
Core/Src) → Import… -
General → File System → Next
-
Select the external file
-
Instead of copying, use the option like “Create links in workspace” / “Link to files” (wording can vary)
-
Finish
⚠️ If that external path moves, the build breaks—so prefer relative paths or a stable shared folder.
4) If #include "xxx.h" fails: add include paths
If the header is not under Core/Inc (or is in a custom folder), add the folder to the compiler include search path:
-
Project → Properties
-
C/C++ General → Paths and Symbols → Includes
-
Select language: GNU C
-
Add your folder (e.g.,
Middlewares/MyLib/Inc)
-
-
Apply and Close
Alternative place (also common):
-
C/C++ Build → Settings → Tool Settings → MCU GCC Compiler → Includes
5) If the .c file exists but isn’t compiling: check “Exclude from build”
-
Right-click the file → Resource Configurations → Exclude from Build…
-
Make sure it’s NOT excluded for your active configuration (Debug/Release)
6) If you’re “including” a prebuilt library (.a)
-
Put the
.asomewhere in the project (e.g.,Middlewares/MyLib/Lib) -
Project → Properties → C/C++ Build → Settings
-
MCU GCC Linker → Libraries
-
Add library search path: the folder containing the
.a -
Add the library name (usually without
libprefix and without.a)
-
.jpg)
评论
发表评论