I faced with some compilation issues, here is details:
Lets say I have a declared of some data structure my_data_struct<typename T>
. The reason I use templates here is I want my data structure to be able to work with any data type (Like std::stack
or std::vector
do)
In that case, if I don't know the final types it will be used for, I must define and declare my structure in one file.
Lets say then, I have couple of other libs (divided by .h
declaration and .cpp definition). Each of them use my_data_struc
.
Finally, lets say I have main.cpp
file where "all comes together" with #include
's.
How could I get compile my program?
If I will compile my cpp files separately to object files, and will try to link them togather, I will receive multiple definition of
error, because my_data_struct
will be defined couple of times...
The only way I realize, is to move definitions of my libs to its declaration (.h
) files and then just compile the main.cpp
all at once. But, is it the right way to compile programs like that? Or is there is any better way how to build and compile such stuff?
I didn't find any best practices of how to proceed with it...
Thanks in advice.
UPD:
Well, while creating example, I figured out that the actual issues is caused not by template class, but by non template functions that exists beside the class in the same namespace:
//mydatastruct.h
#ifndef MYSTRUCT
#define MYSTRUCT
namespace G{
template<typename T>
class my_data_struct{
public:
my_data_struct(){};
~my_data_struct(){};
int do_something(int a){ return a; };
//...
};
int test(int b){ return 2; };
}
#endif
// a.h
#ifndef A_H
#define A_H
#include "mydatastruct.h"
class A{
public:
A();
~A();
};
#endif
// a.cpp
#include "a.h"
A::A(){}
A::~A(){}
// main.cpp
#include "a.h"
int main(){ return 0; }
Compiling:
g++ -c a.cpp -o a.o
g++ -c main.cpp -o main.o
g++ a.o main.o -o main.exe
And the actual error is:
main.o:main.cpp:(.text+0x0): multiple definition of `G::test(int)'
a.o:a.cpp:(.text+0x0): first defined here