1

I am trying to use sfm module for opencv. The problem is linking that. So, let's see the code.

main.cpp:

...
    cv::sfm::reconstruct(images_paths, Rs_est, ts_est, K2, points3d_estimated, true)
...

I compiled this code to main.o. And now, I would like to link it with libopencv_core.so and libopencv_sfm.so . The second one file contains a definition of function 'reconstruct'. Why am I sure? When I type:

nm -D libopencv_sfm.so | grep reconstruct

I got:

00000000000b4ca0 T _ZN2cv3sfm11reconstructERKNS_11_InputArrayERKNS_12_OutputArrayES6_RKNS_17_InputOutputArrayEb
00000000000b4ba0 T _ZN2cv3sfm11reconstructERKNS_11_InputArrayERKNS_12_OutputArrayES6_RKNS_17_InputOutputArrayES6_b
00000000000b2650 T _ZN2cv3sfm11reconstructESt6vectorISsSaISsEERKNS_12_OutputArrayES6_RKNS_17_InputOutputArrayEb
00000000000b2550 T _ZN2cv3sfm11reconstructESt6vectorISsSaISsEERKNS_12_OutputArrayES6_RKNS_17_InputOutputArrayES6_b

I checked signature of reconstruct function in main.o file. And it match to:

_ZN2cv3sfm11reconstructESt6vectorISsSaISsEERKNS_12_OutputArrayES6_RKNS_17_InputOutputArrayES6_b

So the shared library contains definition of reconstruct.

So now, I link:

g++ libopencv_core.so libopencv_sfm.so main.o -o main

and it gives me error:

undefined reference to `cv::sfm::reconstruct(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, cv::_OutputArray const&, cv::_OutputArray const&, cv::_InputOutputArray const&, cv::_OutputArray const&, bool)'

And I don't understand. After all libopencv_sfm.so contains definition of that function!

And please: don't tell me: use cmake. I tried use cmake and the problem is the same. So I try to understand it on low level.

Thanks in advance.

(gcc version: 5.1.1)

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Gilgamesz
  • 4,727
  • 3
  • 28
  • 63

1 Answers1

3

To link a library use -l flag and add dependencies after the objects they depend on them:

g++ -o main main.o -L. -lopencv_sfm -lopencv_core

The -L. will search for libearies in current path. Omit if not needed.

Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
  • Seems I can't post a new answer because the question is marked as duplicate, so I have a follow-up question: Where your opencv library come from ? Did you built it yourself or did it come from a package of your distro ? (and if yes, which one ?) You may have been bitten by new gcc5 features such as this one: https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html (tldr: try using -D_GLIBCXX_USE_CXX11_ABI=0 when compiling your code) – Dimitri Merejkowsky Mar 04 '16 at 20:39