g2o icon indicating copy to clipboard operation
g2o copied to clipboard

Some examples do not work when building g2o to static libraries

Open txq1986 opened this issue 9 years ago • 5 comments

Hi Rainer,

Thank you for the great g2o work and I am trying to use it.

Are you aware of that some examples do not work when building g2o to static libraries (.a files). For example, I got a bunch of errors of tutorial_slam2d fatal error in creating cache of type TUTORIAL_CACHE_SE2_OFFSET addEdge: FATAL, cannot resolve caches for edge 0x23416e0 g2o::Cache* g2o::CacheContainer::createCache(const g2o::Cache::CacheKey&) fatal error in creating cache of type TUTORIAL_CACHE_SE2_OFFSET addEdge: FATAL, cannot resolve caches for edge 0x23419a0 ...

cmake commands: $cmake -DBUILD_SHARED_LIBS=OFF ../ OS: Ubuntu 14.04

Build to shared libraries are completely perfect and all the examples work well. Do you have an idea what is the possible reasons for it? Thanks

XQ

txq1986 avatar Jan 23 '15 01:01 txq1986

I met the same problem. Have you solved it ? @txq1986

jsgaobiao avatar Mar 21 '17 11:03 jsgaobiao

I have the same problem, I was thinking the different version of g2o wasn't compatible. But I remove all files, and reinstall g2o again, it still doesn't work well. OS:Ubuntu 14.04

AlwaysLearningJary avatar Mar 23 '17 08:03 AlwaysLearningJary

A classic problem with static linking... compiler optimizes out what it thinks is not used, which is a problem with class factories.

The origin of the problem is this:

    Factory* f = Factory::instance();
    HyperGraph::HyperGraphElement* e = f->construct(key.type());

The factory returns nullptr for string "TUTORIAL_CACHE_SE2_OFFSET", which means that this line is not being executed:

  G2O_REGISTER_TYPE(TUTORIAL_CACHE_SE2_OFFSET, CacheSE2Offset);

The trick used in G2O_REGISTER_TYPE() to ensure the automatic registration is not working with static linking.

I know of this problem because I had to switch to a more complex auto-registration macro for a project of mine. Solutions:

  • Forget about static linking...
  • Implement something like MRPT_INITIALIZER() in G2O... Basically, to use __attribute__((constructor)); in GCC/CLANG and continue relying on the existing approach for MSVC (so it will not work with static linking, that's a limitation we have!)

Cheers!

jlblancoc avatar Mar 23 '17 09:03 jlblancoc

@jlblancoc

Hi, I am sorry. I do not understand how to implement something like MRPT_INITIALIZER() in G2O. Do you have any demos for solving this problem?

Thank you!

taiping-z avatar May 25 '17 01:05 taiping-z

do like this namespace g2o { namespace tutorial {

	G2O_REGISTER_TYPE_GROUP(tutorial_slam2d);

	G2O_REGISTER_TYPE(TUTORIAL_VERTEX_SE2, VertexSE2);
	G2O_REGISTER_TYPE(TUTORIAL_VERTEX_POINT_XY, VertexPointXY);

	G2O_REGISTER_TYPE(TUTORIAL_PARAMS_SE2_OFFSET, ParameterSE2Offset);

	G2O_REGISTER_TYPE(TUTORIAL_CACHE_SE2_OFFSET, CacheSE2Offset);

	G2O_REGISTER_TYPE(TUTORIAL_EDGE_SE2, EdgeSE2);
	G2O_REGISTER_TYPE(TUTORIAL_EDGE_SE2_POINT_XY, EdgeSE2PointXY);
}

} // end namespace int main() { return 0; }

kyosho81 avatar Dec 25 '19 09:12 kyosho81