- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 56.2k
Description
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 commentedon Oct 31, 2018
Compare exact full compiler/linker command lines.
Con-Mi commentedon Oct 31, 2018
@alalek what do you mean?
Compile for example with g++ with pkg-config?
alalek commentedon Oct 31, 2018
No.
Use
make VERBOSE=1
and compare logs for both cases.Con-Mi commentedon 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 commentedon Nov 1, 2018
These lines are required for OpenCV 4.0 apps:
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 commentedon 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 commentedon Nov 16, 2018
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 commentedon 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:
Current minimal supported CMake version is 3.5.x (Ubuntu 16.04 has 3.5.1 by default).
sergiud commentedon Nov 16, 2018
You are not supposed to use
cxx_std_11
, but explicit feature requirements such ascxx_auto_type
etc.zacario-li commentedon Jan 8, 2019
rebuild your opencv4 from source with "add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)", have fun.
hoboh commentedon 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 commentedon Jan 16, 2020
@alalek I also tried u mentioned
It shows
So what happened?
alalek commentedon Jan 16, 2020
I believe there is mess with
std::string
vsstd::__cxx11::string
.Check compiler command line options too (before linking).
Try
-std=c++11
or other options from the comments above.hoboh commentedon Jan 16, 2020
@alalek It has been set in the CMakeLists.txt
13 remaining items
gezabohus commentedon 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 commentedon Aug 3, 2020
It works for me. Thanks a lot. ^O^
latuanvinh1998 commentedon Dec 7, 2020
Same issue solved for me : pytorch/pytorch#14620 (comment)
Vibhuarvind commentedon Apr 8, 2021
bro i am getting this error on colab so how do i run it on colab using g++?
ingbeeedd commentedon 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.
xun-dao commentedon Apr 3, 2022
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:
RL-arch commentedon Aug 8, 2022
Hi
if I use c++ 17 for example, do I need to add GLIBCXX_USE_CXX17_ABI=0?
zacario-li commentedon Aug 9, 2022
yes
rajhlinux commentedon Oct 9, 2022
Hello I get the same issue on FreeBSD 13.1:
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 commentedon Oct 10, 2022
cmake -D GLIBCXX_USE_CXX11_ABI=0
subrahmanya01 commentedon Nov 7, 2022
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 commentedon Nov 9, 2023
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 commentedon Nov 9, 2023
@Byxs20 ,
you cannot use this with mingw, it is for VS compilers only
(to use opencv, you must first build the libs from src)
Byxs20 commentedon Nov 9, 2023
@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.