Search code examples
c++opencvcmakemingwvcpkg

Building a cmake c++ project that includes opencv librarie


I am building a c++ project with CMake, this project depends on OpenCV library that was installed using Vcpkg.

Here is my CMakeList.txt file :

# CMakeList.txt : CMake project for CMakeProject2, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
set( CMAKE_TOOLCHAIN_FILE "C:/vcpkg/scripts/buildsystems/vcpkg.cmake" )


# Find Package
find_package( OpenCV REQUIRED )

# Additional Include Directories
include_directories( ${OpenCV_INCLUDE_DIRS} )

message("hello world " ${OpenCV_LIB_DIR} ${OpenCV_LIBS})

# Additional Library Directories
link_directories( ${OpenCV_LIB_DIR} )
link_libraries(${OpenCV_LIBS})


#set( OpenCV_DIR "C:/vcpkg/installed/x64-windows/share/opencv" )
# Add source to this project's executable.
add_executable (CMakeProject2 "CMakeProject2.cpp" "CMakeProject2.h")

# Additional Dependencies
target_link_libraries( CMakeProject2 ${OpenCV_LIBS} )

CMakeProject2.cpp file :

#include "CMakeProject2.h"
#include <iostream>
#include "opencv2/opencv.hpp"

using namespace cv;

using namespace std;

int main()
{
    cout << "OpenCV version : " << CV_VERSION << endl;
    cout << "Hello CMake." << endl;
    return 0;
}

I am using these commands to build these project :

  1. cmake ..\CMakeProject2 -G "MinGW Makefiles" -DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake
  2. make

and this is what i get :

\CMakeProject2\build>make
[ 50%] Linking CXX executable CMakeProject2.exe
CMakeFiles\CMakeProject2.dir/objects.a(CMakeProject2.cpp.obj):CMakeProject2.cpp:(.text$_ZN2cv6StringD1Ev[_ZN2cv6StringD1Ev]+0x11): undefined reference to `cv::String::deallocate()'
CMakeFiles\CMakeProject2.dir/objects.a(CMakeProject2.cpp.obj):CMakeProject2.cpp:(.text$_ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x25): undefined reference to `cv::String::deallocate()'
collect2.exe: error: ld returned 1 exit status
make[2]: *** [CMakeProject2.exe] Erreur 1
make[1]: *** [CMakeFiles/CMakeProject2.dir/all] Erreur 2
make: *** [all] Erreur 2

Solution

  • I had to run those commands :

    cmake -G "Visual Studio 15 2017 Win64" -DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake ..
    

    and

    cmake --build . --config Release