How to make library compile its headers

Viewed 157

I have a question:

I'm creating a library like this:

myLibrary.h
class MyLibrary{

public:
    MyLibrary(int);

private:

    int myInt;
}
myLibrary.cpp
#include "myLibrary.h"

MyLibrary::MyLibrary(int i){
    this->myInt = i;
}
main.cpp
#include "myLibrary.h"

int main(){
    MyLibrary library(10);
}

If i now do my Makefile like this:

all:
    g++ -o library.o -c myLibrary.cpp
    g++ -o main.o -c main.cpp

    g++ -o main.out main.o library.o

everything works, but how do I make it, when I provide this library to someone else to use it in his code, he only has to type in his program #include "myLibrary.h" and that's it. So I don't want to make him type all the g++ calls for every c++ file in my library in his makefile, is there a way to include a makefile into an other makefile or something?

I know it might sound like a stupid question, but it would be very gently if you could help me.

1 Answers

You do not make library compile its headers.
Headers are not compiled, source code files are, they include headers.

And you cannot achieve that a user of your library 'only has to type in his program #include "myLibrary.h" and that's it', that is impossible.
As you have probably learned when using any non-standard library (i.e. one not coming with your toolchain/compiler) you have to setup your project so that the linker uses the binary part of the library.
It would, strictly speaking, even be possible to use the library (if linked) WITHOUT any header, if you would declare needed prototypes etc. manually. Unwise, but possible.

Your makefile shows that you are NOT creating a library, you are simply building a program from two source code files, one of them meaningfully having its declarations in a header.

Sorry to say but what you probably are trying to do, creating a library (shared, dll, static, whatever) for reuse, it more complex. I recommend to search for "creating a XXXX library", with XXXX being the kind of library. Explaining the differences and how to create the different kinds is too complex to cover it on side note here and is beyond answering the question as asked.

Please understand that I consider clarifying what I consider your relevant misunderstandings an answer to your question and that the "how-to" is in my opinion a different and elsewhere already answered question.

However, picking up applicable feedback from a comment by user "bloody":
If you are not thinking of the kind of libraries I mentioned above and consider the combination of header file and a file which does not need compiling, then you are practically already done with what you have shown. If you provide the "library.o" file resulting from compiling your "myLibrary.cpp" then the user can use it like you did in your last line of the shown makefile. That could be considered a static library.
(Strictly speaking it does however not really match your description of 'only has to type in his program #include "myLibrary.h" and that's it'.)

Related