Pages: [1]   Go Down
Author Topic: When to use a .C library vs a .CPP library  (Read 390 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
he's not a real doctor
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Connecticut, US
Offline Offline
Edison Member
*
Karma: 2
Posts: 1036
Whatduino
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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:
[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]
« Last Edit: January 23, 2009, 11:25:18 pm by halley » Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
he's not a real doctor
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: