ʹÓÃCMake¹¹½¨Linux¸ßÐÔÄÜÅÌËãÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ
ʹÓÃcmake¹¹½¨linux¸ßÐÔÄÜÅÌËãÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ
ÔÚLinuxÇéÐÎÏ¿ª·¢¸ßÐÔÄÜÅÌËãÓ¦ÓóÌÐòÊÇÒ»Ïî¾ßÓÐÌôÕ½ÐÔµÄʹÃü¡£ÎªÁËÄܹ»³ä·ÖʹÓöàºË´¦ÀíÆ÷ºÍ¸ßËÙµÄÍøÂçÅþÁ¬£¬³ÌÐòµÄ±àÒëºÍÁ´½ÓÀú³ÌÐèÒª¾ÙÐÐһЩÓÅ»¯¡£CMakeÊÇÒ»¿îÊ¢ÐеĹ¹½¨¹¤¾ß£¬Ëü¿ÉÒÔ¼ò»¯¹¹½¨Àú³Ì£¬Í¬Ê±ÌṩÁËһЩÓÅ»¯Ñ¡Ïî¡£±¾ÎĽ«ÏÈÈÝһЩʹÓÃcmake¹¹½¨linux¸ßÐÔÄÜÅÌËãÓ¦ÓóÌÐòµÄÉèÖü¼ÇÉ£¬²¢ÌṩÏìÓ¦µÄ´úÂëʾÀý¡£
Ò»¡¢×°ÖÃCMake
Ê×ÏÈ£¬ÎÒÃÇÐèҪװÖÃCMake¹¤¾ß¡£ÔÚ´ó´ó¶¼Linux¿¯ÐаæÖУ¬¿ÉÒÔͨ¹ýÒÔÏÂÏÂÁîÀ´×°ÖÃCMake£º
$ sudo apt-get install cmake
µÇ¼ºó¸´ÖÆ
¶þ¡¢½¨ÉèCMakeLists.txtÎļþ
CMakeʹÓÃCMakeLists.txtÎļþÀ´ÐÎòÏîÄ¿µÄ¹¹½¨¹æÔò¡£ÎÒÃÇ¿ÉÒÔÔÚÏîÄ¿¸ùĿ¼Ï½¨ÉèÒ»¸öCMakeLists.txtÎļþ£¬È»ºóÔÚÆäÖнç˵¹¹½¨¹æÔò¡£
ÀýÈ磬ÎÒÃǵÄÏîÄ¿°üÀ¨Á½¸öÔ´Îļþ£ºmain.cppºÍutils.cpp¡£ÎÒÃÇ¿ÉÒÔƾ֤ÒÔÏ·½·¨À´½¨ÉèCMakeLists.txtÎļþ£º
# CMake×îµÍ°æ±¾ÒªÇó cmake_minimum_required(VERSION 3.10) # ÏîÄ¿Ãû³Æ project(MyApp) # ½ç˵¿ÉÖ´ÐÐÎļþ add_executable(MyApp main.cpp utils.cpp)
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀýÖУ¬ÎÒÃÇÊ×ÏÈÖ¸¶¨ÁËËùÐèµÄCMakeµÄ×îµÍ°æ±¾¡£È»ºó£¬Ê¹ÓÃprojectÏÂÁî¸øÏîÄ¿ÃüÃû¡£×îºó£¬Ê¹ÓÃadd_executableÏÂÁî½ç˵ÁËÒ»¸ö¿ÉÖ´ÐÐÎļþMyApp£¬²¢½«main.cppºÍutils.cpp×÷ΪԴÎļþ¾ÙÐбàÒë¡£
Èý¡¢Ìí¼Ó±àÒëºÍÁ´½ÓÑ¡Ïî
ÔÚ¾ÙÐиßÐÔÄÜÅÌËãÓ¦ÓóÌÐòµÄ±àÒëºÍÁ´½Óʱ£¬ÎÒÃÇͨ³£ÐèÒªÌí¼ÓһЩ±àÒëÑ¡ÏîºÍÁ´½ÓÑ¡ÏîÒÔÓÅ»¯ÐÔÄÜ¡£
ÀýÈ磬ÎÒÃÇ¿ÉÒÔʹÓÃ-O3Ñ¡Ïî¶Ô´úÂë¾ÙÐÐÓÅ»¯£º
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀýÖУ¬ÎÒÃÇʹÓÃsetÏÂÁCMAKE_CXX_FLAGS±äÁ¿ÉèÖÃΪĿ½ñµÄCXXFLAGS±äÁ¿Öµ¼ÓÉÏ-O3Ñ¡ÏÌåÏÖ¶ÔC++´úÂë¾ÙÐÐ×îºéÁ÷ƽµÄÓÅ»¯¡£
ÁíÍ⣬ÈôÊÇÎÒÃǵijÌÐòÒÀÀµÓÚһЩÍⲿ¿â£¬ÎÒÃÇ»¹ÐèÒªÖ¸¶¨ÕâЩ¿âµÄλÖúÍÃû³Æ¡£ÀýÈ磬ÈôÊÇÎÒÃǵijÌÐòÒÀÀµÓÚOpenMP¿â£¬ÎÒÃÇ¿ÉÒÔƾ֤ÒÔÏ·½·¨À´ÉèÖãº
# ²éÕÒOpenMP¿â find_package(OpenMP REQUIRED) # Ìí¼ÓOpenMP±àÒëÑ¡Ïî set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}") # Ìí¼ÓOpenMPÁ´½ÓÑ¡Ïî target_link_libraries(MyApp ${OpenMP_CXX_LIBRARIES})
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀýÖУ¬ÎÒÃÇÊ×ÏÈʹÓÃfind_packageÏÂÁî²éÕÒOpenMP¿â£¬²¢½«Æä´æ´¢ÔÚOpenMP±äÁ¿ÖС£È»ºó£¬Ê¹ÓÃsetÏÂÁ±àÒëÑ¡ÏîCMAKE_CXX_FLAGSÉèÖÃΪĿ½ñµÄCXXFLAGS±äÁ¿Öµ¼ÓÉÏOpenMPµÄ±àÒëÑ¡Ïî¡£×îºó£¬Ê¹ÓÃtarget_link_librariesÏÂÁOpenMPµÄÁ´½Ó¿âÌí¼Óµ½¿ÉÖ´ÐÐÎļþMyAppÖС£
ËÄ¡¢¾ÙÐй¹½¨
Íê³ÉÁËCMakeLists.txtÎļþµÄ±àдºó£¬ÎÒÃÇ¿ÉÒÔ¾ÙÐй¹½¨ÁË¡£Ê×ÏÈ£¬ÎÒÃÇÐèÒªÔÚÏîÄ¿¸ùĿ¼Ï½¨ÉèÒ»¸öbuildÎļþ¼Ð£¬²¢½øÈë¸ÃÎļþ¼Ð£º
$ mkdir build $ cd build
µÇ¼ºó¸´ÖÆ
È»ºó£¬Ê¹ÓÃcmakeÏÂÁîÀ´ÌìÉúMakefile£º
$ cmake ..
µÇ¼ºó¸´ÖÆ
½Ó×Å£¬Ê¹ÓÃmakeÏÂÁî¾ÙÐбàÒ룺
$ make
µÇ¼ºó¸´ÖÆ
µ½´ËΪֹ£¬ÎÒÃÇÒѾÀֳɵØʹÓÃCMake¹¹½¨ÁËÎÒÃǵĸßÐÔÄÜÅÌËãÓ¦ÓóÌÐò¡£
×ܽá
ͨ¹ý±¾ÎĵÄÏÈÈÝ£¬ÎÒÃÇÏàʶÁËÔõÑùʹÓÃCMakeÀ´¹¹½¨Linux¸ßÐÔÄÜÅÌËãÓ¦ÓóÌÐò£¬²¢ÌṩÁËһЩ³£ÓõÄÉèÖü¼ÇɺʹúÂëʾÀý¡£Ê¹ÓÃCMake¿ÉÒÔ´ó´ó¼ò»¯¹¹½¨Àú³Ì£¬Ìá¸ß¿ª·¢Ð§ÂÊ¡£Ï£Íû±¾ÎĶÔÄúÓÐËù×ÊÖú£¬Äܹ»ÔÚÄúµÄÏîÄ¿ÖÐÊ©Õ¹×÷Óá£
ÒÔÉϾÍÊÇʹÓÃCMake¹¹½¨Linux¸ßÐÔÄÜÅÌËãÓ¦ÓóÌÐòµÄÉèÖü¼ÇɵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡