Writing a library in c++What's involved
Page 1 of 1
12 Replies - 1600 Views - Last Post: 23 April 2008 - 07:36 PM
#1 Guest_Whizzy*
Writing a library in c++
Posted 22 April 2008 - 04:52 PM
From start to finish, what would be involved in writing a library?
First, I'd have to get my functions together.... then.... ???
Replies To: Writing a library in c++
#2
Re: Writing a library in c++
Posted 22 April 2008 - 05:28 PM
#3 Guest_Whizzy*
Re: Writing a library in c++
Posted 22 April 2008 - 05:32 PM
Amadeus, on 22 Apr, 2008 - 05:28 PM, said:
implement a single file that contains a variety of functions that you often use that you can include in all my programs?
#4
Re: Writing a library in c++
Posted 22 April 2008 - 05:38 PM
Here's an example:
http://www.fredosaur...iple-files.html
#5
Re: Writing a library in c++
Posted 22 April 2008 - 08:00 PM
On Windows, LIB.EXE is the library management tool.
Say you want to make a library called Fooey.LIB. In it is code from FOO.CPP and BAR.CPP. You'd compile FOO.CPP into FOO.OBJ, and compile BAR.CPP into BAR.OBJ. You can then take FOO.OBJ and BAR.OBJ and put them into Fooey.LIB using LIB.EXE to do it.
The code in FOO.CPP and BAR.CPP probably needs a header file to declare its classes, data, and functions. So, sure: you should also have FOOEY.H, or maybe you just give FOO.H or BAR.H. It's kind of arbitrary, but you do want to end up working towards the convenience of your library's users.
If you want to write Program.CPP, it will build to Program.OBJ. Then, you tell the linker to combine Program.OBJ and Fooey.LIB to produce Program.EXE.
I hope that helps.
#6 Guest_Whizzy*
Re: Writing a library in c++
Posted 22 April 2008 - 08:33 PM
mikeblas, on 22 Apr, 2008 - 08:00 PM, said:
Thank you for your input, It was very informative, and I'm sure someone will find a good use for it. However, It's a tad over my head. Amadeus knows the project I'm working on, my skill (ROFL) level and he knew exactly what I'm looking into doing. I may have worded it incorrectly, and for that, I apologize. But he clipped me short for a reason, being he put me right where I needed to be, at this time in my C++ learning devlopment. I do thank you however. Your time, thoughts, and advice were most appreciated.
and
Thank you Daddy Amadeus...
This post has been edited by Whizzy: 22 April 2008 - 08:36 PM
#7
Re: Writing a library in c++
Posted 22 April 2008 - 09:44 PM
Given that additional information, though, I should change my answer. Perhaps the most important thing to do when writing a library is to decide why you want to write a library. What're your goals?
#8
Re: Writing a library in c++
Posted 23 April 2008 - 05:21 AM
#9
Re: Writing a library in c++
Posted 23 April 2008 - 06:19 AM
Amadeus, on 23 Apr, 2008 - 05:21 AM, said:
Oh, I see. I guess you're assuming that "single file" is a *.CPP file, not a *.LIB file. To me, *.LIB static library files are more common than both a frequently compiled *.CPP file and a *.DLL file. After all, not all platforms support *.DLL files, though almost all platforms support static libraries.
#10
Re: Writing a library in c++
Posted 23 April 2008 - 06:25 AM
#11
Re: Writing a library in c++
Posted 23 April 2008 - 06:53 AM
Can I jump in and leach from your question. In the initial response *.DLL extension was mentioned. Can you explain the process that might go into producing a DLL file for this problem? i.e a DLL file that can be used to hold commonly used functions.
thanks,
#12
Re: Writing a library in c++
Posted 23 April 2008 - 07:24 AM
http://logix4u.net/P..._with_VC++.html
http://www.codeproje...eginnerdll.aspx
http://www.flipcode....sing_DLLs.shtml
#13
Re: Writing a library in c++
Posted 23 April 2008 - 07:36 PM
foohoo, on 23 Apr, 2008 - 06:53 AM, said:
Can I jump in and leach from your question. In the initial response *.DLL extension was mentioned. Can you explain the process that might go into producing a DLL file for this problem? i.e a DLL file that can be used to hold commonly used functions.
thanks,
In Windows, a DLL is actually an executable image, so it's created by the linker instead of the librarian. You can link your OBJ files into the DLL, then tell the linker you're building a DLL by giving the linker the /DLL option on its command line.
You'll want to define a DllMain() function in one of your modules in your DLL, must the same way that you define a main() or WinMain() entry point. DllMain() is called by Windows when the DLL loads, when it unloads, and when threads attach or detach to the DLL. This provides an opportunity to do some initialization and shutdown.
A static library automatically exports all of its public symbols. Any public symbol in any OBJ within the static library can be referenced by a consumer of the library. DLLs don't quite work like that. They require the library developer to individually export each function explicitly. There's a couple of different ways to do this; either with a module definition file, or with special non-standard keywords implemented by your compiler.
Exporting C++ classes is very difficult, as the decorated names used by the compiler for special functions must be used to export the implementation name of the function. The compiler might also generate special functions (like destructors or constructors, for example) that the developer can't even see in their source directly. Further, it's necessary to keep these names ordered in a stable way in order to keep the consumers of the DLL working between builds of the DLL.
Another confusing feature of DLLs is that they also create a static library. If you build FOO.DLL, you also get FOO.LIB. The FOO.LIB static library is typically used by the consumer of the DLL to automatically load and call the entry points in the DLL. Without the static library, the consumer must use the LoadLibrary() and GetProcAddress() APIs to find each function entry point ... and then cast to a function poointer, pass the appropriate arguments, and make the call. Obviously, this is very cumbersome.
I hope that helps. If you have any questions about the process, please let me know.
|
|

New Topic/Question
Reply
MultiQuote








|