一、概述
在Windows10上配置一个C++开发环境:
工具:VSCode
编译器:Mingw64(使用gcc进行编译)
构建工具:CMake
第三方库:集成OpenCV、FFmpeg、SDL2
二、操作步骤
1.安装mingw64并配置bin目录到环境变量
2.下载VSCode并安装
3.安装CMake并将bin目录配置到环境变量
4.下载OpenCV、FFmpeg、SDL2库备用
5.安装VSCode插件
6.Ctrl+Shift+P配置C++头文件路径,如果不配置则没有代码提示
{ "configurations": [ { "name": "Win32", "includePath": [ "${workspaceFolder}/**", "D:/ffmpeg/dev/SDL2-2.28.5-dev/x86_64-w64-mingw32/include", "D:/opencv/build_install/install/include", "D:/ffmpeg/dev/ffmpeg-5.1.2-dev/include", "E:/tony/demo/script/cmake_demo/cmake_opencv/src" ], "defines": [ "_DEBUG", "UNICODE", "_UNICODE" ], "compilerPath": "D:/mingw64/bin/gcc.exe", "cStandard": "c89", "cppStandard": "gnu++98", "intelliSenseMode": "windows-gcc-x86" } ], "version": 4 }
7.创建一个CMakeLists.txt构建项目
#参考样例(Cmake保姆级教程):https://subingwen.cn/cmake/CMake-primer/ cmake_minimum_required(VERSION 3.0.0) #设置C++的编译版本,其中CMAKE_CXX_STANDARD是固定的,11代表C++11 set(CMAKE_CXX_STANDARD 11) #设置项目名称 project(opencv_demo VERSION 0.1.0) #定义一个宏在代码中使用 # add_definitions(-DDEBUG) #include(CTest) #enable_testing() set(CURRENT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) message("项目当前路径,也就是CMakeLists.txt所在的路径:"${CURRENT_DIR}) #设置可执行文件的输出路径,会在bin目录下输出scrcpy_demo.exe set(EXECUTABLE_OUTPUT_PATH ${CURRENT_DIR}/bin) #生成一个可构建的执行程序,在根目录下的build文件夹 #这里的项目名scrcpy_demo和project中的(scrcpy_demo)没有一毛钱关系 #设置源码路径 # set(SRC_LIST main.cpp) #包含头文件(其實就是搜索頭文件) # include_directories(${PROJECT_SOURCE_DIR}/src) # #搜索源文件 SRC_LIST代表變量名 # file(GLOB SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c) # message("搜索源码Lu经:"${SRC_LIST}) ##############添加opencv相关类库的支持 #编译opencv(opencv引入始终不成功) include_directories("D:/opencv/build_install/install/include") link_directories("D:/opencv/build_install/install/x64/mingw/bin") #导入sdl2 include_directories("D:/ffmpeg/dev/SDL2-2.28.5-dev/x86_64-w64-mingw32/include") link_directories("D:/ffmpeg/dev/SDL2-2.28.5-dev/x86_64-w64-mingw32/bin") #导入ffmpeg include_directories("D:/ffmpeg/dev/ffmpeg-5.1.2-dev/include") link_directories("D:/ffmpeg/dev/ffmpeg-5.1.2-dev/bin") # file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cpp ${PROJECT_SOURCE_DIR}/*.h) # set(SRC_1 main.cpp) #搜索src目录下的源文件 include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src/sdl/include) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/src/sdl SRC_LIST) #生成可執行文件 add_executable(opencv_demo main.cpp ${SRC_LIST}) target_link_libraries(opencv_demo SDL2 libopencv_calib3d453 libopencv_features2d453 libopencv_gapi453 libopencv_flann453 libopencv_core453 libopencv_dnn453 libopencv_ml453 libopencv_highgui453 libopencv_imgcodecs453 libopencv_imgproc453 #引入ffmpeg avformat-59 avutil-57 avcodec-59 avdevice-59 avfilter-8 ) set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) include(CPack) #message用于输出信息 message("项目名称:"${PROJECT_NAME}) message("项目版本:"${PROJECT_VERSION}) message("项目源码路径"${PROJECT_SOURCE_DIR}) #[[ 这是一个多行注释 ]]
8.创建一个shell脚本用于执行CMakeLists.txt
#!/usr/bin/env bash #自动化运行的脚本 BUILD_DIR='build' #删除build目录 rm -rf $BUILD_DIR #重新编译 -B build代表编译文件存放目录 cmake -B build -G "MinGW Makefiles" echo $BUILD_DIR #进入build目录 cd $BUILD_DIR #执行make编译 mingw32-make #运行可执行文件 ../bin/opencv_demo.exe # ./opencv_demo.exe
9.创建一个main.cpp进行测试:ps:测试代码是在网上随便找的
1.测试OpenCV显示一个图片
2.测试SDL2创建一个原生窗口
3.测试打印ffmpeg的codec的版本号
#include <iostream> #include <opencv2/opencv.hpp> #include "SDL2/SDL.h" extern "C"{ #include <libavcodec/avcodec.h> } #include "create_window.h" using namespace std; using namespace cv; #undef main int main(int argc, char *argv[]){ // CreateWindow *window = new CreateWindow(); // window->showWindow(); cout<< "hello world" <<endl; cv::Mat src = cv::imread("e:/tony/demo/script/cmake_demo/cmake_opencv/horse.jpeg"); if(src.empty()){ cout << "this image is empty"<<endl; } // cvtColor(src,COLOR_BGR2GRAY); cv::imshow("src",src); cv::waitKey(0); // cout <<"Hello SDL2"<< endl; //创建一个SDL_VIDEO窗口 SDL_Window* window = NULL; SDL_Renderer *renderer = NULL;//渲染器 SDL_Texture *texture = NULL;//纹理 SDL_Init(SDL_INIT_VIDEO); window = SDL_CreateWindow("Title", SDL_WINDOWPOS_UNDEFINED, // 默认x y 显示到窗口中间 SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);// 设置窗口属性 if(!window){ printf("不能创建window,%s",SDL_GetError()); } //创建渲染器(一个窗口可以有多个渲染器) renderer = SDL_CreateRenderer(window,-1,0); if(NULL==renderer){ printf("渲染器创建失败:%s",SDL_GetError()); //销毁窗口释放资源 SDL_DestroyWindow(window); SDL_Quit(); return -1; } //基于渲染器创建纹理 texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 640, 480); if(NULL==texture){ printf("纹理创建失败:%s",SDL_GetError()); //销毁窗口释放资源 SDL_DestroyWindow(window); SDL_Quit(); return -1; } int showCnt = 0;//显示次数 int run = 1;//循环退出条件 SDL_Rect rect;//定义一个SDL举行 rect.w = 50; rect.h = 50; while(run){ //碎甲rect的位置 rect.x = rand()%600; rect.y = rand() % 400; // 设置渲染目标为纹理 SDL_SetRenderTarget(renderer, texture); // 设置渲染绘制颜色 SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255); // 刷新渲染 SDL_RenderClear(renderer); // 设置渲染绘制颜色 SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); // 绘制矩形 SDL_RenderDrawRect(renderer, &rect); // 绘制填充矩形 SDL_RenderFillRect(renderer, &rect); // 恢复渲染目标为窗口 SDL_SetRenderTarget(renderer, NULL); SDL_RenderCopy(renderer, texture, NULL, NULL); // 显示纹理 SDL_RenderPresent(renderer); SDL_Delay(300); if(showCnt++ >= 30) run = 0; } SDL_Delay(5000);//延迟5000毫秒 SDL_DestroyWindow(window);//销毁窗口 SDL_Quit();//释放资源 // 引入ffmpeg并打印版本号 unsigned codecVer = avcodec_version(); cout << codecVer<<endl; cout << "codec版本号:"<<codecVer <<endl; return 0; }
10.打开mingw64命令行工具,输入./run.sh执行脚本
11.执行结果会在项目根目录下的bin中输出可执行文件的.exe文件并自动运行,build目录为编译目录
12.效果图如下
标签:bin,cmake,ffmpeg,sdl2,dev,opencv,include,DIR From: https://www.cnblogs.com/tony-yang-flutter/p/17824536.html