Skip to main content
ASaho.2
Associate
August 7, 2020
Question

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

  • August 7, 2020
  • 5 replies
  • 7606 views

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
August 7, 2020

PFA Screenshot of my project settings and errors:

ASaho.2
ASaho.2Author
Associate
August 7, 2020

PFA Screenshot of my project settings and errors:

ASaho.2
ASaho.2Author
Associate
August 7, 2020

PFA Screenshot of my project settings and errors:

mattias norlander
ST Employee
August 7, 2020

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!

ASaho.2
ASaho.2Author
Associate
August 11, 2020

Thanks you very much Mattias the solution worked well.

I modified my project as per your second suggestion.