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)

  1. In Project Explorer, choose the target folder:

    • C files → Core/Src

    • Header files → Core/Inc

  2. Right-click the folder → Import…

  3. Choose General → File SystemNext

  4. Browse to your file(s) → check them

  5. Make sure “Copy files into project” is enabled

  6. Finish

Then in your code:

#include "myfile.h"

2) Add a new source/header file (create inside the project)

  1. Right-click Core/SrcNew → Source File

  2. Name it myfile.cFinish

  3. Right-click Core/IncNew → Header File

  4. Name it myfile.hFinish


3) Use a file that lives outside the project (link, don’t copy)

Useful if you share code across multiple projects.

  1. Right-click your target folder (e.g., Core/Src) → Import…

  2. General → File SystemNext

  3. Select the external file

  4. Instead of copying, use the option like “Create links in workspace” / “Link to files” (wording can vary)

  5. 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:

  1. Project → Properties

  2. C/C++ General → Paths and Symbols → Includes

    • Select language: GNU C

    • Add your folder (e.g., Middlewares/MyLib/Inc)

  3. 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”

  1. Right-click the file → Resource Configurations → Exclude from Build…

  2. Make sure it’s NOT excluded for your active configuration (Debug/Release)


6) If you’re “including” a prebuilt library (.a)

  1. Put the .a somewhere in the project (e.g., Middlewares/MyLib/Lib)

  2. Project → Properties → C/C++ Build → Settings

  3. MCU GCC Linker → Libraries

    • Add library search path: the folder containing the .a

    • Add the library name (usually without lib prefix and without .a)

评论

此博客中的热门博文

Detailed Explanation of STM32 HAL Library Clock System

How do you set up ADC (Analog-to-Digital Converter) in STM32?

How To Connect Stm32 To PC?