记录一下相关的cpp开发的配置,在QtCreator里使用CMake创建MainWindow项目,并能运行程序,在导入相关的库,比如OpenCV或Libssh2 等, 可以查看 按照OpenCVConfig.cmake里的配置 和vcpkg里安装libssh2的配置 比如OpenCVConfig.cmake里
# FIND_PACKAGE(OpenCV REQUIRED)
# TARGET_LINK_LIBRARIES(MY_TARGET_NAME ${OpenCV_LIBS})
那么添加opencv的dir目录到cmake_prefix_path 以及相应的target_link_libraries配置
list(APPEND CMAKE_PREFIX_PATH "D:/opencv/build")
find_package(OpenCV REQUIRED )
类似的查看安装libssh2里的配置以及相应的target_link_libraries
E:\ld\vcpkg-master>vcpkg install libssh2
Computing installation plan...
The following packages are already installed:
libssh2[core,zlib,openssl]:x64-windows@1.11.0#1
libssh2:x64-windows is already installed
Total install time: 613 us
libssh2 provides CMake targets:
find_package(Libssh2 CONFIG REQUIRED)
target_link_libraries(main PRIVATE $<IF:$<TARGET_EXISTS:Libssh2::libssh2_shared>,Libssh2::libssh2_shared,Libssh2::libssh2_static>)
添加
list(APPEND CMAKE_PREFIX_PATH "E:/ld/vcpkg-master/installed/x64-windows/")
find_package(Libssh2 CONFIG REQUIRED)
最后统一修改taget_link_libraries
..
target_link_libraries(DemoSSH2_2 PRIVATE Qt5::Widgets PRIVATE $<IF:$<TARGET_EXISTS:Libssh2::libssh2_shared>,Libssh2::libssh2_shared,Libssh2::libssh2_static>
PRIVATE ${OpenCV_LIBS})
这样就配置好了OpenCV和libssh2的环境。
在VSCode里在安装好c++的拓展和VS2017本机命令工具并可以执行cl命令后可以使用 cd project和 code . 命令启动VSCode程序,后在Run/Debug选中cl.exe里的相关提示 会生成一个task.json文件
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
// task.json
这样就完成了c/c++的MSVC环境配置,可以运行HelloWorld
当使用#include <opencv2/opencv.hpp> opencv环境时需要配置c_cpp_properties, 在提示Edit includePath settings里修改Include path 里的配置为
${workspaceFolder}/**
D:/opencv/build
D:/opencv/build/include
这样可以不会报错include <opencv2/opencv.hpp> 后还是运行不了程序,需要配置lib dll链接,这时会生成一个c_cpp_properties.json文件
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/opencv/build",
"D:/opencv/build/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "cl.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-msvc-x64"
}
],
"version": 4
}
// c_cpp_properties.json
之前操作会将选中的includePath 导入到json的includePath里,再修改task.json文件的args 项添加“/I” 和 “/link”, 其它不变,args项如下
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/I", "D:/opencv/build/include",
"/I", "D:/opencv/build/include/opencv2",
"/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}",
"/link",
"D:/opencv/build/x64/vc14/lib/opencv_world410.lib",
"/link",
"D:/opencv/build/x64/vc14/lib/opencv_world410d.lib"
],
修改后完整的task.json如下
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/I", "D:/opencv/build/include",
"/I", "D:/opencv/build/include/opencv2",
"/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}",
"/link",
"D:/opencv/build/x64/vc14/lib/opencv_world410.lib",
"/link",
"D:/opencv/build/x64/vc14/lib/opencv_world410d.lib"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
// tasks.json
这样就可以使用opencv的环境了,需要注意使用VS2017本机提示符和opencv的版本有x86和x64区别,不同时会报错
LINK : warning LNK4044: ...
opencv_world410.lib(opencv_world410.dll) : fatal error LNK1112: ...
... 是乱码的