Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 3 months ago.
how to define a function in another .c file?
I have main.c and test.c and I can't access a function in test.c by main.c
I get this error: Error: Undefined symbol blah() (referred from main.NUCLEO_F091RC.o).
main.c has: ... extern void blah( void );
void blahblah( void ) { blah(); }
test.c has:
void blah( void ) { int a=5; int b=5*5; }
Both files are within the project folder, and not within any subfolder.
1 Answer
8 years, 3 months ago.
Hello Dough,
I have tested your code and it compiles smoothly with no errors. Please check the file names in your project because when I rename main.c to main.cpp then I get the same error as you have reported. It's because C++ is using name mangling. The solution is
- tell the compiler that
blah()
is not a C++ but C function as follows:
main.cpp
extern "C" void blah(void); int main() { blah(); }
- or rename also test.c to test.cpp and then
blah()
will be compiled as a C++ function.