ʹÓÃCMake¹¹½¨LinuxÖÇÄÜÅ©ÒµÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ
ʹÓÃcmake¹¹½¨linuxÖÇÄÜÅ©ÒµÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ
ͻ񻣼
Ëæ×ÅÅ©ÒµÊÖÒÕµÄÒ»Ö±Éú³¤£¬ÖÇÄÜÅ©ÒµÓ¦ÓóÌÐòÕýÖð½¥Êܵ½¹Ø×¢¡£ÔÚ¿ª·¢ºÍ¹¹½¨ÖÇÄÜÅ©ÒµÓ¦ÓóÌÐòʱ£¬Ñ¡ÔñÊʵ±µÄ¹¹½¨¹¤¾ßÖÁ¹ØÖ÷Òª¡£CMake ÊÇÒ»¸öÓÃÓÚ¹¹½¨¡¢²âÊԺʹò°üC/C++Ó¦ÓóÌÐòµÄ¿çƽ̨¹¤¾ß¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃCMakeÀ´ÉèÖÃLinuxÖÇÄÜÅ©ÒµÓ¦ÓóÌÐòµÄ¹¹½¨Àú³Ì£¬²¢ÌṩÏìÓ¦µÄʾÀý´úÂë¡£
CMake¼ò½é
CMakeÊÇÒ»¸ö¿ªÔ´µÄ¿çƽ̨×Ô¶¯¹¹½¨ÏµÍ³£¬¿ÉÒÔÌìÉúMakefile»òÆäËû²î±ð²Ù×÷ϵͳËùÖ§³ÖµÄ¹¹½¨¾ç±¾¡£ËüʹÓüòÆÓµÄÉèÖÃÎļþÀ´½ç˵Õû¸ö¹¹½¨Àú³Ì£¬²¢ÇÒ¿ÉÒÔ×Ô¶¯¼ì²âϵͳÇéÐκͿâµÄÒÀÀµ¹Øϵ¡£CMakeµÄÉèÖúÜÊÇÎÞа£¬ÊÊÓÃÓÚÖݪֲî±ðµÄÏîÄ¿¡£
ÖÇÄÜÅ©ÒµÓ¦ÓóÌÐòµÄ¹¹½¨ÉèÖÃ
ÔÚÖÇÄÜÅ©ÒµÓ¦ÓóÌÐòµÄ¹¹½¨Àú³ÌÖУ¬ÎÒÃÇÐèҪ˼Á¿ÒÔϼ¸¸ö·½ÃæµÄÉèÖãº
2.1 ÉèÖñàÒëÆ÷ºÍ±àÒëÑ¡Ïî
ÔÚCMakeLists.txtÎļþÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÉèÖÃCMAKE_C_COMPILER»òCMAKE_CXX_COMPILER±äÁ¿À´Ö¸¶¨±àÒëÆ÷¡£ÎÒÃÇ»¹¿ÉÒÔÉèÖÃCMAKE_CXX_FLAGS»òCMAKE_C_FLAGS±äÁ¿À´Ìí¼Ó±àÒëÑ¡ÏÀýÈçÓÅ»¯¼¶±ð¡¢ÖÒÑÔ¼¶±ðµÈ¡£Ê¾ÀýÈçÏ£º
cmake_minimum_required(VERSION 3.10) project(SmartAgriApp) set(CMAKE_CXX_COMPILER g++) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall")
µÇ¼ºó¸´ÖÆ
2.2 Ö¸¶¨Ô´ÎļþºÍ°üÀ¨Ä¿Â¼
ͨ¹ýʹÓÃadd_executableÏÂÁîÖ¸¶¨Ô´Îļþ£¬²¢Í¨¹ýtarget_include_directoriesÏÂÁîÖ¸¶¨ÐèÒª°üÀ¨µÄĿ¼¡£Ê¾Àý´úÂëÈçÏ£º
add_executable(app main.cpp helper.cpp) target_include_directories(app PUBLIC include)
µÇ¼ºó¸´ÖÆ
2.3 Ìí¼ÓÒÀÀµ¿â
ÔÚÖÇÄÜÅ©ÒµÓ¦ÓóÌÐòÖУ¬¿ÉÄÜÐèÒªÒýÈëÍⲿ¿âÀ´ÊµÏÖһЩ¹¦Ð§¡£Í¨¹ýʹÓÃfind_packageÏÂÁîÕÒµ½ËùÐèµÄ¿â£¬²¢Ê¹ÓÃtarget_link_librariesÏÂÁËüÃÇÁ´½Óµ½×ðÁú¿Ê±Ó¦ÓóÌÐòÖС£ÀýÈ磬ÈôÊÇ×ðÁú¿Ê±Ó¦ÓóÌÐòÐèҪʹÓÃOpenCV¿â£¬ÎÒÃÇ¿ÉÒÔÔÚCMakeLists.txtÎļþÖÐÌí¼ÓÒÔÏ´úÂ룺
find_package(OpenCV REQUIRED) target_link_libraries(app ${OpenCV_LIBS})
µÇ¼ºó¸´ÖÆ
2.4 ÌìÉú¿ÉÖ´ÐÐÎļþ
×îºó£¬Í¨¹ýʹÓÃadd_executableÏÂÁîÀ´ÌìÉú¿ÉÖ´ÐÐÎļþ£¬²¢Ê¹ÓÃinstallÏÂÁ¿ÉÖ´ÐÐÎļþ×°Öõ½Ö¸¶¨µÄλÖá£Ê¾Àý´úÂëÈçÏ£º
add_executable(app main.cpp helper.cpp) install(TARGETS app DESTINATION bin)
µÇ¼ºó¸´ÖÆ
ʾÀý´úÂë
ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄÖÇÄÜÅ©ÒµÓ¦ÓóÌÐòµÄʾÀý´úÂë:
#include <iostream> #include <opencv2/opencv.hpp> void processImage(cv::Mat& image) { // ͼÏñ´¦Öóͷ£Âß¼ } int main() { cv::VideoCapture cap(0); if (!cap.isOpened()) { std::cerr << "ÎÞ·¨·¿ªÏà»ú" << std::endl; return -1; } cv::Mat frame; while (cap.read(frame)) { processImage(frame); cv::imshow("ÖÇÄÜÅ©ÒµÓ¦ÓóÌÐò", frame); if (cv::waitKey(1) == 27) { break; } } cv::destroyAllWindows(); return 0; }
µÇ¼ºó¸´ÖÆ
×ܽá
ͨ¹ýʹÓÃCMakeÀ´ÉèÖÃLinuxÖÇÄÜÅ©ÒµÓ¦ÓóÌÐòµÄ¹¹½¨Àú³Ì£¬ÎÒÃÇ¿ÉÒÔÎÞаµØ½ç˵±àÒëÆ÷¡¢±àÒëÑ¡Ïî¡¢Ô´Îļþ¡¢ÒÀÀµ¿âµÈ¡£CMakeÌṩÁËÒ»¸ö¾«Á·¶øÇ¿Ê¢µÄ¹¹½¨ÏµÍ³£¬Ê¹µÃ¿ª·¢ÖÇÄÜÅ©ÒµÓ¦ÓóÌÐò±äµÃÔ½·¢Àû±ãºÍ¸ßЧ¡£
²Î¿¼ÎÄÏ×£º
CMake¹Ù·½Îĵµ: https://cmake.org/documentation/
OpenCV¹Ù·½Îĵµ: https://docs.opencv.org/
£¨×¢£ºÎÄÕÂÖеÄʾÀý½ö¹©²Î¿¼£¬ÏêϸµÄÉèÖúʹúÂë¿ÉÄÜÒò²î±ðµÄÏîÄ¿¶øÓÐËù²î±ð¡££©
ÒÔÉϾÍÊÇʹÓÃCMake¹¹½¨LinuxÖÇÄÜÅ©ÒµÓ¦ÓóÌÐòµÄÉèÖü¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡