CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (http://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   Putting functions different files, linker error (undefined reference) (http://www.cfd-online.com/Forums/openfoam-programming-development/109593-putting-functions-different-files-linker-error-undefined-reference.html)

Krischan November 21, 2012 09:52

Putting functions different files, linker error (undefined reference)
 
Hey everyone,
I'm currently working on a solver and wrote some functions I would like to put in a second .C file (not the main solver code).

I wrote a file helperFunctions.H
Code:

#include "fvCFD.H"

namespace Foam {
template<class Type>
void printField(OSstream& output, const GeometricField<Type, fvPatchField, volMesh>& vf);
}
}

And have the definition of that function in helperFunctions.C
Code:

#include "helperFunctions.H"
namespace Foam {
template<class Type>
void printField(OSstream& output, const GeometricField<Type, fvPatchField, volMesh>& vf) {
    forAll (vf.internalField(),icell) {
        output << vf.internalField()[icell] << " ";
    }
}
}

I also added helperFunctions.C to Make/files
My main solver as well as helperFunctions.C compile fine, but when it comes to linking I get the error message

Code:

undefined reference to `void Foam::printField<double>(Foam::OSstream&, Foam::GeometricField<double, Foam::fvPatchField, Foam::volMesh> const&)'
Obviously I'm doing something wrong here, but I don't see the reason, so any help is appreciated.

Thanks,
Christian

Krischan November 21, 2012 15:25

Okay, after reading my Stroustrup and the C++ FAQ I got it.
It's because of how templates work... Time to refresh my C++ Skills, I guess ;)


All times are GMT -4. The time now is 18:25.