ʹÓÃCMake¹¹½¨LinuxÈ˹¤ÖÇÄÜÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ
ʹÓÃcmake¹¹½¨linuxÈ˹¤ÖÇÄÜÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ
È˹¤ÖÇÄÜ£¨Artificial Intelligence£¬¼ò³ÆAI£©ÊÖÒÕÔÚµ±½ñÌìϵĸ÷¸öÁìÓò»ñµÃÁËÆÕ±éµÄÓ¦Ó㬲¢ÇÒÆäÓ¦ÓõÄÐèÇóÒ²Ò»Ö±ÔöÌí¡£ÔÚLinuxƽ̨ÉÏ£¬Ê¹ÓÃCMake×÷ΪÏîÄ¿¹¹½¨¹¤¾ß¿ÉÒÔ×ÊÖúÎÒÃǸüºÃµØÖÎÀíºÍÉèÖÃÈ˹¤ÖÇÄÜÓ¦ÓóÌÐò¡£±¾ÎĽ«ÏÈÈÝһЩʹÓÃcmake¹¹½¨linuxÈ˹¤ÖÇÄÜÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ£¬²¢ÌṩÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢×°ÖÃCMake
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚLinuxϵͳÉÏ×°ÖÃCMake¡£¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´×°ÖÃCMake£º
sudo apt-get update sudo apt-get install cmake
µÇ¼ºó¸´ÖÆ
¶þ¡¢½¨ÉèCMakeLists.txtÎļþ
ÔÚÏîÄ¿µÄ¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪCMakeLists.txtµÄÎļþ£¬¸ÃÎļþÊÇCMakeµÄÉèÖÃÎļþ£¬ÓÃÓÚÐÎòÏîÄ¿µÄ¹¹½¨¹æÔòºÍÒÀÀµ¹Øϵ¡£
cmake_minimum_required(VERSION 3.14) project(AIApp) # ÉèÖÃC++±ê×¼ set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) # ÉèÖÃÔ´ÎļþÁбí set(SOURCES main.cpp ai_module.cpp) # Ìí¼Ó¿ÉÖ´ÐÐÎļþ add_executable(AIApp ${SOURCES}) # Ìí¼ÓÒÀÀµ¿â£¨Ê¾ÀýÖ»°üÀ¨Ò»¸öOpenCV¿â£© find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) target_link_libraries(AIApp ${OpenCV_LIBS})
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀýÉèÖÃÎļþÖУ¬Ê×ÏÈÖ¸¶¨ÁËCMakeÐèÒªµÄ×îµÍ°æ±¾ÐÅÏ¢¡£È»ºó£¬ÉèÖÃÁËC++µÄ±ê׼ΪC++11£¬²¢ÒªÇó±àÒëÆ÷Öª×ã¸Ã±ê×¼µÄÒªÇó¡£
½ÓÏÂÀ´£¬½ç˵ÁËÔ´ÎļþµÄÁÐ±í£¬ÕâÀïÖ»ÁгöÁËÁ½¸öÔ´Îļþ£¨main.cppºÍai_module.cpp£©£¬ÏÖʵÏîÄ¿ÖпÉÄÜ»áÓиü¶àµÄÔ´Îļþ¡£
ʹÓÃadd_executableÏÂÁîÌí¼ÓÁËÒ»¸öÃûΪAIAppµÄ¿ÉÖ´ÐÐÎļþ£¬²¢½«Ö®Ç°½ç˵µÄÔ´ÎļþÁбíת´ï¸ø¸ÃÏÂÁî¡£
ͨ¹ýfind_packageÏÂÁî²éÕÒOpenCV¿â£¬²¢Ê¹ÓÃinclude_directories½«¿âµÄÍ·ÎļþĿ¼Ìí¼Óµ½±àÒëÆ÷µÄËÑË÷·¾¶ÖС£×îºó£¬Ê¹ÓÃtarget_link_librariesÏÂÁOpenCV¿âÓëAIAppÁ´½ÓÆðÀ´¡£
Èý¡¢¹¹½¨ºÍÔËÐÐ
ÔÚÏîÄ¿µÄ¸ùĿ¼ÏÂÖ´ÐÐÒÔÏÂÏÂÁîÀ´¹¹½¨È˹¤ÖÇÄÜÓ¦ÓóÌÐò£º
mkdir build cd build cmake .. make
µÇ¼ºó¸´ÖÆ
ÉÏÊöÏÂÁîÊ×ÏȽ¨ÉèÁËÒ»¸öÃûΪbuildµÄĿ¼£¬²¢½øÈë¸ÃĿ¼¡£È»ºóʹÓÃcmakeÏÂÁîƾ֤CMakeLists.txtÎļþÌìÉúÓÃÓÚ¹¹½¨µÄMakefile¡£×îºóʹÓÃmakeÏÂÁî¾ÙÐбàÒëºÍÁ´½Ó¡£
±àÒëÍê³Éºó£¬ÔÚbuildĿ¼Ï»áÌìÉú¿ÉÖ´ÐÐÎļþAIApp¡£¿ÉÒÔÖ±½ÓÔËÐиÿÉÖ´ÐÐÎļþÀ´Ê¹ÓÃÈ˹¤ÖÇÄÜÓ¦ÓóÌÐò¡£
ËÄ¡¢ÆäËûÉèÖÃÑ¡Ïî
³ýÁËÉÏÊöµÄ»ù±¾ÉèÖÃÍ⣬CMake»¹ÌṩÁËһЩÆäËûµÄÉèÖÃÑ¡Ï¿ÉÒÔƾ֤ÏÖʵÐèÒª¾ÙÐе÷½â¡£ÒÔÏÂÊÇһЩ³£ÓõÄÉèÖÃÑ¡ÏîʾÀý£º
ÉèÖÃÊä³ö·¾¶
ͨ¹ýÖ¸¶¨CMAKE_BUILD_TYPE±äÁ¿¿ÉÒÔÉèÖÃÊä³öÎļþµÄ·¾¶¡£¿ÉÒÔÔÚCMakeLists.txtÎļþÖÐÌí¼ÓÒÔÏÂÐУº
set(CMAKE_BUILD_TYPE Release)
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀý½«Êä³öÎļþ·ÅÔÚ¸ùĿ¼ÏµÄreleaseĿ¼ÖС£¿ÉÒÔƾ֤ÐèÇóÉèÖÃΪDebug¡¢Release»òÆäËû×Ô½ç˵µÄÖµ¡£
Ìí¼Ó±àÒëÑ¡Ïî
¿ÉÒÔʹÓÃÒÔÏÂÏÂÁî¸ø±àÒëÆ÷Ìí¼ÓÑ¡Ï
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3")
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀýÌí¼ÓÁ˱àÒëÆ÷µÄÖÒÑÔÐÅÏ¢Êä³öºÍÓÅ»¯Ñ¡Ïî¡£
Ìí¼ÓµÚÈý·½ÒÀÀµ¿â
¹ØÓÚһЩ³£ÓõĵÚÈý·½ÒÀÀµ¿â£¬CMakeÒѾÌṩÁËÏìÓ¦µÄÄ£¿é£¨Module£©¾ÙÐвéÕÒºÍÉèÖá£Ê¾ÀýÈçÏ£º
find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) target_link_libraries(AIApp ${OpenCV_LIBS})
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀýʹÓÃÁËOpenCV¿â£¬²¢Í¨¹ýfind_packageÏÂÁîÕÒµ½Á˸ÿ⡣Ȼºóͨ¹ýinclude_directories½«Í·ÎļþĿ¼Ìí¼Óµ½±àÒëÆ÷µÄËÑË÷·¾¶ÖУ¬ÔÙͨ¹ýtarget_link_librariesÏÂÁ¿âÓë¿ÉÖ´ÐÐÎļþÁ´½Ó¡£
×ܽ᣺
±¾ÎÄÏÈÈÝÁËʹÓÃcmake¹¹½¨linuxÈ˹¤ÖÇÄÜÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ£¬²¢ÌṩÁËÏìÓ¦µÄ´úÂëʾÀý¡£Í¨¹ýCMakeµÄÉèÖÃÎļþCMakeLists.txt£¬ÎÒÃÇ¿ÉÒÔ¸üÀû±ãµØÖÎÀíºÍÉèÖÃÏîÄ¿µÄ¹¹½¨¹æÔòºÍÒÀÀµ¹Øϵ¡£Ï£ÍûÕâЩ¼¼ÇɹØÓÚ¿ª·¢Ö°Ô±ÔÚLinuxƽ̨ÉϹ¹½¨È˹¤ÖÇÄÜÓ¦ÓóÌÐòʱÓÐËù×ÊÖú¡£
ÒÔÉϾÍÊÇʹÓÃCMake¹¹½¨LinuxÈ˹¤ÖÇÄÜÓ¦ÓóÌÐòµÄÉèÖü¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡