Thursday, July 30, 2009

Static libraries and header files in c++?

ok, i need to make some program, make static library out of it, and put the inteface in header file. i've got couple of classes but only one of them contains the interface.


all classes are in separate .cpp and .h files (what can i say i do java :) - class definitions are in .h and implementation is in .cpp - so do if a class needs another class from other file, what do i include in what (first.h in second.cpp or what?) - i would like .o files to be per class (i understood i join them via ar to static library then).


and what should go to header file with interface? class definition of my interface class, or just some methods like


myclass::method1();

Static libraries and header files in c++?
Keeping the class definition in .h and declaration in .cpp is good. I work as C++/UNIX developer for a larger s/w company and we are doing it the same way. Keeps it clean.





Including headers are just like Java i guess (I don't have much experience with Java).





Ex: A class, B class. B needs to create a object of A. So in the B class .h include the A class .h file


#include "A.h"





I'm not sure what is your development platform (UNIX/Windows). Any compiler creates .obj (.o) files based on the .cpp files. Since you have kept your class in separate files, this would give you .obj per each class. But to link the .o and create the .lib (static lib) classes doesn't need to be on separate files. The linker is intelligent enough to do it.





Interface is totally based on your requirement.


You can write a set of functions and compile and create a .lib (no classes).


If you are using classes and needs to have an entry point for outside users to use it then you need to have a library interface.


Also you can have a collection of classes in a static lib which are not interrelated but in a one container, in this situation you don't need an interface.





Also since you are creating a static lib all the header files are need to be included in any program you are using it.
Reply:"ok, i need to make some program, make static library out of it, and put the inteface in header file."


Accurately said, what you need is the declarations in the header file. For example, let's say you had a function called foobar() in your code somewhere. If you want other programs to use this function, the compiler needs to know it exists. You can't just throw in the library and hope it works; the compiler won't like it. That's why you need to declare the existence of foobar() in the header file.





"class definitions are in .h and implementation is in .cpp"


I'm going to correct your terminology here because it is important. Header files don't take in class definitions. They have class *declarations*. An implementation is the same as saying definition, so class *definitions* are in the cpp file.





"so do if a class needs another class from other file, what do i include in what "


You declare the existence of the classes you use. So let's say you have Class A and Class B, but Class B needs to use Class A. You need to declare in classb.cpp or whatever cpp file it is in: the existence of Class A. You can either do this explicitly: class CMyClassA; or you can just #include the header file for Class A, which contains this declaration.





"i would like .o files to be per class"


Not possible. .o files are per translation unit, which is pretty much per source file. If you have file.cpp, and you put Class A and Class B in file.cpp, how many o files do you get? One. Because you only have file.cpp. if you have file.cpp and file2.cpp, but only Class A, how many o files do you get? Two. Because you have two cpp files. Got it? If you want only one o file per class, put only one class definition in one source file.





"i understood i join them via ar to static library then"


Yes, take all the o files you want a static library for, and use ar to create one static library.





"and what should go to header file with interface? class definition of my interface class, or just some methods like


myclass::method1();"


Whatever needs to be declared (but not defined because you did that in the source file). Look, your compiler says, hey what the heck is Class A? What is Class B? It doesn't know anything about libraries, static libraries, and other code. You need to tell the compiler Class A exists in the declaration. So declare the class, declare the member functions and member variables. But don't write code for them, because you did that already in the source file. And you put that source file (compiled) into the static library.


No comments:

Post a Comment