ʹÓÃCMake¹¹½¨Linux¹¤Òµ×Ô¶¯»¯Ó¦ÓóÌÐòµÄÉèÖü¼ÇÉ
ʹÓÃcmake¹¹½¨linux¹¤Òµ×Ô¶¯»¯Ó¦ÓóÌÐòµÄÉèÖü¼ÇÉ
¹¤Òµ×Ô¶¯»¯Ó¦ÓóÌÐòÔÚÏÖ´ú¹¤ÒµÁìÓòÖÐÊ©Õ¹×ÅÖ÷Òª×÷Óá£ÎªÁËÀû±ã¿ª·¢Ö°Ô±ÖÎÀíºÍ¹¹½¨ÏîÄ¿£¬Ê¹ÓÃÒ»¸öÇ¿Ê¢µÄ¹¹½¨¹¤¾ßÊDZز»¿ÉÉٵġ£ÔÚLinuxϵͳÉÏ£¬CMakeÊÇÒ»¸ö±»ÆÕ±éʹÓõĹ¹½¨¹¤¾ß¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃCMakeÀ´ÉèÖú͹¹½¨Linux¹¤Òµ×Ô¶¯»¯Ó¦ÓóÌÐò£¬²¢ÌṩÏà¹ØµÄ´úÂëʾÀý¡£
×°ÖÃCMake
Ê×ÏÈ£¬ÐèÒªÔÚLinuxϵͳÉÏ×°ÖÃCMake¡£¿ÉÒÔͨ¹ýÏÂÁîÐÐÖÕ¶ËÖ´ÐÐÒÔÏÂÏÂÁîÀ´×°ÖÃCMake£º
$ sudo apt-get install cmake
µÇ¼ºó¸´ÖÆ
×°ÖÃÍê³Éºó£¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´ÑéÖ¤CMakeÊÇ·ñ×°ÖÃÀֳɣº
$ cmake --version
µÇ¼ºó¸´ÖÆ
½¨ÉèCMakeLists.txtÎļþ
ÔÚÏîÄ¿µÄ¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪCMakeLists.txtµÄÎļþ¡£¸ÃÎļþÊÇCMakeµÄÉèÖÃÎļþ£¬ÓÃÓÚÐÎòÏîÄ¿µÄ¹¹½¨¹æÔòºÍÒÀÀµ¹Øϵ¡£ÒÔÏÂÊÇÒ»¸ö¼òÆÓµÄCMakeLists.txtÎļþʾÀý£º
# CMake°æ±¾ÒªÇó cmake_minimum_required(VERSION 3.10) # ÉèÖÃÏîÄ¿Ãû³Æ project(MyIndustrialApp) # ÉèÖñàÒëÆ÷ set(CMAKE_CXX_COMPILER g++) # ÉèÖÃC++±ê×¼ set(CMAKE_CXX_STANDARD 11) # Ìí¼ÓÔ´Îļþ set(SOURCES main.cpp foo.cpp bar.cpp) # Ìí¼Ó¿ÉÖ´ÐÐÎļþ add_executable(MyIndustrialApp ${SOURCES}) # Ìí¼Ó¿âÎļþ target_link_libraries(MyIndustrialApp pthread)
µÇ¼ºó¸´ÖÆ
ÔÚÉÏÃæµÄʾÀýÖУ¬Ê×ÏÈͨ¹ýcmake_minimum_requiredÏÂÁîÖ¸¶¨ËùÐèµÄCMake°æ±¾¡£È»ºóͨ¹ýprojectÏÂÁîÉèÖÃÏîÄ¿Ãû³Æ¡£½ÓÏÂÀ´£¬Í¨¹ýsetÏÂÁîÉèÖñàÒëÆ÷ºÍC++±ê×¼¡£È»ºóʹÓÃsetÏÂÁî½ç˵һ¸ö±äÁ¿SOURCES£¬ÓÃÓÚ´æ·ÅÏîÄ¿µÄÔ´Îļþ¡£×îºó£¬Í¨¹ýadd_executableÏÂÁîÌí¼Ó¿ÉÖ´ÐÐÎļþ²¢Í¨¹ýtarget_link_librariesÏÂÁîÌí¼Ó¿âÎļþ¡£
ÉèÖÃÒÀÀµÏî
ÔÚ¹¤Òµ×Ô¶¯»¯Ó¦ÓóÌÐòÖУ¬ÍùÍùÐèÒªÒÀÀµÒ»Ð©µÚÈý·½¿â¡£ÔÚCMakeÖУ¬¿ÉÒÔʹÓÃfind_packageÏÂÁîÀ´ÉèÖúÍʹÓÃÕâЩÒÀÀµÏî¡£ÒÔÏÂÊÇÒ»¸öʹÓÃOpenCV¿âµÄʾÀý£º
# ²éÕÒOpenCV¿â find_package(OpenCV REQUIRED) # ÈôÊÇÕÒµ½ÁËOpenCV¿â£¬½«ÆäÌí¼Óµ½¿ÉÖ´ÐÐÎļþ if(OpenCV_FOUND) include_directories(${OpenCV_INCLUDE_DIRS}) target_link_libraries(MyIndustrialApp ${OpenCV_LIBS}) endif()
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄʾÀýÖУ¬Í¨¹ýfind_packageÏÂÁî²éÕÒOpenCV¿â¡£ÈôÊÇÕÒµ½ÁËOpenCV¿â£¬¾Íͨ¹ýinclude_directoriesÏÂÁîÌí¼Ó°üÀ¨Â·¾¶£¬²¢Í¨¹ýtarget_link_librariesÏÂÁOpenCV¿âÁ´½Óµ½¿ÉÖ´ÐÐÎļþÖС£
¹¹½¨ÏîÄ¿
ÉèÖÃÍê³Éºó£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´¹¹½¨ÏîÄ¿£º
$ mkdir build $ cd build $ cmake .. $ make
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÏÂÁÔÚÏîÄ¿µÄbuildĿ¼ÏÂÌìÉúMakefileÎļþ£¬²¢Ê¹ÓÃmakeÏÂÁî¾ÙÐбàÒëºÍÁ´½Ó¡£
ÔËÐÐÓ¦ÓóÌÐò
ÔÚÏîÄ¿¹¹½¨Íê³Éºó£¬¿ÉÒÔʹÓÃÒÔÏÂÏÂÁîÀ´ÔËÐÐÓ¦ÓóÌÐò£º
$ ./MyIndustrialApp
µÇ¼ºó¸´ÖÆ
¸ÃÏÂÁִÐÐÃûΪMyIndustrialAppµÄ¿ÉÖ´ÐÐÎļþ¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃCMakeÀ´ÉèÖú͹¹½¨Linux¹¤Òµ×Ô¶¯»¯Ó¦ÓóÌÐò¡£Ê×ÏÈ£¬ÐèҪװÖÃCMake£¬²¢½¨ÉèÒ»¸öCMakeLists.txtÉèÖÃÎļþ¡£È»ºó£¬¿ÉÒÔÉèÖÃÒÀÀµÏ²¢Ê¹ÓÃÏÂÁîÐÐÖն˹¹½¨ºÍÔËÐÐÓ¦ÓóÌÐò¡£CMakeÊÇÒ»¸öÇ¿Ê¢µÄ¹¹½¨¹¤¾ß£¬Äܹ»¼«´óµØ¼ò»¯ÏîÄ¿µÄÖÎÀíºÍ¹¹½¨Àú³Ì¡£
Ï£Íû±¾ÎĶÔÄúÔÚLinuxϵͳÉϹ¹½¨¹¤Òµ×Ô¶¯»¯Ó¦ÓóÌÐòʱÓÐËù×ÊÖú¡£ÈôÓÐÈκÎÎÊÌ⣬ÇëËæʱÌáÎÊ¡£×£ÄúÔÚ¹¤Òµ×Ô¶¯»¯ÁìÓòÈ¡µÃÀֳɣ¡
ÒÔÉϾÍÊÇʹÓÃCMake¹¹½¨Linux¹¤Òµ×Ô¶¯»¯Ó¦ÓóÌÐòµÄÉèÖü¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡