构建一个包含 opencv 库的 cmake c++ 项目

Building a cmake c++ project that includes opencv librarie

我正在使用 CMake 构建一个 c++ 项目,该项目依赖于使用 Vcpkg 安装的 OpenCV 库。

这是我的 CMakeList.txt 文件:

# 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 文件:

#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;
}

我正在使用这些命令来构建这些项目:

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

这就是我得到的:

\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

我必须运行那些命令:

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

cmake --build . --config Release

很快,您无法使用 -G "MinGW Makefiles"

目前,vcpkg 不提供对 MinGW 的支持。

它是 requested, but eventually it has not been implemented 并且 vcpkg 维护者的结论性评论包括对任何想再次选择 MinGW 支持的人的建议:

However, we have not looked much into using mingw so far. If you have a functioning CMake toolchain file, you can follow our triplet documentation[1] to create a triplets/x64-windows-mingw.cmake file that will use that toolchain to build libraries.

[1] https://github.com/Microsoft/vcpkg/blob/master/docs/users/triplets.md