pid.h
#include <iostream>
template <class T>
void f(T t);
pid.c
#include "pid.h"
template <class T>
void f(T t) {
std::cout << t;
}
template
void f<int>(int);
pid2.c
#include "pid.h"
template <class T>
void f(T t) {
std::cout << 55;
}
template
void f<int>(int);
main.c
#include "pid.h"
int main()
{
f(1);
return 0;
}
command:
g++ "-IC:\\Users\\kam\\workspace\\boost_1_56_0" -O0 -g3 -pg -Wall -c -fmessage-length=0 -std=c++11 -pthread -o "src\\pid2.o" "..\\src\\pid2.cpp"
g++ "-IC:\\Users\\kam\\workspace\\boost_1_56_0" -O0 -g3 -pg -Wall -c -fmessage-length=0 -std=c++11 -pthread -o "src\\pid.o" "..\\src\\pid.cpp"
g++ "-IC:\\Users\\kam\\workspace\\boost_1_56_0" -O0 -g3 -pg -Wall -c -fmessage-length=0 -std=c++11 -pthread -o "src\\main.o" "..\\src\\main.cpp"
g++ -pg -o Hello_World.exe "src\\pid2.o" "src\\pid.o" "src\\main.o"
result:
55
Now, if I make the functions in pid.c and pid2.c non-templated I do get the multiple definition error.