Skip to content

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

@Con-Mi

Description

@Con-Mi

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?

Activity

alalek

alalek commented on Oct 31, 2018

@alalek
Member

Compare exact full compiler/linker command lines.

Con-Mi

Con-Mi commented on Oct 31, 2018

@Con-Mi
Author

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

alalek

alalek commented on Oct 31, 2018

@alalek
Member

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

Con-Mi

Con-Mi commented on Nov 1, 2018

@Con-Mi
Author

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

alalek commented on Nov 1, 2018

@alalek
Member

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

Con-Mi commented on Nov 5, 2018

@Con-Mi
Author

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

sergiud commented on Nov 16, 2018

@sergiud
Contributor

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

alalek commented on Nov 16, 2018

@alalek
Member

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

sergiud commented on Nov 16, 2018

@sergiud
Contributor

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

zacario-li

zacario-li commented on Jan 8, 2019

@zacario-li

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

hoboh

hoboh commented on Jan 15, 2020

@hoboh

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

hoboh commented on Jan 16, 2020

@hoboh

@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

alalek commented on Jan 16, 2020

@alalek
Member

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

hoboh commented on Jan 16, 2020

@hoboh

@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()

13 remaining items

gezabohus

gezabohus commented on Jul 21, 2020

@gezabohus

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

Scharfsinnig commented on Aug 3, 2020

@Scharfsinnig

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

latuanvinh1998 commented on Dec 7, 2020

@latuanvinh1998

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

Vibhuarvind

Vibhuarvind commented on Apr 8, 2021

@Vibhuarvind

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

ingbeeedd

ingbeeedd commented on Sep 15, 2021

@ingbeeedd

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.

xun-dao

xun-dao commented on Apr 3, 2022

@xun-dao

hi all,
I foud one solution. I have two versions of c++(4.8 and 9.4), I remove the 4.8 version of c++ by run yum remove g++, then I can compile my code.

and here is my CMakeLists.txt content:

cmake_minimum_required(VERSION 3.0)

project(project)
find_package( OpenCV REQUIRED )
include_directories(${OpenCV_INCLUDE_DIRS})
message("OpenCV_INCLUDE_DIRS: " ${OpenCV_INCLUDE_DIRS})
add_executable(executableFileName main.cpp)
target_link_libraries( executableFileName ${OpenCV_LIBS} )
RL-arch

RL-arch commented on Aug 8, 2022

@RL-arch

Hi
if I use c++ 17 for example, do I need to add GLIBCXX_USE_CXX17_ABI=0?

zacario-li

zacario-li commented on Aug 9, 2022

@zacario-li

Hi if I use c++ 17 for example, do I need to add GLIBCXX_USE_CXX17_ABI=0?

yes

rajhlinux

rajhlinux commented on Oct 9, 2022

@rajhlinux

Hello I get the same issue on FreeBSD 13.1:

[ 98%] Building CXX object CMakeFiles/darknet_l.dir/src/image_opencv.cpp.o
[ 99%] Building C object CMakeFiles/darknet.dir/examples/darknet.c.o
[100%] Linking CXX static library libdarknet_s.a
[100%] Built target darknet_s
[100%] Linking CXX shared library libdarknet_l.so
[100%] Linking CXX executable darknet
[100%] Built target darknet_l
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `open_video_stream':
/home/user/darknet/src/image_opencv.cpp:186: undefined reference to `cv::VideoCapture::VideoCapture(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `show_image_frame_cv':
/home/user/darknet/src/image_opencv.cpp:208: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `make_window_cv':
/home/user/darknet/src/image_opencv.cpp:216: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/local/bin/ld: /home/user/darknet/src/image_opencv.cpp:218: undefined reference to `cv::setWindowProperty(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, double)'
/usr/local/bin/ld: /home/user/darknet/src/image_opencv.cpp:220: undefined reference to `cv::resizeWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, int)'
/usr/local/bin/ld: /home/user/darknet/src/image_opencv.cpp:221: undefined reference to `cv::moveWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, int)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `cv_capture_from_file':
/home/user/darknet/src/image_opencv.cpp:283: undefined reference to `cv::VideoCapture::VideoCapture(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `is_invalid_file_to_imread_cv':
/home/user/darknet/src/image_opencv.cpp:397: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `load_image_cv':
/home/user/darknet/src/image_opencv.cpp:422: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `save_image_jpg_cv':
/home/user/darknet/src/image_opencv.cpp:489: undefined reference to `cv::imwrite(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `mark_image_and_save_cv':
/home/user/darknet/src/image_opencv.cpp:274: undefined reference to `cv::imwrite(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/examples/detector.c.o: in function `test_ddetector':
/home/user/darknet/examples/detector.c:756: undefined reference to `empty'
collect2: error: ld returned 1 exit status
--- darknet ---
*** [darknet] Error code 1

make[2]: stopped in /usr/home/user/darknet/build
1 error

make[2]: stopped in /usr/home/user/darknet/build
--- CMakeFiles/darknet.dir/all ---
*** [CMakeFiles/darknet.dir/all] Error code 2

make[1]: stopped in /usr/home/user/darknet/build
1 error

make[1]: stopped in /usr/home/user/darknet/build
*** [all] Error code 2

make: stopped in /usr/home/user/darknet/build
1 error

make: stopped in /usr/home/user/darknet/build

Tried many online solutions, nothing works.

I haven't tried the GLIBCXX_USE_CXX17_ABI=0 solution since I use FreeBSD 13.1 ports version of OpenCV 4.6 and not sure how to add this line of code during compilation from source, since we install from source with the following:

make install clean

Installing the GitHub version of OpenCV does not work and requires a lot of patching for FreeBSD...

Not sure how to solve the issue.

Thanks.

zacario-li

zacario-li commented on Oct 10, 2022

@zacario-li

Hello I get the same issue on FreeBSD 13.1:

[ 98%] Building CXX object CMakeFiles/darknet_l.dir/src/image_opencv.cpp.o
[ 99%] Building C object CMakeFiles/darknet.dir/examples/darknet.c.o
[100%] Linking CXX static library libdarknet_s.a
[100%] Built target darknet_s
[100%] Linking CXX shared library libdarknet_l.so
[100%] Linking CXX executable darknet
[100%] Built target darknet_l
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `open_video_stream':
/home/user/darknet/src/image_opencv.cpp:186: undefined reference to `cv::VideoCapture::VideoCapture(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `show_image_frame_cv':
/home/user/darknet/src/image_opencv.cpp:208: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `make_window_cv':
/home/user/darknet/src/image_opencv.cpp:216: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/local/bin/ld: /home/user/darknet/src/image_opencv.cpp:218: undefined reference to `cv::setWindowProperty(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, double)'
/usr/local/bin/ld: /home/user/darknet/src/image_opencv.cpp:220: undefined reference to `cv::resizeWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, int)'
/usr/local/bin/ld: /home/user/darknet/src/image_opencv.cpp:221: undefined reference to `cv::moveWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int, int)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `cv_capture_from_file':
/home/user/darknet/src/image_opencv.cpp:283: undefined reference to `cv::VideoCapture::VideoCapture(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `is_invalid_file_to_imread_cv':
/home/user/darknet/src/image_opencv.cpp:397: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `load_image_cv':
/home/user/darknet/src/image_opencv.cpp:422: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `save_image_jpg_cv':
/home/user/darknet/src/image_opencv.cpp:489: undefined reference to `cv::imwrite(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/src/image_opencv.cpp.o: in function `mark_image_and_save_cv':
/home/user/darknet/src/image_opencv.cpp:274: undefined reference to `cv::imwrite(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'
/usr/local/bin/ld: CMakeFiles/darknet.dir/examples/detector.c.o: in function `test_ddetector':
/home/user/darknet/examples/detector.c:756: undefined reference to `empty'
collect2: error: ld returned 1 exit status
--- darknet ---
*** [darknet] Error code 1

make[2]: stopped in /usr/home/user/darknet/build
1 error

make[2]: stopped in /usr/home/user/darknet/build
--- CMakeFiles/darknet.dir/all ---
*** [CMakeFiles/darknet.dir/all] Error code 2

make[1]: stopped in /usr/home/user/darknet/build
1 error

make[1]: stopped in /usr/home/user/darknet/build
*** [all] Error code 2

make: stopped in /usr/home/user/darknet/build
1 error

make: stopped in /usr/home/user/darknet/build

Tried many online solutions, nothing works.

I haven't tried the GLIBCXX_USE_CXX17_ABI=0 solution since I use FreeBSD 13.1 ports version of OpenCV 4.6 and not sure how to add this line of code during compilation from source, since we install from source with the following:

make install clean

Installing the GitHub version of OpenCV does not work and requires a lot of patching for FreeBSD...

Not sure how to solve the issue.

Thanks.

cmake -D GLIBCXX_USE_CXX11_ABI=0

subrahmanya01

subrahmanya01 commented on Nov 7, 2022

@subrahmanya01

If you are using Visual Studio Code
Try to change the kit used for project. select Visual Studio Build Tool 2017 Release-x86_amd64. It uses compilers 15.9.37( x86_x64 architecture)

It worked for me...

Byxs20

Byxs20 commented on Nov 9, 2023

@Byxs20

gcc version: gcc.exe (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.5.0

CMakeLists.txt:

`
cmake_minimum_required(VERSION 3.10)
project(main)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

set(OpenCV_DIR "D:/Tensorrt/Opencv/opencv453/build")
find_package(OpenCV QUIET)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")

include_directories(
"${OpenCV_DIR}/include"
)

add_executable(main main.cpp)

find_package(fmt CONFIG REQUIRED)

target_link_libraries(main PRIVATE
"${OpenCV_DIR}/x64/vc15/lib/opencv_world453.lib"
fmt::fmt
fmt::fmt-header-only
)
`

[main] 正在配置项目: Step2
[proc] 执行命令: "D:\Program Files\CMake\bin\cmake.exe" --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_TOOLCHAIN_FILE:STRING=D:/vcpkg/scripts/buildsystems/vcpkg.cmake -DVCPKG_TARGET_TRIPLET:STRING=x64-windows -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=D:\mingw64\bin\gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=D:\mingw64\bin\g++.exe -SD:/Source/Learn_Vcpkg/Step2 -Bd:/Source/Learn_Vcpkg/Step2/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- OpenCV library status:
[cmake] -- version: 4.5.3
[cmake] -- libraries:
[cmake] -- include path:
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: D:/Source/Learn_Vcpkg/Step2/build

build:
[build] [ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.obj [build] [100%] Linking CXX executable main.exe [build] D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.5.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0x42): undefined reference to cv::imread(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int)'
[build] D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.5.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0x5d): undefined reference to cv::Mat::empty() const' [build] D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.5.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0xa0): undefined reference to cv::imshow(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, cv::_InputArray const&)'
[build] D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.5.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0xba): undefined reference to cv::waitKey(int)' [build] D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.5.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0xc2): undefined reference to cv::Mat::~Mat()'
[build] D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.5.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0xfc): undefined reference to cv::Mat::~Mat()' [build] D:/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.5.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\main.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0x10c): undefined reference to cv::Mat::~Mat()'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\main.dir\build.make:101: main.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/main.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:90: all] Error 2`

berak

berak commented on Nov 9, 2023

@berak
Contributor

@Byxs20 ,

"${OpenCV_DIR}/x64/vc15/lib/opencv_world453.lib"

you cannot use this with mingw, it is for VS compilers only
(to use opencv, you must first build the libs from src)

Byxs20

Byxs20 commented on Nov 9, 2023

@Byxs20

@berak
Thanks for your help!
You're right, I'm using this repository to build the already-built. link: https://github.com/huihut/OpenCV-MinGW-Build
Already fixed the linking problem.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @berak@sergiud@alalek@agavrel@Scharfsinnig

      Issue actions

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