Skip to main content

Command Palette

Search for a command to run...

How to add source file in STM32CubeIDE?

Published
2 min read
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

  1. In Project Explorer, expand your project.

  2. Right-click the folder you want (usually Core/Src for .c and Core/Inc for .h).

  3. New → Source File (for .c) or New → Header File (for .h).

  4. 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

  1. Right-click Core/Src (or your own folder like Drivers/MyLib/Src).

  2. Import… → General → File System (or just drag & drop).

  3. Select your file(s).

  4. When prompted, choose Copy files into the workspace/project.

Method B: Drag & drop

  • Drag the .c/.h into Core/Src / Core/Inc

  • Choose Copy (not “Link”) unless you really want external linking.

Add a whole folder/library (clean structure)

A common structure:

  • Core/Inc – your headers

  • Core/Src – your sources

  • Drivers/MyLib/Inc

  • Drivers/MyLib/Src

Steps:

  1. Right-click project → New → Folder (create Drivers/MyLib/Inc and Drivers/MyLib/Src)

  2. Import/copy files into those folders.

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

  1. Right-click project → Properties

  2. C/C++ Build → Settings

  3. Tool Settings → MCU GCC Compiler → Includes

  4. Add your include folder, e.g.:

    • ../Drivers/MyLib/Inc
  5. Apply and Close

Then rebuild.

Make sure the file is actually being compiled

If the code doesn’t link, check:

  • Right-click the .c file → Resource Configurations → Exclude from Build

    • Make sure it’s NOT excluded for Debug/Release.
  • The file extension is correct (.c gets compiled; .h doesn’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.hMyLib.h

  • Name collisions: two files with same function names cause “multiple definition” linker errors.

  • C++ vs C: if you add .cpp, you may need extern "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