opencv undefined reference to imread with CMake on windows10 VS Code - c++

I want to run OpenCV on VS Code IDE with CMake, but it shows undefined reference to cv::imread(). I've tried several ways (e.g edit PATH variables...) to solve the problem, however it still can link to the opencv library.
Here is my CMakelists.txt
# cmake needs this line
cmake_minimum_required(VERSION 2.8)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Define project name
project(cvtest)
# Find OpenCV, you may need to set OpenCV_DIR variable
# to the absolute path to the directory containing OpenCVConfig.cmake file
# via the command line or GUI
find_package(OpenCV REQUIRED)
set(OpenCV_INCLUDE_DIRS "C:/opencv/build/include")
include_directories(${OpenCV_INCLUDE_DIRS})
# If the package has been found, several variables will
# be set, you can find the full list with descriptions
# in the OpenCVConfig.cmake file.
# 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}")
# Declare the executable target built from your sources
add_executable(cvtest cvtest.cpp)
# Link your application with OpenCV libraries
target_link_libraries(cvtest ${OpenCV_LIBS})
After finishing CMake settings, I run $ cmake -G "MinGW Makefiles".
-- OpenCV library status:
-- version: 2.4.13.6
-- libraries: opencv_videostab;opencv_video;opencv_ts;opencv_superres;opencv_stitching;opencv_photo;opencv_ocl;opencv_objdetect;opencv_nonfree;opencv_ml;opencv_legacy;opencv_imgproc;opencv_highgui;opencv_gpu;opencv_flann;opencv_features2d;opencv_core;opencv_contrib;opencv_calib3d
-- include path: C:/opencv/build/include
At last, I run $ mingw32-make, but it came out with some errors
Scanning dependencies of target cvtest
[ 50%] Building CXX object CMakeFiles/cvtest.dir/cvtest.cpp.obj
[100%] Linking CXX executable cvtest.exe
CMakeFiles\cvtest.dir/objects.a(cvtest.cpp.obj):cvtest.cpp:(.text+0x63):
undefined reference to cv::imread(std::string const&, int)'
CMakeFiles\cvtest.dir/objects.a(cvtest.cpp.obj):cvtest.cpp:(.text+0x87):
undefined reference to cv::_InputArray::_InputArray(cv::Mat const&)'
CMakeFiles\cvtest.dir/objects.a(cvtest.cpp.obj):cvtest.cpp:(.text+0xc1):
undefined reference to cv::imshow(std::string const&, cv::_InputArray const&)'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:
CMakeFiles\cvtest.dir/objects.a(cvtest.cpp.obj): bad reloc address 0xf in section `.text$_ZN2cv3MatD1Ev[__ZN2cv3MatD1Ev]'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe:
final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
CMakeFiles\cvtest.dir\build.make:104: recipe for target 'cvtest.exe' failed
mingw32-make[2]: *** [cvtest.exe] Error 1
CMakeFiles\Makefile2:71: recipe for target 'CMakeFiles/cvtest.dir/all' failed
mingw32-make[1]: *** [CMakeFiles/cvtest.dir/all] Error 2
Makefile:82: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
Here is cvtest.cpp
#include <cstdio>
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main(int argc, char *argv[]) {
Mat img = imread("lena.bmp", CV_LOAD_IMAGE_COLOR);
imshow("lena", img);
}
Did I misunderstanding something I should know and how can I correct it ? Thanks !

Related

CMake & CodeSynthesis Header-only library not compiling

I have been trying to implement the runtime library which is a header-only library of CodeSynthesis. But I only get a linking compiling error whenever I try to run the generated files which are made by the XSD executable.
Here is the error to show you that I have a linking problem with the runtime library:
[100%] Linking CXX executable XercesGebeuren.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\XercesGebeuren.dir/objects.a(hello.cxx.obj): in function `xsd::cxx::xml::initialize()':
C:/Users/husey/Desktop/XercesGebeuren/xsd-4.0.0-i686-windows/libxsd/xsd/cxx/xml/elements.hxx:84: undefined reference to `xercesc_3_2::XMLPlatformUtils::Initialize(char const*, char const*, xercesc_3_2::PanicHandler*, xercesc_3_2::MemoryManager*)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\XercesGebeuren.dir/objects.a(hello.cxx.obj): in function `xsd::cxx::xml::terminate()':
C:/Users/husey/Desktop/XercesGebeuren/xsd-4.0.0-i686-windows/libxsd/xsd/cxx/xml/elements.hxx:90: undefined reference to `xercesc_3_2::XMLPlatformUtils::Terminate()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\XercesGebeuren.dir/objects.a(hello.cxx.obj): in function `xsd::cxx::xml::sax::std_input_stream::std_input_stream(std::istream&)':
C:/Users/husey/Desktop/XercesGebeuren/xsd-4.0.0-i686-windows/libxsd/xsd/cxx/xml/sax/std-input-source.hxx:27: undefined reference to `xercesc_3_2::BinInputStream::BinInputStream()'
---- [And so on] ----
collect2.exe: error: ld returned 1 exit status
mingw32-make[3]: *** [CMakeFiles\XercesGebeuren.dir\build.make:122: XercesGebeuren.exe] Error 1
mingw32-make[2]: *** [CMakeFiles\Makefile2:95: CMakeFiles/XercesGebeuren.dir/all] Error 2
mingw32-make[1]: *** [CMakeFiles\Makefile2:102: CMakeFiles/XercesGebeuren.dir/rule] Error 2
mingw32-make: *** [Makefile:137: XercesGebeuren] Error 2
I have followed the guide given by CodeSynthesis to generate my hello.cxx & hello.hxx files with the command: xsd cxx-tree --std c++11 hello.xsd
CMake
cmake_minimum_required(VERSION 3.17)
project(XercesGebeuren)
set(CMAKE_CXX_STANDARD 20)
set(Xsd_DIR ./cmake)
find_package(XercesC REQUIRED)
find_package(Xsd REQUIRED)
add_executable(XercesGebeuren main.cpp src/hello.cxx)
add_library(mylib INTERFACE)
target_include_directories(mylib PUBLIC INTERFACE "./libxsd/")
target_include_directories(XercesGebeuren PUBLIC ${XSD_INCLUDE_DIR})
target_link_libraries(XercesGebeuren PUBLIC mylib)
Thanks to 'Asteroids With Wings' I figured out that a header-only library should not be added as a library in CMake.
Including the library and linking the XercesC library was enough.
CMake solution
cmake_minimum_required(VERSION 3.17)
project(XercesGebeuren)
set(CMAKE_CXX_STANDARD 20)
set(Xsd_DIR ./cmake)
find_package(XercesC REQUIRED)
find_package(Xsd REQUIRED)
add_executable(XercesGebeuren main.cpp src/hello.cxx )
target_include_directories(XercesGebeuren PUBLIC ${XSD_INCLUDE_DIR})
target_link_libraries(XercesGebeuren PUBLIC XercesC::XercesC)

how avoid automatic linking of Qt5 libraries in cmake?

I am intending to compile the following opencv code:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
printf("hello world \n");
return 0;
}
My CMakeLists.txt is the following:
cmake_minimum_required(VERSION 2.8)
find_package( OpenCV REQUIRED )
project( main )
add_executable(main main.cpp )
target_link_libraries( main ${OpenCV_LIBS} )
When compiled the result is as follows:
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
/usr/bin/ld: cannot find -lQt5::Core
/usr/bin/ld: cannot find -lQt5::Gui
/usr/bin/ld: cannot find -lQt5::Widgets
/usr/bin/ld: cannot find -lQt5::Test
/usr/bin/ld: cannot find -lQt5::Concurrent
/usr/bin/ld: cannot find -lQt5::OpenGL
collect2: error: ld returned 1 exit status
CMakeFiles/main.dir/build.make:143: recipe for target 'main' failed
make[2]: *** [main] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/main.dir/all' failed
make[1]: *** [CMakeFiles/main.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I believe that CMake in automatic way include Qt5 libraries, I would want to know how avoid this situation.
Ideology of CMake is to never add something that is not required. It is possible that Qt libs were added to configuration list of CMake.
They might be featured in standard variable you use. You can print content by
message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
Configuration file looks OpenCVConfig.cmake.in and may include other files which for some reason were changed.

Can't add locally compiled library to CMake project

As part of a project I'm working on, I need to make use of the WebKitGTK+ library.
I downloaded the library (tarball) and compiled it as described here.
After the compilation was done:
The lib's headers are in /usr/local/include.
The lib's .so files are in /usr/local/lib.
In my C++ project I tried to add the following CMakeLists.txt file:
cmake_minimum_required(VERSION 3.7)
project(CVE_2016_4657)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(CVE_2016_4657 ${SOURCE_FILES})
find_package(PkgConfig REQUIRED)
include_directories(/usr/local/include/webkitgtk-4.0)
link_directories(/usr/local/lib/webkit2gtk-4.0)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
add_definitions(${GTK3_CFLAGS_OTHER})
pkg_check_modules(SOUP REQUIRED libsoup-2.4)
include_directories(${SOUP_INCLUDE_DIRS})
link_directories(${SOUP_LIBRARY_DIRS})
add_definitions(${SOUP_CFLAGS_OTHER})
target_link_libraries(
CVE_2016_4657
${GTK3_LIBRARIES}
${SOUP_LIBRARIES})
However, when compiling the project I'm getting the following error:
[ 50%] Linking CXX executable CVE_2016_4657
CMakeFiles/CVE_2016_4657.dir/main.cpp.o: In function `main':
/home/idanas/CLionProjects/Switcheroo/main.cpp:17: undefined reference to
`webkit_web_view_get_type'
/home/idanas/CLionProjects/Switcheroo/main.cpp:17: undefined reference to
`webkit_web_view_new'
/home/idanas/CLionProjects/Switcheroo/main.cpp:29: undefined reference to
`webkit_web_view_load_uri'
collect2: error: ld returned 1 exit status
CMakeFiles/CVE_2016_4657.dir/build.make:94: recipe for target
'CVE_2016_4657' failed
make[3]: *** [CVE_2016_4657] Error 1
CMakeFiles/Makefile2:67: recipe for target
'CMakeFiles/CVE_2016_4657.dir/all' failed
make[2]: *** [CMakeFiles/CVE_2016_4657.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target
'CMakeFiles/CVE_2016_4657.dir/rule' failed
make[1]: *** [CMakeFiles/CVE_2016_4657.dir/rule] Error 2
Makefile:118: recipe for target 'CVE_2016_4657' failed
make: *** [CVE_2016_4657] Error 2
I have little-to-none experience with CMake and could really use some help.
You are trying to use WebKitGTK+, which is a separate library from both GTK+ and libsoup. You will need to duplicate your pkg_check_modules() code again for WebKitGTK+. You'll need to determine first if you are using WebKit1 or WebKit2 (they have subtly different APIs), and then find the appropriate pkg-config name for that version of WebKitGTK+; check the documentation and the contents of your /usr/lib/pkgconfig directory.

Why I cannot use boost correctly with CMake?

I will try to explain in detail the difficulties I encountered.
Recently I want to use boost libraries in Ubuntu Mint 18 x64. So I download the newest version of it which is 1.62.0. Sha256sum is fine. Then I started compiled it with "./bootstrap.sh" and "./b2". Finally copied then to "/usr/local" with "sudo ./b2 install". So far so good.
I am using Clion right now, so new job is to add libraries in Cmakelist.txt. Code below is what I have added.
set(BOOST_ROOT /usr/local)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.62.0)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(BoostTest main.cpp)
target_link_libraries(BoostTest ${Boost_LIBRARIES})
endif()
First thing I want to explain is that I know it is not good to set up a specific location in CMake, but without it Cmake cannot find it. So I just do that. And, at this moment, CMake did not post any error info. Here is my hello_world demo.
#include <iostream>
#include <boost/thread/testable_mutex.hpp>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
I just include a .hpp for test, nothing else. When I attemped to run it. I got these.
-- Boost version: 1.62.0
-- Configuring done
-- Generating done
-- Build files have been written to: /home/vita-nove/ClionProjects/BoostTest/cmake-build-debug
[ 50%] Building CXX object CMakeFiles/BoostTest.dir/main.cpp.o
[100%] Linking CXX executable BoostTest
CMakeFiles/BoostTest.dir/main.cpp.o: In function `__static_initialization_and_destruction_0(int, int)':
/usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
collect2: error: ld returned 1 exit status
CMakeFiles/BoostTest.dir/build.make:94: recipe for target 'BoostTest' failed
make[3]: *** [BoostTest] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/BoostTest.dir/all' failed
make[2]: *** [CMakeFiles/BoostTest.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/BoostTest.dir/rule' failed
make[1]: *** [CMakeFiles/BoostTest.dir/rule] Error 2
Makefile:118: recipe for target 'BoostTest' failed
make: *** [BoostTest] Error 2
Wired thing is that I do find some .hpp file in External Libraries. They are all located in /include/boost. In /usr/local/lib and /usr/local/include/boost everything shows up as I expect.
Sorry for my bad English. After spending nearly whole day searching and testing I cannot solve it. I don't know what dependencies I have missed or something else. Anyway, Thank you for reading this, hope someone will help.
You have to tell cmake that you need the thread library
set(BOOST_ROOT /usr/local)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost 1.62.0 COMPONENTS thread system) # <-- here
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(BoostTest main.cpp)
target_link_libraries(BoostTest ${Boost_LIBRARIES})
endif()

cmake and external library

I try to use cmake with an external library but i can't. I use minGW and g++ compiler.
My project is organised as follow:
project/
CMakeLists.txt
src
main.cpp
lib
win
x86
SDL2-2.0.3 (contains SDL2.dll, SDL2.lib, SDL2main.lib and SDL2test.lib
include
SDL2 (contains all header)
bin
The cmake generator works `cmake -G"Eclipse CDT4 - Unix Makefiles"
My cmake file:
`
#
# File generated by CMakeBuilder
#
#
cmake_minimum_required(VERSION 2.6)
PROJECT ( project CXX )
################ INCLUDE Libs ####################
SET (CMAKE_INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include/")
SET (CMAKE_LINKER_PATH "${PROJECT_SOURCE_DIR}/lib/win/x86/SDL2-2.0.3/")
message(STATUS "INCLUDE_PATH: ${CMAKE_INCLUDE_PATH}" )
message(STATUS "LINKER_PATH: ${CMAKE_LINKER_PATH}" )
INCLUDE_DIRECTORIES(${CMAKE_INCLUDE_PATH})
LINK_DIRECTORIES(${CMAKE_LINKER_PATH})
set (CMAKE_CXX_FLAGS "-lSDL2main -lSDL2 -lmingw32 ")
#Generate file source
file( GLOB_RECURSE source_files src/*)
ADD_EXECUTABLE(project ${source_files})
set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})
But when i launch make, i obtain this error:
`
CMakeFiles/project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x0): multiple
definition of main
C:/Users/Gege/workspaces/workspaceCPP/project/lib/win/x86/SDL2-2.0.3/SDL2m
ain.lib(./Release/SDL_windows_main.obj):(.text[_main]+0x0): first defined here
Warning: corrupt .drectve at end of def file
C:/Users/Gege/workspaces/workspaceCPP/project/lib/win/x86/SDL2-2.0.3/SDL2m
ain.lib(./Release/SDL_windows_main.obj):(.text[_main]+0x12): undefined reference
to SDL_main'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:/Users/Gege/workspaces/workspaceCPP/project/lib/win/x86/SDL2- 2.0.3/SDL2main.lib(./
Release/SDL_windows_main.obj): bad reloc address 0x8 in section .text[_WinMain#
16]'
collect2.exe: error: ld returned 1 exit status
CMakeFiles/project.dir/build.make:87: recipe for target 'bin/project.exe'
failed
make[2]: *** [bin/project.exe] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/project.dir/all' faile
d
make[1]: *** [CMakeFiles/arduinoGeo.dir/all] Error 2
Makefile:75: recipe for target 'all' failed
make: *** [all] Error 2
`
Actually i don't want to put my lib in minGW folder to keep the build easy for the others.
Thanks for you help