Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined reference to cv::imread(std::string const&, int) #13000

Closed
Con-Mi opened this issue Oct 31, 2018 · 29 comments
Closed

Undefined reference to cv::imread(std::string const&, int) #13000

Con-Mi opened this issue Oct 31, 2018 · 29 comments

Comments

@Con-Mi
Copy link

@Con-Mi Con-Mi commented Oct 31, 2018

I am building a project with OpenCV 4 beta and when I use the function cv::imread or cv::imwrite I get an undefined reference to the functions.

My CMakeLists.txt is as follows:

cmake_minimum_required(VERSION 3.1.0)

project(myImageProgram)

set(CMAKE_PREFIX_PATH /home/marios-cellink/libtorch)

find_package(OpenCV 4.0.0 REQUIRED)
find_package(Torch REQUIRED)
add_executable(${PROJECT_NAME} "main.cpp")

message(STATUS "OpenCV library status:")
message(STATUS " config: ${OPENCV_DIR}")
message(STATUS " version: ${OpenV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include_path: ${OpenCV_INCLUDE_DIRS}")

message(STATUS "Torch library status:")
message(STATUS " include_path: ${TORCH_INCLUDE_DIRS}")
message(STATUS " TORCHLIB: ${TORCH_LIBRARIES}")

target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS})
target_link_libraries(${PROJECT_NAME} ${TORCH_LIBRARIES})

And the program is as follows:

#include
#include <torch/script.h>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>

int main()
{
cv::Mat orig = cv::imread("../myImageProgram/0003_.png");
cv::Mat img = cv::imread("../myImagePorgram/0003_.png", cv::IMREAD_COLOR);
std::shared_ptrtorch::jit::script::Module module = torch::jit::load("../myImageProgram/model.pt");
return 0;
}

Am I linking OpenCV correctly with cmake? It seems that if I comment out the torch parts from CMake, the program build succesfully. Any ideas?

@alalek
Copy link
Contributor

@alalek alalek commented Oct 31, 2018

Compare exact full compiler/linker command lines.

@Con-Mi
Copy link
Author

@Con-Mi Con-Mi commented Oct 31, 2018

@alalek what do you mean?
Compile for example with g++ with pkg-config?

@alalek
Copy link
Contributor

@alalek alalek commented Oct 31, 2018

No.
Use make VERBOSE=1 and compare logs for both cases.

@Con-Mi
Copy link
Author

@Con-Mi Con-Mi commented Nov 1, 2018

Ok this is what I have tried:
If I do:

g++ -std=c++11 main.cpp -o test pkg-config opencv4 --cflags --clibs

Then this works.

If I also run the above CMakeLists.txt file with:

find_package(OpenCV REQUIRED)

it finds OpenCV 3.4.2 that I have in my system installed for python and then this also works.

Any ideas why? I installed OpenCV 4.0-beta from downloading the zip file that is in the website and I build it with cmake.

@alalek
Copy link
Contributor

@alalek alalek commented Nov 1, 2018

These lines are required for OpenCV 4.0 apps:

# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

finds OpenCV 3.4.2
I have in my system installed

Into /usr/local ? Side-by-side installations may not work in this case (too many possible conflicts).
Try to pass -DOpenCV_DIR=<path to OpenCVConfig.cmake> to CMake (and clear build dir before that to drop CMake cache).


We don't help with remote investigation of problems which we are not able to reproduce on our side (because it is very time consuming and usually configuration problem without any following fix into OpenCV), but we can take a look on the clear reason of the problem and suggest some fix/workaround.

@Con-Mi
Copy link
Author

@Con-Mi Con-Mi commented Nov 5, 2018

No I have opencv 3.4.2 on Miniconda folder not on usr/local and that what cmake finds.

I have removed opencv 4 beta and I have build the 3.4.3.

I did the same steps again with find_package(OpenCV 3.4.3 REQUIRED) and everything seems to
work just fine with the above code I have provided. I have also set the flags for c++11.

I don't know if I am missing something, but it seemed that with imread, imwrite and other io functions opencv 4.0.0 beta didn't work. I guess I will work with opencv 3.4.3. for now.

@sergiud
Copy link
Contributor

@sergiud sergiud commented Nov 16, 2018

These lines are required for OpenCV 4.0 apps:

# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

I have not looked into OpenCV 4.0 yet, but C++11 requirements should be set by OpenCV itself through target_compile_features. Requiring users to set these two lines constitutes incorrect CMake usage.

@alalek
Copy link
Contributor

@alalek alalek commented Nov 16, 2018

target_compile_features(target PUBLIC cxx_std_11) is supported by CMake 3.8+ only.
Produced CMake configuration files can't be reused by CMake less than CMake 3.8:

CMake Error in cpp/CMakeLists.txt:
  Specified unknown feature "cxx_std_11" for target "example_cpp_edge".

Current minimal supported CMake version is 3.5.x (Ubuntu 16.04 has 3.5.1 by default).

@sergiud
Copy link
Contributor

@sergiud sergiud commented Nov 16, 2018

You are not supposed to use cxx_std_11, but explicit feature requirements such as cxx_auto_type etc.

@zacario-li
Copy link

@zacario-li zacario-li commented Jan 8, 2019

rebuild your opencv4 from source with "add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)", have fun.

@hoboh
Copy link

@hoboh hoboh commented Jan 15, 2020

I faced the same problem

main.cpp:(.text+0x113): undefined reference to cv::imread(std::string const&, int)'
main.cpp:(.text+0x179): undefined reference to cv::imread(std::string const&, int)' main.cpp:(.text+0x567): undefined reference to cv::imshow(std::string const&, cv::_InputArray const&)'
main.cpp:(.text+0x856): undefined reference to cv::imshow(std::string const&, cv::_InputArray const&)' main.cpp:(.text+0xa17): undefined reference to cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator > const&)'
main.cpp:(.text+0xabc): undefined reference to cv::imshow(std::string const&, cv::_InputArray const&)' /home/sy/anaconda3/lib/libpng16.so.16: undefined reference to inflateValidate@ZLIB_1.2.9'
liblbd_mod.so: undefined reference to cv::write(cv::FileStorage&, std::string const&, int)' liblbd_mod.so: undefined reference to cv::FileNode::string() const'
liblbd_mod.so: undefined reference to cv::Algorithm::getDefaultName() const' liblbd_mod.so: undefined reference to cv::error(int, std::string const&, char const*, char const*, int)'
liblbd_mod.so: undefined reference to cv::operator<<(cv::FileStorage&, std::string const&)' liblbd_mod.so: undefined reference to cv::FileStorage::FileStorage(std::string const&, int, std::string const&)'
liblbd_mod.so: undefined reference to cv::Algorithm::save(std::string const&) const' collect2: error: ld returned 1 exit status
And the relative content of CMakeLists is as follows:
`...

OpenCV

find_package(OpenCV COMPONENTS core imgproc highgui imgcodecs REQUIRED)
...
target_link_libraries(lbd_mod
${OpenCV_LIBRARIES})
target_link_libraries(lbd_mod_test
lbd_mod
${OpenCV_LIBRARIES})
...
target_include_directories(${PYPROJECT_NAME} PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/cpp/include"
${Boost_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
${PYTHON_INCLUDE_DIRS}
${LBD_MOD_INCLUDE}
)

target_link_libraries(${PYPROJECT_NAME}
${Boost_LIBRARIES}
${OpenCV_LIBRARIES}
${PYTHON_LIBRARIES}
)
...`
@alalek
I tried lots of ideas published in the net, but those didn't work. Please help me, thank u

@hoboh
Copy link

@hoboh hoboh commented Jan 16, 2020

@alalek I also tried u mentioned

make VERBOSE=1

It shows

Linking CXX executable lbd_mod_test
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/lbd_mod_test.dir/link.txt --verbose=1
/usr/bin/c++ -rdynamic CMakeFiles/lbd_mod_test.dir/main.cpp.o -o lbd_mod_test -Wl,-rpath,/home/sy/code/lbdmod-master/build:/usr/local/lib liblbd_mod.so /usr/local/lib/libopencv_highgui.so.4.1.0 /usr/local/lib/libopencv_videoio.so.4.1.0 /usr/local/lib/libopencv_imgcodecs.so.4.1.0 /usr/local/lib/libopencv_imgproc.so.4.1.0 /usr/local/lib/libopencv_core.so.4.1.0
CMakeFiles/lbd_mod_test.dir/main.cpp.o: In function main': main.cpp:(.text+0x113): undefined reference to cv::imread(std::string const&, int)'
main.cpp:(.text+0x179): undefined reference to cv::imread(std::string const&, int)' main.cpp:(.text+0x567): undefined reference to cv::imshow(std::string const&, cv::_InputArray const&)'
main.cpp:(.text+0x856): undefined reference to cv::imshow(std::string const&, cv::_InputArray const&)' main.cpp:(.text+0xa17): undefined reference to cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator > const&)'
main.cpp:(.text+0xabc): undefined reference to cv::imshow(std::string const&, cv::_InputArray const&)' /home/sy/anaconda3/lib/libpng16.so.16: undefined reference to inflateValidate@ZLIB_1.2.9'
liblbd_mod.so: undefined reference to cv::write(cv::FileStorage&, std::string const&, int)' liblbd_mod.so: undefined reference to cv::FileNode::string() const'
liblbd_mod.so: undefined reference to cv::Algorithm::getDefaultName() const' liblbd_mod.so: undefined reference to cv::error(int, std::string const&, char const*, char const*, int)'
liblbd_mod.so: undefined reference to cv::operator<<(cv::FileStorage&, std::string const&)' liblbd_mod.so: undefined reference to cv::FileStorage::FileStorage(std::string const&, int, std::string const&)'
liblbd_mod.so: undefined reference to `cv::Algorithm::save(std::string const&) const'
collect2: error: ld returned 1 exit status
CMakeFiles/lbd_mod_test.dir/build.make:89: recipe for target 'lbd_mod_test' failed
make[2]: *** [lbd_mod_test] Error 1
make[2]: Leaving directory '/home/sy/code/lbdmod-master/build'
CMakeFiles/Makefile2:72: recipe for target 'CMakeFiles/lbd_mod_test.dir/all' failed
make[1]: *** [CMakeFiles/lbd_mod_test.dir/all] Error 2
make[1]: Leaving directory '/home/sy/code/lbdmod-master/build'
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2

So what happened?

@alalek
Copy link
Contributor

@alalek alalek commented Jan 16, 2020

I believe there is mess with std::string vs std::__cxx11::string.
Check compiler command line options too (before linking).

Try -std=c++11 or other options from the comments above.

@hoboh
Copy link

@hoboh hoboh commented Jan 16, 2020

@alalek It has been set in the CMakeLists.txt

###============= C++11 support====================================
if(${CMAKE_VERSION} VERSION_LESS "3.1")
include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if (COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif (COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else ()
message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif ()
else()
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
endif()

@alalek
Copy link
Contributor

@alalek alalek commented Jan 16, 2020

How did you get OpenCV binaries?

Please dump symbols:

  • from OpenCV on your machine:
$ nm -g ./lib/libopencv_imgcodecs.so | grep 6imread
00000000000a2386 T _ZN2cv6imreadERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi
  • from your app:
$ nm -g liblbd_mod.so | grep 6imread

@hoboh
Copy link

@hoboh hoboh commented Jan 16, 2020

The result is as follows:

$ nm -g /usr/local/lib/libopencv_imgcodecs.so | grep 6imread
000000000006b700 T _ZN2cv6imreadERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEi

$ nm -g liblbd_mod.so | grep 6imread
U _ZN2cv6imreadERKSsi

@hoboh
Copy link

@hoboh hoboh commented Jan 16, 2020

I also tried "g++" instead of "make", it turns

$ g++ main.cpp -L/usr/local/lib -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -o lbd_mod_test
In file included from main.cpp:22:0:
cpp/src/precomp.hpp:54:36: fatal error: opencv2/core/utility.hpp: No such file or directory
#include "opencv2/core/utility.hpp"

But "utility.hpp" do exists.

@alalek
Copy link
Contributor

@alalek alalek commented Jan 16, 2020

_ZN2cv6imreadERKSsi

So, -std=c++11 is not applied during build of liblbd_mod.so.

@hoboh
Copy link

@hoboh hoboh commented Jan 16, 2020

I add
CXXFLAGS := -std=c++11

in the Makefile, it also doesn't work.

@hoboh
Copy link

@hoboh hoboh commented Jan 16, 2020

It seems that the opencv libraries are messed up, but I didn't figure out how to resolve it.

@hoboh
Copy link

@hoboh hoboh commented Jan 22, 2020

Finally I reinstalled opencv on another machine, it solved.

@hoboh
Copy link

@hoboh hoboh commented Jan 22, 2020

Finally I reinstalled opencv on another machine, it solved. @alalek Please close the subject, thx.

@agavrel
Copy link

@agavrel agavrel commented Mar 29, 2020

Before building use:

cmake -D CMAKE_BUILD_TYPE=Release -D GLIBCXX_USE_CXX11_ABI=0 -D CMAKE_INSTALL_PREFIX=/usr/local ..

Instead of what is recommended on the official website.

Full steps (ubuntu 18.04):

git clone https://github.com/opencv/opencv.git
cd ~/opencv && mkdir release && cd release
cmake -D CMAKE_BUILD_TYPE=Release -D GLIBCXX_USE_CXX11_ABI=0 -D CMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install

Then compile .cpp file with:

g++ main.cpp -I/opencv2/include/ -lopencv_core -lopencv_highgui -lopencv_imgcodecs -lopencv_imgproc -lopencv_videoio

@elephantjohn
Copy link

@elephantjohn elephantjohn commented Mar 31, 2020

target_link_libraries(your_name ${OpenCV_LIBS})

@gezabohus
Copy link

@gezabohus gezabohus commented Jul 21, 2020

PKG_CONFIG_PATH should contain the path the opencv pkg_config dir or some such. In my case

PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig:"$PKG_CONFIG_PATH"

helped. You might need a variation of this.

locate opencv

should help.

@Scharfsinnig
Copy link

@Scharfsinnig Scharfsinnig commented Aug 3, 2020

rebuild your opencv4 from source with "add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)", have fun.

It works for me. Thanks a lot. ^O^

@latuanvinh1998
Copy link

@latuanvinh1998 latuanvinh1998 commented Dec 7, 2020

Same issue solved for me : pytorch/pytorch#14620 (comment)

@Vibhuarvind
Copy link

@Vibhuarvind Vibhuarvind commented Apr 8, 2021

bro i am getting this error on colab so how do i run it on colab using g++?

@ingbeeedd
Copy link

@ingbeeedd ingbeeedd commented Sep 15, 2021

For me, It works well when you change the libtorch compiler to libtorch-shared-with-deps-1.9.0%2Bcu111.zip -> libtorch-cxx11-abi-shared-with-deps-1.9.0%2Bcu111.zip.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.