ʹÓÃCMake¹¹½¨LinuxÖÇÄܶ¼»áÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ
ʹÓÃcmake¹¹½¨linuxÖÇÄܶ¼»áÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ
ÕªÒª£º±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃcmake¹¹½¨linuxÖÇÄܶ¼»áÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ¡£Í¨¹ýʹÓÃCMake¿ÉÒÔ¼ò»¯¹¹½¨Àú³Ì£¬²¢ÇÒÌṩÁËÎÞаÐԺͿÉά»¤ÐÔ¡£±¾ÎĽ«ÏêϸÏÈÈÝÔõÑùÉèÖÃCMakeÒÔ¹¹½¨Ò»¸öÖÇÄܶ¼»áÓ¦ÓóÌÐò£¬²¢ÌṩÁËÏìÓ¦µÄ´úÂëʾÀý¡£
СÐò£ºLinux×÷Ϊһ¸ö¿ª·ÅÇҿɶ¨ÖƵIJÙ×÷ϵͳ£¬ÔÚÖÇÄܶ¼»áÓ¦ÓÃÁìÓò»ñµÃÁËÆÕ±éµÄÓ¦Óá£ÎªÁËÄܹ»¸ßЧµØ¹¹½¨ÖÇÄܶ¼»áÓ¦ÓóÌÐò£¬¿ª·¢ÕßÃÇÐèÒªÒ»¸öÇ¿Ê¢¶øÎÞаµÄ¹¹½¨¹¤¾ß¡£CMake×÷Ϊһ¸ö¿çƽ̨µÄ¹¹½¨×Ô¶¯»¯¹¤¾ß£¬Îª¹¹½¨LinuxÓ¦ÓóÌÐòÌṩÁ˱㵱¡£±¾ÎĽ«ÒÔÖÇÄܶ¼»áÓ¦ÓóÌÐòΪÀý£¬ÏÈÈÝCMakeµÄÉèÖü¼ÇÉ£¬ÒÔ¼°ÔõÑùʹÓÃËüÀ´¹¹½¨LinuxÖÇÄܶ¼»áÓ¦Óá£
×°ÖÃCMake
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚLinuxϵͳÉÏ×°ÖÃCMake¡£¿ÉÒÔÔÚÏÂÁîÐÐÏÂÖ´ÐÐÒÔÏÂÏÂÁîÀ´×°ÖÃCMake£º
$ sudo apt-get install cmake
µÇ¼ºó¸´ÖÆ
½¨ÉèCMakeLists.txtÎļþ
ÔÚÏîÄ¿µÄ¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪCMakeLists.txtµÄÎļþ£¬¸ÃÎļþ½«ÓÃÀ´ÐÎòÏîÄ¿µÄ¹¹½¨¹æÔò¡£ÔÚ¸ÃÎļþÖУ¬ÎÒÃÇ¿ÉÒÔÉèÖÃÏîÄ¿µÄÃû³Æ¡¢°æ±¾ºÅ¡¢Á´½Ó¿â¡¢Ô´ÎļþµÈµÈ¡£
ÒÔÏÂÊÇÒ»¸öʾÀýµÄCMakeLists.txtÎļþ£º
# CMake ×îµÍ°æ±¾ÒªÇó cmake_minimum_required(VERSION 3.10.0) # ÏîÄ¿Ãû³Æ project(SmartCityApp) # ÉèÖÃC++±ê×¼ set(CMAKE_CXX_STANDARD 11) # Ìí¼ÓÁ´½Ó¿â find_library(MYSQL_LIBRARY mysqlclient REQUIRED) # Ìí¼ÓÍ·Îļþ include_directories(include) # Ìí¼Ó¿ÉÖ´ÐÐÎļþ add_executable(SmartCityApp src/main.cpp src/sensor.cpp) # Á´½Ó¿â target_link_libraries(SmartCityApp ${MYSQL_LIBRARY})
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëʾÀýÖУ¬ÎÒÃÇÊ×ÏÈÖ¸¶¨ÁËCMakeµÄ×îµÍ°æ±¾ÒªÇóΪ3.10.0¡£È»ºó£¬ÎÒÃǽç˵ÁËÏîÄ¿µÄÃû³ÆΪSmartCityApp¡£½ÓÏÂÀ´£¬ÎÒÃÇÉèÖÃÁËC++µÄ±ê׼ΪC++11¡£È»ºó£¬ÎÒÃÇʹÓÃfind_libraryÖ¸ÁîÀ´²éÕÒÃûΪmysqlclientµÄÁ´½Ó¿â£¬²¢½«Æ丳ֵ¸øMYSQL_LIBRARY±äÁ¿¡£ÎÒÃÇʹÓÃinclude_directoriesÖ¸ÁîÀ´Ìí¼ÓÍ·ÎļþĿ¼£¬ÓÃÀ´Ñ°ÕÒÍ·ÎļþµÄλÖá£È»ºó£¬ÎÒÃÇʹÓÃadd_executableÖ¸ÁîÀ´Ìí¼ÓÒ»¸ö¿ÉÖ´ÐÐÎļþSmartCityApp£¬²¢Ö¸¶¨ÁËÔ´ÎļþµÄλÖá£×îºó£¬ÎÒÃÇʹÓÃtarget_link_librariesÖ¸ÁîÀ´Ö¸¶¨ÐèÒªÁ´½ÓµÄ¿â¡£ÔÚ±¾Ê¾ÀýÖУ¬ÎÒÃǽ«mysqlclient¿âÁ´½Óµ½SmartCityApp¿ÉÖ´ÐÐÎļþÉÏ¡£
¹¹½¨ÏîÄ¿
ÔÚʹÓÃCMake¹¹½¨ÏîĿ֮ǰ£¬ÎÒÃÇÐèÒªÔÚÏîÄ¿µÄ¸ùĿ¼Ï½¨ÉèÒ»¸öÃûΪbuildµÄÎļþ¼Ð£¬²¢ÔÚ¸ÃÎļþ¼ÐÏÂÖ´ÐÐÒÔÏÂÏÂÁ
$ cd build $ cmake .. $ make
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉÏ°ì·¨£¬CMake»áƾ֤CMakeLists.txtÎļþÖеÄÉèÖùæÔòÀ´ÌìÉúMakefileÎļþ¡£È»ºóͨ¹ýmakeÏÂÁîÀ´Ö´ÐÐMakefileÎļþ£¬´Ó¶øÍê³ÉÏîÄ¿µÄ¹¹½¨¡£
ÔËÐÐÓ¦ÓóÌÐò
¹¹½¨Àֳɺ󣬿ÉÒÔÔÚbuildÎļþ¼ÐÏÂÕÒµ½ÌìÉúµÄ¿ÉÖ´ÐÐÎļþSmartCityApp¡£Í¨¹ýÔÚÏÂÁîÐÐÏÂÖ´ÐÐÒÔÏÂÏÂÁî¿ÉÒÔÔËÐиÃÓ¦ÓóÌÐò£º
$ ./SmartCityApp
µÇ¼ºó¸´ÖÆ
ͨ¹ýÒÔÉÏ°ì·¨£¬ÎÒÃÇ¿ÉÒÔÀֳɵØʹÓÃCMake¹¹½¨LinuxÖÇÄܶ¼»áÓ¦ÓóÌÐò¡£
½áÂÛ£º±¾ÎÄÏÈÈÝÁËÔõÑùʹÓÃCMakeÀ´¹¹½¨ÖÇÄܶ¼»áÓ¦ÓóÌÐò¡£Í¨¹ýCMake£¬ÎÒÃÇ¿ÉÒÔ¼ò»¯¹¹½¨Àú³Ì£¬²¢ÌṩÁËÎÞаÐԺͿÉά»¤ÐÔ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃÇչʾÁËÒ»¸öʹÓÃCMake¹¹½¨LinuxÖÇÄܶ¼»áÓ¦ÓóÌÐòµÄÉèÖÃʾÀý£¬²¢ÌṩÁËÏìÓ¦µÄ´úÂëʾÀý¡£Ï£Íû±¾ÎĶÔÍýÏëʹÓÃCMake¹¹½¨LinuxÖÇÄܶ¼»áÓ¦ÓóÌÐòµÄ¿ª·¢ÕßÃÇÓÐËù×ÊÖú¡£
²Î¿¼ÎÄÏ×£º
[1] https://cmake.org/
[2] https://cmake.org/cmake/help/latest/
[3] https://www.cyberciti.biz/faq/install-apt-get-ubuntu-16-04/
[4] https://linuxize.com/post/how-to-use-cmake-to-build-c-projects/
ÒÔÉϾÍÊÇʹÓÃCMake¹¹½¨LinuxÖÇÄܶ¼»áÓ¦ÓóÌÐòµÄÉèÖü¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡