cancel
Showing results for 
Search instead for 
Did you mean: 

How to add .cpp and .h files to a project without undefined error?

EMurp.1
Associate II

Something weird is going on with my STM32Cube project building. I have a fresh project created from the template for the STM32H745I-DISCO. In the Core/Inc folder I created a foo.h header file containing a method. In the Core/Src folder, I wrote the implementation of that method in foo.cpp. I added the header and source files to the build path, but when I try to call the method in foo.h in main, I get "undefined reference to method". In the debug files, the foo.cpp gets compiled to a .o file, leading me to think it is a linker issue.

5 REPLIES 5

>>leading me to think it is a linker issue.

Yeah, most times it will be apparent whether the compiler or linker is complaining.

The actual build log, and specific error details might help gain some perspective here.

If you include a .H from a .C file it might not get the name-mangling done, but you indicate you're doing a .CPP file.

Perhaps you can inspect the object with objcopy or fromelf type tools to get symbols and disassemblies.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@Community member​ "The actual build log, and specific error details might help gain some perspective here"

The "actual build log" will be found in the 'Console' window:

0693W00000JOmOPQA1.png 

"If you include a .H from a .C file it might not get the name-mangling done"

+1

Check that it is being compiled as C++ - not C

Nikita91
Lead II

You "call the method in foo.h in main". Main is C or C++ ?

EMurp.1
Associate II

Main is a C file. I switched it to a .CPP and it compiles now.

0693W000008y9fZQAQ.png