Skip to main content
ASaho.2
Associate
Aug 7, 2020 5:34 AM UTC
Question

undefined reference to "a c function in main.cpp"

I am getting errors "undefined reference to two c function " screenshot and whole project archived file is attached here.

This project was initially generated by STM32CubeIde as c project then I converted it to c++ using the option "convert to c++ project", main.c was renamed to main.cpp. I don't have any issue with it.

I added the source .c and include .h files appropriately, I still get the error as shown above the two functions.

This topic has been closed for replies.

5 replies

ASaho.2
ASaho.2Author
Associate
Aug 7, 2020 5:38 AM UTC

PFA Screenshot of my project settings and errors:

ASaho.2
ASaho.2Author
Associate
Aug 7, 2020 5:40 AM UTC

PFA Screenshot of my project settings and errors:

ASaho.2
ASaho.2Author
Associate
Aug 7, 2020 5:41 AM UTC

PFA Screenshot of my project settings and errors:

mattias norlander
ST Technical Moderator
Aug 7, 2020 8:48 AM UTC

Hi,

In your "Radio_RF_Control.h", try this instead:

#ifdef __cplusplus
 extern "C" {
 void NRF24L01_One_Time_Configuartion(void);
 void NRF24L01_Schedule_And_Event_Control(void);
 }
 #endif

Your C code needs to be seen like C code at link time by the C++ linker, that is why these directives need to be added.

For further reading maybe check this: https://stackoverflow.com/questions/3789340/combining-c-and-c-how-does-ifdef-cplusplus-work

Side note, when downloading your project I notice that the project structure is slightly broken. Not your fault, this is an Eclipse/CDT bug.

It can be fixed. Look at the project explorer: You have two folders "Suporting_Devices/Radio_Module/NRF24L01" and "Suporting_Devices".

Both of them have exclamation mark on the folder icon...

This issue occurs when you:

  1. Add /folder1/folder2 to your project
  2. Then right-click on the "folder1" --> Resource Configuration --> Exclude from build --> Deselect all.
  3. Right-click on "/folder1/folder2" --> Resource Configuration --> Exclude from build --> Deselect all.

Issue happens when you perform step3. folder2 already considered a source code folder since this property is inherited from its parent.

If you want to fix this. Make back-up copy of Support_Devices on file system. Remove both folders in project explorer. Add Supporting_Devices again into project and only perform step2 above!

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question.
ASaho.2
ASaho.2Author
Associate
Aug 11, 2020 5:32 AM UTC

Thanks you very much Mattias the solution worked well.

I modified my project as per your second suggestion.