This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Static Method Eigen Template

Tags: None
(comma "," separated)
RLovelett
Registered Member
Posts
11
Karma
0

Static Method Eigen Template

Mon Aug 29, 2011 8:48 pm
I am writing a class that will exist in a static library. I want to write some unit tests for this static library and class. I have chosen GTest as my unit testing harness.

I have a static method in the class called kronecker (Klass::kronecker). It takes two const Eigen::MatrixBase parameters. When I reference the Klass::kronecker method inside of my tests I get a linker error saying the method is undefined (LNK2001: unresolved external symbol).

I feel like this has something to do with my lack of understanding regarding the templating used by Eigen.

HELP? ???

Code: Select all
// klass.h
class klass
{
  Klass();
  ~Klass();
  template <typename Derived>
  static void kronecker(const Eigen::MatrixBase<Derived>& mat1, const Eigen::MatrixBase<Derived>& mat2);
}

// Klass.cpp
template <typename Derived>
void Klass::kronecker(const Eigen::MatrixBase<Derived>& mat1, const Eigen::MatrixBase<Derived>& mat2) { ... };
}

// klass_test.cpp
TEST(KlassTest, Kronecker)
{
  Eigen::Vector4i mat1;
  Eigen::Vector4i mat2;
  mat1 << 1, 2, 3, 4;
  mat2 << 1, 1, 1, 1;
  Klass::kronecker(mat1, mat2);
}
jitseniesen
Registered Member
Posts
204
Karma
2

Re: Static Method Eigen Template

Tue Aug 30, 2011 9:34 am
I don't know GTest so it's a bit hard to comment. However, are you sure it is okay to put the implementation of the template method in the .cpp file? Does the compiler not need access to the implementation when instantiating your class during the compilation of klass_test (this has nothing to do with Eigen).
RLovelett
Registered Member
Posts
11
Karma
0

Re: Static Method Eigen Template

Tue Aug 30, 2011 3:57 pm
I probably should not have brought up GTest that is just a mis-direction to what the real problem is.

The same error occurs if I just write a simple console program that links against my library. It will complain:

LNK2001: unresolved external symbol "public: static class Eigen::Matrix<int,16,16,0,16,16> __cdecl Klass::kronecker<int,4,4>(class Eigen::Matrix<int,4,4,0,4,4> &,class Eigen::Matrix<int,4,4,0,4,4> &)" (??$kronecker@H$03$03@Interp@@SA?AV?$Matrix@H$0BA@$0BA@$0A@$0BA@$0BA@@Eigen@@AAV?$Matrix@H$03$03$0A@$03$03@2@0@Z)


When I look at the dumpbin output the kronecker method does not appear in my library.

I think that this: http://cboard.cprogramming.com/cplusplu ... tions.html is the answer to what ales me. What is more I don't think I can do anything about it.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Static Method Eigen Template

Wed Aug 31, 2011 7:13 am
Could you provide the complete "console" test. In your initial post, jitseniesen was write about the fact that the Klass::kronecker function must be defined in the .h file, not in a cpp file. If you do, you have to rely on explicit template instantiations and instantiate the class for all the types you plan to use.


Btw, in the unsupported/ folder of Eigen there already is a kronecker product implementation.
RLovelett
Registered Member
Posts
11
Karma
0

Re: Static Method Eigen Template

Wed Aug 31, 2011 3:04 pm
The console test program I was talking about would be this:

Code: Select all
#include <klass.h>

void main()
{
  Eigen::Vector2i v1;
  Eigen::Vector2i v2;
  v1 << 1, 1;
  v2 << 1, 2;

  Eigen::Matrix<int, 4, 1> v3 = Klass::kronecker(v1, v2);
  Eigen::Matrix<int, 4, 1> v4 = Klass::kronecker(v2, v1);
}


Well basically, it came down to my lack of understanding of how a C++ template actually works. I never realized that they only really exist at compile time and therefore must be written in the header. So what I did to solve this was to implement the PIMPL idiom. Then in my unit testing cases include the *.cpp file; so I could compile and link the tests.

Also, I would prefer to use the Eigen built-in Kronecker method. However, in my Eigen folder I cannot find the method. I ran `grep -r -i kronecker ./` on the unsupported directory and nothing came back.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Static Method Eigen Template

Wed Aug 31, 2011 6:31 pm
ah right, it is in the devel branch only.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]