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

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

Comments

4 participants
@Con-Mi
Copy link

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

This comment has been minimized.

Copy link
Contributor

commented Oct 31, 2018

Compare exact full compiler/linker command lines.

@Con-Mi

This comment has been minimized.

Copy link
Author

commented Oct 31, 2018

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

@alalek

This comment has been minimized.

Copy link
Contributor

commented Oct 31, 2018

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

@Con-Mi

This comment has been minimized.

Copy link
Author

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Author

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link
Contributor

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

This comment has been minimized.

Copy link

commented Jan 8, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.