Included file compiles without errors, but it's functions are inaccessible?

When compiling a test project, I get this error message:

C:\Program Files (x86)\Arduino/DMP2.ino:12: undefined reference to `mpu_init(int_param_s*)''

The DMP2.ino and inv_mpu.h resides inside D:\Documents\Arduino\DMP2

mpu_init resides inside the inv_mpu.c, and is included in the DMP2.ino like this:

#include "inv_mpu.h"

Why is this happening? I’ve added the whole project as an attachment.

DMP2.zip (40.1 KB)

#include "inv_mpu.h"

Hi, this library should be in a folder called inv_mpu in the library folder.

Please post your entire sketch, so we can see what is causing your errors.

Thanks…

Tom… :slight_smile:

TomGeorge: ```

include "inv_mpu.h"





Hi, this library should be in a folder called inv_mpu in the library folder.

Please post your entire sketch, so we can see what is causing your errors.

Thanks..

Tom.......... :)

I thought it was possible to include all files residing inside the project directory? The whole project directory is uploaded alongside the original post, so you can download it. The contents of this .zip file is residing inside D:\Documents\Arduino\DMP2\

I'll try moving the files into the "libraries" folder.

I tried moving the files into the "libraries" folder under "inv_mpu", but the same error appears. Might it be that it doesn't handle ANSI C code?

You need to wrap your C functions in code that tells C++ that they're just C, when defining or using them in C++ files (incuding .ino files.) Otherwise C++ will do name mangling to implement possible polymorphism.

Add the following to the top of inv_mpu.h:

#ifdef __cplusplus
extern "C" {
#endif

And put this at the bottom. (these are very "standard" constructs; you can look up details in the web.)

#ifdef __cplusplus
}
#endif

westfw: You need to wrap your C functions in code that tells C++ that they're just C, when defining or using them in C++ files (incuding .ino files.) Otherwise C++ will do name mangling to implement possible polymorphism.

Add the following to the top of inv_mpu.h:

#ifdef __cplusplus
extern "C" {
#endif

And put this at the bottom. (these are very "standard" constructs; you can look up details in the web.)

#ifdef __cplusplus
}
#endif

Thank you for pointing this out! It worked very well! I've been programming in dozens of languages for 8 years, but this is the first time I'm ever doing bare metal programming. It's really a completely new world. Having a completely new perspective on things, I can see clear disadvantages and advantages of how bare metal is done.

Again, thank you for taking your time helping me!

This particular construct gets used whenever you have some sort of C library that you need to call from C++ code, not just "bare metal" programming. (of course, in the non-bare-metal world you tend to get new C++ libraries instead.)