Question:
how to make your own library in C++ ?
Dj AMC
2008-11-12 12:57:51 UTC
i want to use my own functions in my own library which can be included in other programs,how do u make a library e.g mylibrary.h in c++?
Five answers:
Chris C
2008-11-12 13:09:35 UTC
1) Create the files "mylibrary.h" and "mylibrary.cpp".

2) Only compile the library, and it shouldn't have a "main()" function in it. This step will generate an object file called "myfile.o".

3) Create another file called something like "main.cpp". In main.cpp you need to #include "mylibrary.h".

4) Compile main.cpp and add the library from step 2 to link in (in this case "myfile.o"). If you don't add this library, you'll get a error message like "linker error - can't find function definition", or something like that.
jplatt39
2008-11-12 13:52:46 UTC
Aerivium is correct as far as he goes. Obviously the first thing he mentioned -- you want to compile but not link your routines. Why not? Because you'll be linking them in with the other libraries. In fact, in gcc in linux which I use, you compile them with a command line like:

g++ -o myprogram.o -c myprogram.cc and then use ar as in ar mylib.a myprog.o. That is a static library which will be compiled into the final code. For a dynamic library -- sorry, I haven't done that.



Turbo C used to compile to object files (.obj) when I used them. Those are the same as .o files and you can link them into libraries.



If I were you, I would read the documentation on your compiler carefully.



Here are a couple of tutorials using versions of C I don't know much about.



http://www.hlrs.de/organization/tsc/services/tools/docu/kcc/tutorials/libraries.html



http://docs.sun.com/source/819-3690/Building.Libs.html



http://www.openismus.com/documents/linux/building_libraries/building_libraries.shtml
CatNip
2008-11-12 13:11:28 UTC
Under Linux:



use the ar command to create your archive(lib), e.g.,



$ g++ -c test1.c create test1.o



$ g++ -c test2.c create test2.o



Now add the object files to the archive

$ ar cr libmydemo.a test2.o test1.o





Next step is to compile and link it:



$ g++ -o test3 test3.c -L. -lmydemo



Here, "-L." is used to tell the system that when the compiler searches for library files it should also include the current directory. The name of the library is passed using the -l command option, you call the library without the lib prefix and the .a extension..
heiselman
2016-10-25 05:44:00 UTC
Library isn't some thing yet .c/.cpp archives with header archives. case in point, you've the want to make a maths library: Create following archives: maths.h: placed all function declarations, or in C++, placed type declarations in straight forward words maths.c/.cpp: placed all definitions right here Thats it, your library is waiting. to apply it, only contain its .h report on your application, and link .cpp report. Gorav
Aerivium
2008-11-12 13:04:35 UTC
make files called mylibrary.h and mylibrary.c



mylibrary.h just has prototype methods with no bodies



eg: void doSomeStuff(int* stuff);



mylibrary.c has all of these functions again but with their bodies



eg: void doSomeStuff(int* stuff){

printf("%i",stuff);

}



you then include the .h file when you want to use it



once you've compiled it once you will get a mylibrary.o, this and the .h file are all you need to use it, the .c is only needed to change it and recompile the .o file


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...