Linker error. It is not possible [duplicate] - c++

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 6 years ago.
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)

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.

Related

undefined reference to boost::filesystem::path_traits::convert

I am trying to compile a program using cmake, and am seeing the following linker error:
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log_setup.so:
undefined reference to
boost::filesystem::path_traits::convert(wchar_t const*, wchar_t
const*, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >&, std::codecvt<wchar_t, char, __mbstate_t>
const&)' /home/quant/bin/boost_1_61_0/stage/lib/libboost_log.so:
undefined reference to
boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry
const&, std::__cxx11::basic_string,
std::allocator >&)'
The linker command that ninja generated looks like this:
g++ -pthread -DBOOST_ALL_DYN_LINK
utility/test/CMakeFiles/utilityTest.dir/loadCSVTests.cpp.o
utility/test/CMakeFiles/utilityTest.dir/main.cpp.o
utility/test/CMakeFiles/utilityTest.dir/randomDeviceTests.cpp.o -o
utility/test/utilityTest -rdynamic
/home/quant/bin/boost_1_61_0/stage/lib/libboost_thread.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_program_options.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_serialization.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_unit_test_framework.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_system.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log_setup.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_filesystem.so
utility/lib/libutilityLib.a utility/testLib/libutilityTestLib.a
utility/lib/libutilityLib.a
/home/quant/bin/boost_1_61_0/stage/lib/libboost_thread.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_program_options.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_serialization.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_unit_test_framework.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_system.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_log_setup.so
/home/quant/bin/boost_1_61_0/stage/lib/libboost_filesystem.so
-Wl,-rpath,/home/quant/bin/boost_1_61_0/stage/lib
As you can see, I am linking against boost_filesystem and boost_system, so it's not the same problem as referenced on this SO post (and the many others like it).
I am using boost 1.61, which I compiled with gcc 5.3 (the same compiler as the one I'm compiling my program with).
What am I doing wrong?
I had a similar issue, this could be because of a new ABI which is introduced from gcc 5.1.
https://github.com/openframeworks/openFrameworks/issues/4203
I fixed mine by adding "add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)" to CMakeLists.txt

PCL & VTK Object File Errors

I'm using a CamBoard Nano and Point Cloud Library in a C++ program. It uses a couple other libraries and one main C++ function as of right now. Someone else wrote the code for Windows, and I'm porting it over to Linux (Ubuntu 12.04). I've been able to get the C++ file to compile with a Makefile, but now I'm getting a ton of errors from the object file. I'm new to C++, and I don't understand what they all mean. It looks like they all stem from the same issue, hopefully one change will fix everything.
I was able to work out the errors in the C++ file by installing some Libraries (PCL, VTK, Eigen, OpenNI, etc.). I'll post what I can of the errors below, they fill up more than the entire terminal. I did look around on Google, and I found this: Qt 4.7 + VTK 5.6.1 on Mac OS X 10.6: errors linking projects
I downloaded QT 5.3, but when I use cmake on the example program I get the following error:
-- The C compiler identification is GNU 4.6.3
-- The CXX compiler identification is GNU 4.6.3
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Error at CMakeLists.txt:20 (message):
VTK was not built with Qt
-- Configuring incomplete, errors occurred!
I'm trying to fix this now. I'll post my Makefile and errors below, so if anyone knows how to fix this or has advice, please let me know. Thanks!
Makefile:
CC = g++-4.4
CFLAGS = -I ../include -I ~/../../usr/include/pcl-1.7/ -I ~/../../usr/include/eigen3/Eigen/src -I ~/../../usr/include/vtk-5.8
# first location: pmd header files (include, back one directory)
# second location: your pcl header files
# third location: your eigen header files
# fourth location: your vtk header files
LDFLAGS = -L. -lpmdaccess2 -lc
all: main
main: main.cpp copysdk copyplugins
$(CC) $(CFLAGS) -c -o main.o main.cpp
$(CC) $(LDFLAGS) -o main main.o
copysdk:
cp ../bin/libpmdaccess* ./ # bin folder back one directory
copyplugins:
cp ../bin/camboardnano* ./
clean:
rm main
rm main.o
rm camboardnano*
rm libpmdaccess*
Example of Errors:
main.o: In function `bool pcl::visualization::PCLVisualizer::addArrow<pcl::PointXYZ, pcl::PointXYZ>(pcl::PointXYZ const&, pcl::PointXYZ const&, double, double, double, double, double, double, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
main.cpp:(.text._ZN3pcl13visualization13PCLVisualizer8addArrowINS_8PointXYZES3_EEbRKT_RKT0_ddddddRKSsi[bool pcl::visualization::PCLVisualizer::addArrow<pcl::PointXYZ, pcl::PointXYZ>(pcl::PointXYZ const&, pcl::PointXYZ const&, double, double, double, double, double, double, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)]+0xcc): undefined reference to `pcl::console::print(pcl::console::VERBOSITY_LEVEL, char const*, ...)'
main.cpp:(.text._ZN3pcl13visualization13PCLVisualizer8addArrowINS_8PointXYZES3_EEbRKT_RKT0_ddddddRKSsi[bool pcl::visualization::PCLVisualizer::addArrow<pcl::PointXYZ, pcl::PointXYZ>(pcl::PointXYZ const&, pcl::PointXYZ const&, double, double, double, double, double, double, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)]+0x22d): undefined reference to `vtkActor2D::GetProperty()'
main.cpp:(.text._ZN3pcl13visualization13PCLVisualizer8addArrowINS_8PointXYZES3_EEbRKT_RKT0_ddddddRKSsi[bool pcl::visualization::PCLVisualizer::addArrow<pcl::PointXYZ, pcl::PointXYZ>(pcl::PointXYZ const&, pcl::PointXYZ const&, double, double, double, double, double, double, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)]+0x279): undefined reference to `pcl::visualization::PCLVisualizer::addActorToRenderer(vtkSmartPointer<vtkProp> const&, int)'
main.o: In function `bool pcl::visualization::PCLVisualizer::addPointCloud<pcl::PointXYZ>(pcl::PointCloud<pcl::PointXYZ>::ConstPtr const&, pcl::visualization::PointCloudGeometryHandler<pcl::PointXYZ> const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)':
main.cpp:(.text._ZN3pcl13visualization13PCLVisualizer13addPointCloudINS_8PointXYZEEEbRKNS_10PointCloudIT_E8ConstPtrERKNS0_25PointCloudGeometryHandlerIS5_EERKSsi[bool pcl::visualization::PCLVisualizer::addPointCloud<pcl::PointXYZ>(pcl::PointCloud<pcl::PointXYZ>::ConstPtr const&, pcl::visualization::PointCloudGeometryHandler<pcl::PointXYZ> const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)]+0x84): undefined reference to `pcl::console::print(pcl::console::VERBOSITY_LEVEL, char const*, ...)'
main.o: In function `vtkSmartPointer<vtkDataSet>::~vtkSmartPointer()':
These aren't all the errors, but all the errors that would fit in the terminal can be found here:
http://pastebin.com/jgbczK74
One of the things I noticed is that vtkSmartPointer came up a lot. I think that was the first header file that I was notified about when I was trying to compile the C++ code without the VTK library. Don't know if that means anything.

How to link against cpp-netlib

I would like to use the Library cpp-netlib for a C++ project. Therefore I installed the boost library with the help of homebrew (OS is Mac OS X 10.8). Then I downloaded cpp-netlib from the projects homepage, used cmake to create the Makefile for g++ and successfully applied make.
"make test" passed all its tests. Then I copied the include folder of cpp-netlib into the boost directory.
So here is when the trouble began:
I tried to compile the documentation's first example http-client but couldn't get it to work. When I used
g++ test.cpp -o out -I/usr/local/Cellar/boost/1.53.0/include
-L/usr/local/Cellar/boost/1.53.0/lib
-lboost_system-mt -lboost_filesystem-mt -lboost_thread-mt
I received these linker errors:
Undefined symbols for architecture x86_64:
"boost::network::uri::detail::parse(__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::network::uri::detail::uri_parts<__gnu_cxx::__normal_iterator<char const*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)", referenced from:
boost::network::uri::uri::parse() in ccs87Dq3.o
"boost::network::http::impl::normal_delegate::normal_delegate(boost::asio::io_service&)", referenced from:
boost::network::http::impl::connection_delegate_factory<boost::network::http::tags::http_async_8bit_udp_resolve>::new_connection_delegate(boost::asio::io_service&, bool, boost::optional<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::optional<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >)in ccs87Dq3.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I really used the search function but I couldn't find any solution for my problem. What am I doing wrong?
Thanks a lot!
After building cpp-netlib (>=0.9.3) there should 3 static libraries:
libcppnetlib-client-connections.a
libcppnetlib-server-parsers.a
libcppnetlib-uri.a
When building your http-client project, you should specify a library path for cpp-netlib (-L) and libraries to link (-l) against: cppnetlib-uri and libcppnetlib-client-connections.
Here is what worked for me. You will need to modify certain parts to deal with different versions of boost, different install paths, and so on.
g++ -o demo \
demo.cpp \
-lcppnetlib-uri \
-lcppnetlib-server-parsers \
-lcppnetlib-client-connections \
-lboost_thread-mt \
-lboost_system-mt \
-lssl \
-lcrypto \
-I/usr/local/include \
-L/usr/local/lib
If you are writing server-side code then I imagine you will also need to include -lcppnetlib-server-parsers.
In case someone is having the same issue with visual studio, provide the library input to the linker.
Project properties > Linker > input >
libcppnetlib-client-connectionsd.lib
libcppnetlib-urid.lib
for Debug configuration and
libcppnetlib-client-connections.lib
libcppnetlib-uri.lib
for Release configuration.
Specify the Library path here,
Project properties > Linker > general > Additional include directories

Undefinded symbols when linking OpenCV

I'm trying to link static libraries for the OpenCV library into my project, yet I get the error:
Undefined symbols for architecture armv7:
"__ZN2cv9ExceptionC1EiRKSsS2_S2_i", referenced from:
__ZL16icvGenerateQuadsPP8CvCBQuadPP10CvCBCornerP12CvMemStorageP5CvMati in libopencv_calib3d.a(calibinit.o)
_cvFindChessboardCorners in libopencv_calib3d.a(calibinit.o)
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have no ideas what that symbol "ZN2cv9ExceptionC1EiRKSsS2_S2_i" should be, that string of Letters found in any source or header file I used to build the static libraries.
Could anyone who's more experienced with the linker explain these errors to me?
EDIT: 01.07. haven't solved this yet, just opted out of checkerboard-detection with calib3d and went for a less complicated circle detection using features2D that gives no linking error.
Most probably you put OpenCV libs in wrong order. Try the following order in your link list:
opencv_contrib opencv_legacy opencv_videostab opencv_photo opencv_stitching opencv_objdetect opencv_video opencv_ml opencv_calib3d opencv_features2d opencv_highgui opencv_flann opencv_imgproc opencv_core
You can use c++filt to unmangle the undefined symbol to find the actual function name.
$ c++filt.exe __ZN2cv9ExceptionC1EiRKSsS2_S2_i
cv::Exception::Exception(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)
Then you'll have to identify which source file contains this function and link in the library corresponding to that.

undefined reference to `__stack_chk_fail'

Getting this error while compiling C++ code:
undefined reference to `__stack_chk_fail'
Options already tried:
added -fno-stack-protector while compiling - did not work, error persists
added a dummy implementation of void __stack_chk_fail(void) in my code. Still getting the same error.
Detailed Error:
/u/ac/alanger/gurobi/gurobi400/linux64/lib/libgurobi_c++.a(Env.o)(.text+0x1034): In function `GRBEnv::getPar/u/ac/alanger/gurobi/gurobi400/linux64/lib/libgurobi_c++.a(Env.o)(.text+0x1034): In function `GRBEnv::getParamInfo(GRB_StringParam, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
: undefined reference to `__stack_chk_fail'
amInfo(GRB_StringParam, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
: **undefined reference to `__stack_chk_fail'**
Earlier, I was getting 10's of such errors. Found out that there was a version mismatch between the gcc of the pre-compiled libraries I am using and the gcc version I was using to compile the code. Updated gcc and now I am getting only 2 of these errors.
Any help, please?
libgurobi_c++.a was compiled with -fno-stack-protector (obviously).
A few things come to mind:
add -fstack-protector when linking. This will make sure that libssp gets linked.
Manually link -lssp
Make your dummy version of __stack_chk_fail(void) in it's own object file and and add this .o file to your linker command AFTER libgurobi_c++.a. GCC/G++ resolves symbols from left to right during linking so despite your code having the function defined, a copy of an object containing the __stack_chk_fail symbol needs to be on the linker line to the right of libgurobi_c++.a.
In gentoo I had the same problem and i resolved creating 2 files. The first contain the option to be parsed by emerge and passed to gcc:
/etc/portage/env/nostackprotector.conf
CFLAGS="-fno-stack-protector -O2"
And the second tells which package should use this settings:
/etc/portage/package.env/nostackprotector
x11-libs/vte nostackprotector.conf
sys-libs/glibc nostackprotector.conf
www-client/chromium nostackprotector.conf
app-admin/sudo nostackprotector.conf
Just had the same issue: c++ code with an implementation of void __stack_chk_fail(void) showing several undefined reference to __stack_chk_fail errors when compiling.
My solution was to define __stack_chk_fail(void) as extern "C":
extern "C" {
__stack_chk_fail(void)
{
...
}
}
This suppressed the compilation error :)
Hope it helps!
https://wiki.ubuntu.com/ToolChain/CompilerFlags
says:
"Usually this is a result of calling ld instead of gcc during a build to perform linking"
This is what I encountered when modified the Makefile of libjpeg manually. Use gcc instead of ld solved the problem.