KevinOConnor / can2040

Software CAN bus implementation for rp2040 micro-controllers
GNU General Public License v3.0
686 stars 68 forks source link

Add extern "C" {} block for C++ compatibility #37

Closed Nate711 closed 1 year ago

Nate711 commented 1 year ago

I was having linking errors using this library until I realized there was no extern "C" block included. I think it would be a nice feature thanks!

KevinOConnor commented 1 year ago

If you are importing a C header file into a C++ file you can wrap the import in the including file with something like:

extern "C" {
#include "can2040.h"
}

-Kevin

Nate711 commented 1 year ago

Thanks, it's just that I spent a long time trying to figure out the linking error when it was just the extern C issue. If the library comes with the extern C blocks built in to the can2040.h file, then newbies like myself won't have to repeat the same struggle. Thanks!

KevinOConnor commented 1 year ago

We could update to the documentation - #39 .

-Kevin

Nate711 commented 1 year ago

Thanks

chuggafan commented 1 year ago

Would it not be possible to just add:

#ifdef __cplusplus
extern "C" {
#endif
// header file
#ifdef __cplusplus
}
#endif

to the header file as well? This would solve the underlying issue and should not overall pollute any of the code.

KevinOConnor commented 1 year ago

I'm aware that one could use an #ifdef, but would prefer to keep the headers focused on C code for now.

Closing as this should be addressed with the documentation update.

-Kevin