Writing libraries for C++
Writing libraries for C++
Probably a question more suited for a C++ newsgroup, but... Can anyone point me to somewhere on how to find out how to write libraries (include files) for C++?
Re:Writing libraries for C++
Details probably depend on the compiler you are using, but in general you just write a collection of routines as if you were doing a normal program, but you omit any sort of main(), because a library is not a program. Then you link them together telling the linker to generate a library. Then you link your program with the library and include any necessary headers in the source.
(P.S. This thread should probably be moved to the General Programming forum.)
(P.S. This thread should probably be moved to the General Programming forum.)
- Pype.Clicker
- Member
- Posts: 5964
- Joined: Wed Oct 18, 2006 2:31 am
- Location: In a galaxy, far, far away
- Contact:
Re:Writing libraries for C++
so in essence, there would be no difference between a C++ library and a C library? ...
Re:Writing libraries for C++
Ok, I worded this wrong. I'm preparing to write my own OS, and I'm going to write my ISR's in Assembly. I need to be able to write C header files (i.e. re-write IOSTREAM.H) so they will use either BIOS routines or my ISR's, instead of trying to use the DOS interrupts which won't be there anymore...
Re:Writing libraries for C++
The header files are the least of your worries. What you need to do is write the actual code with header files containing the declarations for the functions in your code. Then compile everything and, instead of linking against the normal C runtime library, link against your new code.
Re:Writing libraries for C++
No, and yes. (Hi Pype. )Pype.Clicker wrote: so in essence, there would be no difference between a C++ library and a C library? ...
In modus operandi, there is no difference between writing a C lib and a C++ lib. Write your code, ommit the int main(), provide the headers (so client code knows what the library does) and the object code (so client code can link).
However, there are differences.
1) C++ runtime / kernel space. Even if you do not include anything in your C++ (i.e., you do not use the standard lib), the compiler will link in several things that are not available in kernel space unless you implement them. new(), for example (the C++ equivalent of malloc()) can throw an exception if no memory is available; so you either have to make sure your new() implementation does not, or you have to provide exception support.
2) name munging. C++ symbols are significantly different from C symbols. (Symbols, you know, the funny things the linker has to bother with ) In C++, the symbols contain type information, namespace information, and some other bits that make them incomprehensible for C linkage (and, usually, the human reader ). However, in C++ you can declare something to have plain C linkage ([tt]extern "C"...[/tt]).
The downside of 2) is that you can't export an OO interface with C linkage since you would lose the crucial type information. With skill, care, and experience you can "wrap" a C linkage layer around your C++ library.
Then again, I am not sure I understood justinkr's problem. He wants to write an <iostream> library that uses your ISR's instead of DOS interrupts... that doesn't exactly sound like a linkage problem?
Every good solution is obvious once you've found it.