library

As far as i understand if i create

myclass.h
1
2
3
4
class MyClass {
public:
void my_method() const; // Method declaration
};


and myclass.cpp
1
2
3
4
5
6
#include "myclass.h"
#include <iostream>

void MyClass::my_method() const {
cout << "Executing \"my_method\"" << endl;
}


than every time when i #include "myclass.h"
compiler would compile myclass.cpp separately and than would link it to the rest of the project.

How could i precompile this myclass.cpp and probably also myclass.h so i could insta #include this class (would love if i could use it like #include <myclass> ) without a need to compile them every time together with the rest of files. Also without need to make sure they are in the same directory as main.cpp

cout << "This would be sooooooo cool!!! " <<endl;
Last edited on
than every time when i #include "myclass.h"
compiler would compile myclass.cpp separately and than would link it to the rest of the project.
Nope, you will need to pass myclass,cpp as argument to the compiler or add it to project, if you are using some IDE.

Header file (I will not touch concept of templates right now) contains declarations: promises, that some entity will exist in linking stage. It is up to you to provide it.

Relation myclass.h ←→ myclass.cpp is only for convinience. You can rename myclass.h to some_random_name.h and it will work just as well.

To create a precompiled library you just need to compile some file without linking (look up your compiler documentation to know how). However, you will still need to tell compiler that you want to link that library with your programm.
So when i read about templates in some books ill learn more about this?
Tnx a lot for answer, looks like i still know nothing :)
Last edited on
So when i read about templates in some books ill learn more about this?
No, they follow a slightly different model (which makes it impossible to make a precompiled library from them)
Tnx man for info :)
Topic archived. No new replies allowed.