Go Down

Topic: When to use a .C library vs a .CPP library (Read 450 times) previous topic - next topic

drspectro

I am copying the wiring_serial library for testing.  I want to create a new "test_serial" with my mods.  Hopefully that avoids the risks of continuing to edit the core code.  

My question is, HardwareSerial.cpp appears to simply call "wiring.serial.c"  presumably to put a C++ face on it.   Is there any reason I cannot make my new library a .CPP to start with?  It looks like you can hook interrupts in .CPP.   Or for that matter is there any problem with creating a .C library and distributing it?

halley

#1
Jan 24, 2009, 05:22 am Last Edit: Jan 24, 2009, 05:25 am by halley Reason: 1

Feel free to make libraries written in C++.  I think the existing libraries don't go all out and make it all C++ so that the syntax is slightly less confusing for newcomers or C programmers, but C++ can really serve the needs of small programs just as well as C.

If you're going to make C library functions usable from a user's C++ sketch, you just need to ensure that your header file is explicit about its C nature.

MyLibraryWrittenInC.h
Code: [Select]

[glow]#ifdef __cplusplus
extern C {
#endif[/glow]

/* prototypes for functions implemented in C */
int myCfunction(float, char, int);

[glow]#ifdef __cplusplus
} // extern C
#endif[/glow]

drspectro

Thanks - thats just what I needed.  I didnt understand th C header.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy