OpenCV 3.0 undefined reference error? - c++

I've just followed the instructions to install opencv 3 on my ubuntu computer (I've already installed and ran programs using opencv 3 on a different linux device), however, when I tried to run one of my test programs from my old computer I get the following errors:
CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `main':
VideoTest.cpp:(.text+0x6f): undefined reference to `cv::VideoWriter::fourcc(char,char,char, char)'
VideoTest.cpp:(.text+0xc3): undefined reference to `cv::VideoWriter::open(cv::String const&, int, double, cv::Size_<int>, bool)'
VideoTest.cpp:(.text+0x103): undefined reference to `cv::namedWindow(cv::String const&, int)'
VideoTest.cpp:(.text+0x146): undefined reference to `cv::VideoCapture::read(cv::_OutputArray const&)'
VideoTest.cpp:(.text+0x1b1): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `cv::String::String(char const*)':
VideoTest.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x3b): undefined reference to `cv::String::allocate(unsigned int)'
CMakeFiles/VideoTest.dir/VideoTest.cpp.o: In function `cv::String::~String()':
VideoTest.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0xd): undefined reference to `cv::String::deallocate()'
collect2: ld returned 1 exit status
make[2]: *** [VideoTest] Error 1
make[1]: *** [CMakeFiles/VideoTest.dir/all] Error 2
make: *** [all] Error 2
Any idea what's going on? I'm relatively new to opencv. Here's my test code for reference:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[]){
VideoCapture vCap(0);
VideoWriter vWrite;
vWrite.open("test.avi", vWrite.fourcc('M','J','P','G'), 20, Size(640, 480), true);
while (1) {
namedWindow("VideoFeed", WINDOW_AUTOSIZE);
Mat frame;
vCap.read(frame);
vWrite.write(frame);
imshow("VideoFeed", frame);
char c = waitKey(50);
if (c == 27) {
break;
}
}
return 0;
}
Thanks in advance.
Edit: My CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(VideoTest)
find_package(OpenCV REQUIRED)
add_executable(VideoTest VideoTest.cpp)
target_link_libraries(VideoTest ${OpenCV_LIBS})

VideoCapture and VideoWriter were moved to the videoio module in 3.0, so you have to additionally
#include "opencv2/videoio.hpp"
also, you don't seem to link to any of the required opencv libs, those are:
opencv_core, opencv_videoio, opencv_highgui

Alright, I've finally figured out what's going on. Something internally in the CMakeFiles folder went screwy when I moved the directory over to my new system. Removing all files/directories besides the project .cpp file and the CMakeLists.txt file, and running "cmake ." and "make" again seems to solve the problem.

Below cmake file works for me.
cmake_minimum_required(VERSION 3.5)
project(ImageProcessing)
set(CMAKE_CXX_STANDARD 14)
find_package(OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
set(SOURCE_FILES main.cpp)
add_executable(ImageProcessing ${SOURCE_FILES})
target_link_libraries( ImageProcessing ${OpenCV_LIBS} )
# Print some message showing some of them
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")

Related

Undefined reference when using OpenCV and CMake in VSCode

Im trying to use OpenCV in VSCode and followed this videos instructions: https://www.youtube.com/watch?v=m9HBM1m_EMU&t=3s&ab_channel=NicolaiNielsen-ComputerVision%26AI
Difference from the video is im using mingw compiler and im unable to get the msvc compiler (Not the issue though).
main.cpp:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
std::cout << "aa" << std::endl;
std::string img = "lenna.jpg";
Mat srcImage = imread(img);
if (!srcImage.data) {
return 1;
}
imshow("srcImage", srcImage);
waitKey(0);
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.0)
project(OpenCV_Test VERSION 0.1.0)
include(CTest)
enable_testing()
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(OpenCV_Test main.cpp)
target_link_libraries(OpenCV_Test ${OpenCV_LIBS})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
Im able to get it to compile and output "aa" if i remove everything with OpenCV, but adding it gives me this compile error:
[main] Building folder: OpenCVTest
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/Chris/Desktop/OpenCVTest/build --config Debug --target all -j 10 --
[build] Consolidate compiler generated dependencies of target OpenCV_Test
[build] [ 50%] Building CXX object CMakeFiles/OpenCV_Test.dir/main.cpp.obj
[build] [100%] Linking CXX executable OpenCV_Test.exe
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/OpenCV_Test.dir/objects.a(main.cpp.obj):C:/Users/Chris/Desktop/OpenCVTest/main.cpp:12: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/OpenCV_Test.dir/objects.a(main.cpp.obj): in function `main':
[build] C:/Users/Chris/Desktop/OpenCVTest/main.cpp:16: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Chris/Desktop/OpenCVTest/main.cpp:17: undefined reference to `cv::waitKey(int)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Chris/Desktop/OpenCVTest/main.cpp:19: undefined reference to `cv::Mat::~Mat()'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Chris/Desktop/OpenCVTest/main.cpp:19: undefined reference to `cv::Mat::~Mat()'
[build] collect2.exe: error: ld returned 1 exit status
[build] make[2]: *** [CMakeFiles/OpenCV_Test.dir/build.make:117: OpenCV_Test.exe] Error 1
[build] make[1]: *** [CMakeFiles/Makefile2:839: CMakeFiles/OpenCV_Test.dir/all] Error 2
[build] make: *** [Makefile:121: all] Error 2
[build] Build finished with exit code 2

CMake build gives undefined reference to functions in OpenCV

I'm making a jump from python to C++ in vscode. My project utilizes OpenCV and I quickly ran into a lot of problems with trying to get my compiler (mingw-w64) and CMake to find and reference the OpenCV functions.
My test code looks like this. I know I have to change my file path, but when I do I get the same problem.
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
Mat image1,image2;
// Read the file
image1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.jpg");
// Check for invalid input
if(! image1.data )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
//Write the File
imwrite( "C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg",image1);
// Read the Writen File
image2 =imread("C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg");
namedWindow("Image1");
imshow("Image1",image1);
namedWindow("Image2");
imshow("Image2",image2);
waitKey(0);
}
My CMakeLists.txt looks like this
cmake_minimum_required(VERSION 3.0.0)
project(chessbot VERSION 0.1.0)
include(CTest)
enable_testing()
find_package( OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(chessbot main.cpp)
target_link_libraries(chessbot ${OpenCV_LIBS})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
When I use the CMake Build tool (which I believe is supposed to make my executable for me) I get a bunch of undefined references to opencv functions
uild] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:25: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:28: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:29: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:31: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:32: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:33: undefined reference to `cv::waitKey(int)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:33: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:33: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:13: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:25: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:10: undefined reference to `cv::Mat::~Mat()'
[build] CMakeFiles\chessbot.dir/objects.a(main.cpp.obj):C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:10: more undefined references to `cv::Mat::~Mat()' follow
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\chessbot.dir\build.make:115: chessbot.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:838: CMakeFiles/chessbot.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:120: all] Error 2
[build] Build finished with exit code 2
There is another compile tool that doesn't return undefined references and seems to compile just fine, which leads me to believe that CMake cant find the target library for some reason. The learning curve is steep for me. Can someone explain to me the issue? Why cant I reference the OpenCV functions?

linked library is valid in CMakeLists, but doesn't link at compile time

I'm just starting around with messing around with vulkan, and GLFW, but when I try to compile a test program, it gives me a bunch of linker errors:
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Init()':
loops.cpp:(.text+0xd): undefined reference to `glfwInit'
/usr/bin/ld: loops.cpp:(.text+0x1c): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x2b): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x4f): undefined reference to `glfwCreateWindow'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Update()':
loops.cpp:(.text+0xa3): undefined reference to `glfwPollEvents'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::DeInit()':
loops.cpp:(.text+0xcd): undefined reference to `glfwDestroyWindow'
/usr/bin/ld: loops.cpp:(.text+0xd2): undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/vulkan_test.dir/build.make:113: vulkan_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/vulkan_test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(vulkan_test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH /home/headass/CMake_Modules/)
find_package(GLFW REQUIRED)
find_package(Vulkan REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS} ${VULKAN_INCLUDE_DIRS})
add_executable(vulkan_test main.cpp loops.cpp)
target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
Any idea why this is happening? I have both vulkan and GLFW installed, libglfw.so is in my /usr/lib/ directory, and clangd doesn't see anything wrong with it, but it still fails to link properly.
And yes, I HAVE tried googling this, to no avail.
Changing:
target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
to:
target_link_libraries(vulkan_test glfw ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
worked flawlessly

Can't link Boost 1.63.0 through CMake

what I'm trying to do is as you can guess from the title to link Boost libraries through CMake (I'm working with CLion to write cross platform code, so I have no other chance). I am sure I built everything correctly cause when I use it inside Visual Studio it works with no problem at all.
Here's my CMake code:
cmake_minimum_required(VERSION 3.7)
project(BoostHello)
set(BOOST_ROOT C:/boost_1.63.0)
find_package(BOOST 1.6.0 REQUIRED)
include_directories( ${Boost_INCLUDE_DIR} )
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
add_executable(BoostHello ${SOURCE_FILES})
target_link_libraries( BoostHello ${Boost_LIBRARIES} )
And here are my compilation errors:
"C:\Program Files (x86)\JetBrains\CLion 2017.1\bin\cmake\bin\cmake.exe" --build C:\Users\Admin\CLionProjects\BoostHello\cmake-build-debug --target all -- -j 8
Scanning dependencies of target BoostHello
[ 50%] Building CXX object CMakeFiles/BoostHello.dir/main.cpp.obj
[100%] Linking CXX executable BoostHello.exe
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:6: undefined reference to `boost::filesystem::path::root_path() const'
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:7: undefined reference to `boost::filesystem::path::relative_path() const'
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:8: undefined reference to `boost::filesystem::path::filename() const'
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0':
C:/boost_1.63.0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
C:/boost_1.63.0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
C:/boost_1.63.0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function `ZN5boost10filesystem11path_traits7convertEPKwS3_RNSt7__cxx1112basic_stringIcSt1 1char_traitsIcESaIcEEE':
C:/boost_1.63.0/boost/filesystem/path.hpp:989: undefined reference to `boost::filesystem::path::codecvt()'
C:/boost_1.63.0/boost/filesystem/path.hpp:989: 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, int> const&)'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\BoostHello.dir\build.make:96: recipe for target 'BoostHello.exe' failed
mingw32-make.exe[2]: *** [BoostHello.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/BoostHello.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/BoostHello.dir/all' failed
mingw32-make.exe: *** [all] Error 2
Makefile:82: recipe for target 'all' failed
And eventually here's the code I'm trying to compile:
#include <iostream>
#include <boost/filesystem.hpp>
int main(int argc, char** argv) {
boost::filesystem::path myPath = {L"C:/Users/Admin/ClionProjects/BoostHello"};
std::cout << "Root:\t" << myPath.root_path() << std::endl;
std::cout << "Relative:\t" << myPath.relative_path() << std::endl;
std::cout << "Filename:\t" << myPath.filename() << std::endl;
return 0;
}
What am I doing wrong?
I am compiling with MinGW.
Thanks in advance!
Replace
find_package(BOOST 1.6.0 REQUIRED)
by
find_package(Boost 1.63.0 REQUIRED filesystem system)
otherwise CMake doesn't know which boost library to link against.

Error with Poco

I want to use Poco libraries in my C++ project.
But I get an error on compilation.
I'm developing on Ubuntu OS, CLion 1.2.4 IDE
Here it is my source code:
#include "Poco/Net/ServerSocket.h"
#include <iostream>
int main(int arc, char** argv){
std::cout << "Hello world!";
return 0;
}
And this is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.3)
project(rcp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin")
include_directories("libraries")
add_executable(rcp ${SOURCE_FILES})
target_link_libraries(rcp pthread PocoNet PocoUtil PocoFoundation)
And this is the output when I do the compilation:
CMakeFiles/rcp.dir/main.cpp.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::host() const':
main.cpp:(.text._ZNK4Poco3Net4Impl21IPv6SocketAddressImpl4hostEv[_ZNK4Poco3Net4Impl21IPv6SocketAddressImpl4hostEv]+0x11): undefined reference to `Poco::Net::IPAddress::IPAddress(void const*, unsigned int, unsigned int)'
CMakeFiles/rcp.dir/main.cpp.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::~IPv6SocketAddressImpl()':
main.cpp:(.text._ZN4Poco3Net4Impl21IPv6SocketAddressImplD2Ev[_ZN4Poco3Net4Impl21IPv6SocketAddressImplD5Ev]+0x8): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/rcp.dir/main.cpp.o: In function `Poco::Net::Impl::IPv6SocketAddressImpl::~IPv6SocketAddressImpl()':
main.cpp:(.text._ZN4Poco3Net4Impl21IPv6SocketAddressImplD0Ev[_ZN4Poco3Net4Impl21IPv6SocketAddressImplD5Ev]+0xc): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/rcp.dir/main.cpp.o: In function `Poco::Net::Impl::IPv4SocketAddressImpl::~IPv4SocketAddressImpl()':
main.cpp:(.text._ZN4Poco3Net4Impl21IPv4SocketAddressImplD2Ev[_ZN4Poco3Net4Impl21IPv4SocketAddressImplD5Ev]+0x8): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/rcp.dir/main.cpp.o: In function `Poco::Net::Impl::IPv4SocketAddressImpl::~IPv4SocketAddressImpl()':
main.cpp:(.text._ZN4Poco3Net4Impl21IPv4SocketAddressImplD0Ev[_ZN4Poco3Net4Impl21IPv4SocketAddressImplD5Ev]+0xc): undefined reference to `Poco::Net::Impl::SocketAddressImpl::~SocketAddressImpl()'
CMakeFiles/rcp.dir/main.cpp.o:(.rodata._ZTIN4Poco3Net4Impl21IPv4SocketAddressImplE[_ZTIN4Poco3Net4Impl21IPv4SocketAddressImplE]+0x10): undefined reference to `typeinfo for Poco::Net::Impl::SocketAddressImpl'
CMakeFiles/rcp.dir/main.cpp.o:(.rodata._ZTIN4Poco3Net4Impl21IPv6SocketAddressImplE[_ZTIN4Poco3Net4Impl21IPv6SocketAddressImplE]+0x10): undefined reference to `typeinfo for Poco::Net::Impl::SocketAddressImpl'
collect2: error: ld returned 1 exit status
CMakeFiles/rcp.dir/build.make:94: recipe for target '/home/john/projects/rightChoiceProperty/bin/rcp' failed
make[3]: *** [/home/john/projects/rightChoiceProperty/bin/rcp] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/rcp.dir/all' failed
make[2]: *** [CMakeFiles/rcp.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/rcp.dir/rule' failed
make[1]: *** [CMakeFiles/rcp.dir/rule] Error 2
Makefile:118: recipe for target 'rcp' failed
make: *** [rcp] Error 2
Where is my problem I have search all of today but I couldn't find anything please help me to resolve this problem.
Thank you every body
I resolved the problem, as " M.M " guide in above comment:
The output suggests you didn't actually link against Poco::Net.
Another possibility might be if you're using Poco headers from a
different version of Poco than the compiled library you are linking
against
I removed changed the include to include right library.
At first this was my include line:
#include "Poco/Net/ServerSocket.h"
Changed to
#include <Poco/Net/ServerSocket.h>
Everything is working fine now.
Thank you M.M