OSDev.org

The Place to Start for Operating System Developers
It is currently Sat Apr 27, 2024 12:13 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Writing libraries for C++
PostPosted: Wed Jun 04, 2003 1:37 pm 
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++?


Top
  
 
 Post subject: Re:Writing libraries for C++
PostPosted: Wed Jun 04, 2003 2:31 pm 
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.)


Top
  
 
 Post subject: Re:Writing libraries for C++
PostPosted: Wed Jun 04, 2003 2:35 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 2:31 am
Posts: 5964
Location: In a galaxy, far, far away
so in essence, there would be no difference between a C++ library and a C library? ...

_________________
Image May the source be with you.


Top
 Profile  
 
 Post subject: Re:Writing libraries for C++
PostPosted: Fri Jun 06, 2003 10:10 am 
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...


Top
  
 
 Post subject: Re:Writing libraries for C++
PostPosted: Fri Jun 06, 2003 12:49 pm 
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.


Top
  
 
 Post subject: Re:Writing libraries for C++
PostPosted: Fri Jun 13, 2003 4:40 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7614
Location: Germany
Pype.Clicker wrote:
so in essence, there would be no difference between a C++ library and a C library? ...


No, and yes. (Hi Pype. ;-) )

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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], DotBot [Bot], Majestic-12 [Bot] and 34 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group