Skip to main content
DiBosco
Senior
May 27, 2021
Question

How to add C source files to a project in a Cube IDE project

  • May 27, 2021
  • 14 replies
  • 47700 views

I'm no noob to the STM32, but I am to Cube IDE.

I've added a new directory for my user code to separate it as much as possible from this dog's dinner of code that Cube auto config creates.

I've created a new function, it's all correct, its prototype is in its header file, yet the compiler is telling me that there's an undefined reference to the function I'm trying to call from the Cube generated code.

The code is right, so there must be something wrong with the way I've created the new files and that the IDE hasn't automatically added them to the project.

Include directories are set up in the project. If I control click on the function the compiler is complaining about not seeing, the IDE takes me to the function.

I just created a new directory in Eclipse, then created new files by right clicking on the new directory and selecting file from template, then wrote my code. Is there a non-obvious step you have to take to somehow add the actual file to the project? You'd expect the IDE just to add it to the project, but then this is Eclipse and anything's possible with Eclipse.

Thanks =)

This topic has been closed for replies.

14 replies

DiBosco
DiBoscoAuthor
Senior
May 27, 2021

OK, found it. For someone else struggling with the craziness of this:

You have to go to Project Properties and add the folder just like you do with the include paths. Obv. :see_no_evil_monkey:

NRatz.2
Associate II
September 26, 2022

Still don't see how you did it. In Project > Properties, I can see how to add an include file or path, but not a C++ file or path. How did you do it? I, on the other hand, am new to STM32Cube

Paul1
Senior III
May 27, 2021

Wishlist: A way to add only select source files from some folders, where the bulk of the files you don't need.

Tilen MAJERLE
ST Employee
May 27, 2021

You can do this by drag&drop feature. In file browser select files and drop them to source folder in CubeIDE project.

Associate III
September 7, 2022

Drag&drop doesn't seem to work for all folders :(

My project has directories that are shared between the two projects for two CPU cores of the MCU, and the shared directories are outside the two project directory structures. Neither drag&drop nor Refresh seem to work there, specifically the the bottom-level directories (drag&drop seems to work for higher-level shared directories, but Refresh doesn't pick up the new .c files).

Paul1
Senior III
May 27, 2021

Thank you Tilen

MConi.1
Visitor II
May 2, 2023

I followed your solution and it still reports an `undefined reference to function`. In Core, I've created a Cpp folder, added my header file with an extern void declaration, and a source file with the void definition. I've obviously included the header file in the source file, and in the main.c file, but calling the void from the main function still gives an undefined reference function. Using 1.12.1 version.

Rim LANDOLSI
ST Employee
May 2, 2023

Hello @MConi.1​ and welcome to the community,

It would be better to post this issue using CubeIDE version 1.12.1 in a new community thread.

Basically you can place source file in any source folder. You can use existing folders or create a new one.

Header files have to be placed inside an include folder that is in the include path (e.g. 

Core/Inc) or you have to tell the compiler that a specific file should be included. If you want to create a new folder for your header files you have to add the folder or files inside this folder to the include path via:

Project -> Properties -> C/C++ Build -> Tool Setting (tab) -> MCU GCC Compiler or MCU G++ Compiler -> Include paths

Thanks,

Rim

Visitor II
November 22, 2023

Not worked. " undefined reference to `delay_ms'" ( this delay_ms() function is defined in delay.h file , I have added to the project include path.

Graduate II
May 2, 2023

Please note also that you must have blue C mark on the C source folder, otherwise source files are not found/compiled. You can change your folder include/exclude -property by select folder -> right click -> properties -> C/C++ build -> Exclude resource from build

Associate III
October 12, 2023

Thank you, @JTP1 . Learning how to change the folder include/exclude property was very helpful. I was getting pretty tired of reading the CubeIDE manual to figure out how to do this.

KBéla.1
Associate III
March 8, 2024

Create Source Folder for filesystem where source files are available, instead of simple folder. See attachement.

Right click on project - New - Source Folder

And now the compiler can find the source files in Source Folder

Visitor II
March 11, 2024

I've been struggling with setting up a USB audio device for the past week or so.. A few hours ago I found a reference to document UM1734 about working with the USB device library. I followed the instructions from paragraph 6.4.3, how to use the USB Audio class driver. It specifies to put a call to USBD_AUDIO_Init() in the setup. I did that but it turned out the library files were not even added to my project after setting up the device and generating code. (After selecting USB_OTG_FS under Connectivity and USB_Device_M7 under Middleware and setting it up to use the audio device class).

So I looked up which files I need to add and added them to my Src and Inc folders, but it turns out the USBD_AUDIO_Init() function was not even defined in the header file. So I added it, but when building, I keep getting the same error.

I looked at all the previous suggestions in this thread but none of them helped with this issue.. Building the code still reports an undefined reference to USBD_AUDIO_Init(), which is clearly not undefined as you can see in the screenshot. The files and folders are not excluded from build and as you can see in the build log, building them works fine. I see in the file explorer that the object file exists and it's listed in the subdir.mk file as well.

It would be really nice if I could just find some instructions that actually work without an advanced course in USB protocols and such. All I wanted to do was test I2S input data from an ADC chip on a custom H503 board, but it seems it's far from trivial to even get this to work on a nucleo H755 (haven't tried any other Nucleo or Disco boards yet)..