I have a complex project, which consists of source files in various sub-directories which are compiled and archived into libXXX.a files. This compiled and linked well.
I now added the new file stdstrutils.cpp
(together with its header file stdstrutils.h
) to the sub-project in the "common" subdirectory.
This file contains a function with an ordinary template,
template<typename T> bool strToNum(const std::string sData, T *t);
and one with a variadic template,
template<typename... Args> std::string stdsprintf(const std::string sForma, Args... args);
(the declarations in the header file exactly match the functions in the c++-file) These functions are used by four c++-files in various of the sub-directories.
I added the line #include "stdstrutils.cpp"
to the #includes in all the files that use the templated functions from stdstrutils
, but i still get a lot of 'undefined reference to'-message from the linker. All concern the template-functions from stdstrutils
.
The code of stdstrutils
is definitely linked into the library libCommon.a
: i checked this with a test-program - it compiled and linked without complaints, and executed as expected.
This is the link command from the make file:
~/utils/cgcc -fopenmp -std=c++11 -Wall -g -o QHGMain \
QHGMain.o Simulator.o SimParams.o EventChecker.o GridScrambler.o \
-L ../populations -lPopulations \
-L ../genes -lGenes \
-L ../kernel -lKernel \
-L ../modular -lModular \
-L ../qmaps -lQMap \
-L ../icosa -lIcosa \
-L ../geoinfo -lGeoInfo \
-L ../common -lCommon \
-L ../io -lIO \
-lz -lhdf5 -lm -lcrypto
The callers of the functions from stdstrutils
are in libCommon
, libKernel
as well as in the main directory (EventChecker.o
).
Confusingly, there are other functions in libCommon
which also have templates (and are called from different files), but they cause none of the 'undefined reference to'-messages.
So is there some rule about where to include the cpp-files (with the templated functions) which helps me avoid these messages?