2

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.

Max Kofler
  • 86
  • 4
  • In a way you created a legit static library `library.o`. You can deliver this along with its header file to anyone and they are able to link to it (they still need to know how to). Their `makefile` then at least no longer requires this line `g++ -o library.o -c myLibrary.cpp` because you already compiled it. – bloody Jan 08 '21 at 18:10
  • @bloody I just discovered that you comment "moved" here. I accept it, actually have incorporated it into my answer. Are you OK with that? – Yunnosch Jan 08 '21 at 18:14
  • @Yunnosch Sure, no problem, also explained myself under your post. Thanks for info :) – bloody Jan 08 '21 at 18:20

1 Answers1

3

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'.)

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • If the library is small you can put it all in a header file - but look out if it has any global or static variables as they will fail – mmmmmm Jan 08 '21 at 17:58
  • 1
    @mmmmmm I admit the header-only possibility in general and approve the proposed duplicates. However I feel that OP was not thinking of that case and that my answer still has value in sorting misconception. – Yunnosch Jan 08 '21 at 18:06