CMakeã䜿çšããŠãããžã§ã¯ãããã«ãããã«ã¯ãCMakeLists.txtãã¡ã€ã«ããœãŒã¹ããªãŒã®ã«ãŒãã«é 眮ããå¿ èŠããããŸãããœãŒã¹ããªãŒã«ã¯ãã¢ã»ã³ããªã®ã«ãŒã«ãšç®æšãæ ŒçŽãããŠãããããã€ãã®ç°¡åãªæé ãå®è¡ããŸãã
äŸãèŠãŠã¿ãŸãããã
äŸ1. HelloãWorldïŒ
ãŸããæãåçŽãªã¯ãŒã«ãã¯ãŒããäœæãããããžã§ã¯ãæ§é ãäœæããŸãã
main.cpp
#include <iostream> int main(int argc, char** argv) { std::cout << "Hello, World!" << std::endl; return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.8) # CMake. # # , . add_executable(main main.cpp) # main # main.cpp
CMakeã®æ§æã¯bashã®æ§æã«äŒŒãŠããŸãããïŒãæå以éã¯ãã¹ãŠã³ã¡ã³ãã§ãããããã°ã©ã ã«ãã£ãŠåŠçãããŸããã CMakeã¯ãäžæãã¡ã€ã«ã§ãœãŒã¹ããªãŒãä¹±éã«ããªãããšãå¯èœã«ããŸã-ããã¯éåžžã«åçŽã§ãè¿œå ã®ãžã§ã¹ãã£ãŒãªãã§ãã¢ã»ã³ããªã¯ãã¢ãŠããªããœãŒã¹ãã§å®è¡ãããŸãã
äžæãã¡ã€ã«çšã®ç©ºã®ãã£ã¬ã¯ããªãäœæããŠããã«ç§»åããŸãã
fshp @ panica-desktopïŒã$ mkdir tmp
fshp @ panica-desktopïŒã$ cd tmp /
fshp @ panica-desktopïŒã/ tmp $
次ã«ããã©ã¡ãŒã¿ãŒãšããŠãœãŒã¹ãã©ã«ããŒãžã®ãã¹ãæž¡ããŠãcmakeã³ãã³ããå®è¡ããŸãã
fshp @ panica-desktopïŒã/ tmp $ cmakeã/ cmake / example_1 /
...
-ãã«ããã¡ã€ã«ã¯æ¬¡ã®å Žæã«æžã蟌ãŸããŠããŸãïŒ/ home / fshp / tmp
fshp @ panica-desktopïŒã/ tmp $
fshp @ panica-desktopïŒã/ tmp $ ls
CMakeCache.txt CMakeFiles cmake_install.cmake Makefile
fshp @ panica-desktopïŒã/ tmp $
ãããžã§ã¯ãã®ãã«ãã«å¿ èŠãªããã€ãã®äžæãã¡ã€ã«ããã©ã«ããŒã«è¡šç€ºãããŠããããšãããããŸãã
ããã§ãmakeãçŽæ¥å®è¡ã§ããŸãã
fshp @ panica-desktopïŒã/ tmp $ make
ã¿ãŒã²ããmainã®ã¹ãã£ã³äŸåé¢ä¿
[100ïŒ ] CXXãªããžã§ã¯ãCMakeFiles / main.dir / main.cpp.oã®æ§ç¯
CXXå®è¡å¯èœã¡ã€ã³ã®ãªã³ã¯
[100ïŒ ]ãã«ããããã¿ãŒã²ããã¡ã€ã³
fshp @ panica-desktopïŒã/ tmp $ ./main
Hello WorldïŒ
fshp @ panica-desktopïŒã/ tmp $
ã ãããç§ãã¡ã®ããã°ã©ã ã¯éãŸã£ãã
tmpãã©ã«ããŒã¯ããœãŒã¹ãå£ããªã¹ã¯ãªãã«ã¯ãªãŒãã³ã°/åé€ã§ããŸãã CMakeLists.txtãå€æŽãããŠããå ŽåãmakeåŒã³åºãã¯cmakeãèªåçã«éå§ããŸãã ãœãŒã¹ã移åãããå Žåã¯ãäžæãã£ã¬ã¯ããªãã¯ãªã¢ããŠcmakeãæåã§å®è¡ããå¿ èŠããããŸãã
äŸ2.ã©ã€ãã©ãªãŒïŒ
ãããžã§ã¯ãã«ã©ã€ãã©ãªãå«ãŸããŠããå ŽåãCMakeã¯åé¡ãªãã©ã€ãã©ãªããã«ãããŸãã
ãããè¡ãã«ã¯ãäŸãè€éã«ããŸãã
foo.h
void hello_world();
foo.cpp
#include <iostream> void hello_world() { std::cout << "Hello, World!" << std::endl; }
main.cpp
#include "foo.h" int main(int argc, char** argv) { hello_world(); return 0; }
CMakeLists.txt
cmake_minimum_required(VERSION 2.8) # CMake. # # , . project(hello_world) # set(SOURCE_EXE main.cpp) # set(SOURCE_LIB foo.cpp) # , add_library(foo STATIC ${SOURCE_LIB}) # foo add_executable(main ${SOURCE_EXE}) # main target_link_libraries(main foo) #
å€æ°ã¯ãã¹ããŒã¹/ã¿ã/ãã€ãã³ã§åºåãããå€ã®ãªã¹ããä¿åã§ããŸãïŒ
set(SOURCE main.cpp foo.cpp) set(HEADER main.h foo.h)
äž¡æ¹ã®ãªãã·ã§ã³ãæ£ããã§ãã
å€æ°ã®å€ãååŸããã«ã¯ã次ã®æ§é ã䜿çšããŸãã
${var_name}
ãããã£ãŠããã®ããŒãžã§ã³ã®ãããžã§ã¯ãã«ã¯ããœãŒã¹ããã³ã³ãã€ã«ãããéçã©ã€ãã©ãªã1ã€å«ãŸããŠããŸãã ãSTATICãããSHAREDãã«çœ®ãæãããšãåçã©ã€ãã©ãªãäœæãããŸãã ã©ã€ãã©ãªã®ã¿ã€ããæå®ãããŠããªãå Žåãããã©ã«ãã§éçãšããŠã¢ã»ã³ãã«ãããŸãã
ãªã³ã¯ãããšãå¿ èŠãªãã¹ãŠã®ã©ã€ãã©ãªã瀺ãããŸãã
target_link_libraries(main foo ogg vorbis)
æåã³ã³ãã€ã«ãšåæ§ã«ãã©ã€ãã©ãªåã¯æšæºã®ãlibããã¬ãã£ãã¯ã¹ãªãã§æå®ãããŸãã
ãããã£ãŠãCMakeã䜿çšããŠã©ã€ãã©ãªãäœæããŠãåé¡ã¯çºçããŸããããã©ã€ãã©ãªã®ã¿ã€ãã¯1ã€ã®ãã©ã¡ãŒã¿ã®ã¿ã§éç/åçã«å€æŽãããŸãã
äŸ3.ãµããããžã§ã¯ãïŒ
ããã°ã©ã ãè€æ°ã®ã©ã€ãã©ãªã«åå²ãããŠããå ŽåããŸãã¯ãããžã§ã¯ããè€æ°ã®ããã°ã©ã ã§æ§æãããŠããå Žåããµããããžã§ã¯ãã¯éåžžã«äŸ¿å©ã§ãã
åãµããããžã§ã¯ãã¯æ¬è³ªçã«æ¬æ Œçãªãããžã§ã¯ãã§ãããç¬ç«ããŠäœ¿çšã§ããŸãã
ããã§ããµããã£ã¬ã¯ããªã«ãfooããé 眮ãããCMakeLists.txtãµããããžã§ã¯ããããã«é 眮ãããŸããã
CMakeLists.txt
cmake_minimum_required(VERSION 2.8) # CMake. # # , . project(hello_world) # set(SOURCE_EXE main.cpp) # include_directories(foo) # add_executable(main ${SOURCE_EXE}) # main add_subdirectory(foo) # , target_link_libraries(main foo) #
main.cpp
#include "foo.h" int main(int argc, char** argv) { hello_world(); return 0; }
foo / CMakeLists.txt
cmake_minimum_required(VERSION 2.8) # CMake. # # , . project(foo) # set(SOURCE_LIB foo.cpp) # add_library(foo STATIC ${SOURCE_LIB})#
foo / foo.h
void hello_world();
foo / foo.cpp
#include <iostream> void hello_world() { std::cout << "Hello, World!" << std::endl; }
ãµããããžã§ã¯ããã¡ã€ã«ã«ã¯ãæ°ãããã®ã¯äœããããŸããã ãããŠãã¡ã€ã³ãã¡ã€ã«ã®æ°ããã³ãã³ãã¯æ¬¡ã®ãšããã§ãã
include_directories(foo)
main.cppã¯å€æŽããŸããã§ããããfoo.hã¯è»¢éããŸããã ãã®ã³ãã³ãã¯ãããããŒãã¡ã€ã«ãæ¢ãå Žæãã³ã³ãã€ã©ã«æ瀺ããŸãã æ°ååŒã³åºãããšãã§ããŸãã ããããŒã¯ãæå®ããããã¹ãŠã®ãã£ã¬ã¯ããªã§æ€çŽ¢ãããŸãã
add_subdirectory(foo)
ãµããããžã§ã¯ããå«ããã£ã¬ã¯ããªãæå®ããŸãããµããããžã§ã¯ãã¯ç¬ç«ãããã®ãšããŠã¢ã»ã³ãã«ãããŸãã
çµè«ïŒCMakeã®ãããžã§ã¯ãã¯ãããªãè€éãªéå±€æ§é ã«çµã¿åãããããšãã§ããå®éã«ã¯ãåãµããããžã§ã¯ãã¯ç¬ç«ãããããžã§ã¯ãã§ãããããèªäœããµããããžã§ã¯ãã§æ§æãããå ŽåããããŸãã ããã«ãããããã°ã©ã ãå¿ èŠãªæ°ã®åã ã®ã¢ãžã¥ãŒã«ã«ç°¡åã«åå²ã§ããŸãã ãã®ã¢ãããŒãã®äŸã¯KDEã§ãã
äŸ4.ã©ã€ãã©ãªã®æ€çŽ¢ïŒ
CMakeã¯ã€ã³ã¹ããŒã«ãããã©ã€ãã©ãªãæ€çŽ¢ããæ段ãããªãéçºããŸãããããããã¯çµã¿èŸŒã¿ã§ã¯ãªããåå¥ã®ã¢ãžã¥ãŒã«ãšããŠå®è£ ãããŠããŸãã æšæºããã±ãŒãžã«ã¯ããªãã®æ°ã®ã¢ãžã¥ãŒã«ããããŸãããäžéšã®ãããžã§ã¯ãïŒOgreãªã©ïŒã¯ç¬èªã®ã¢ãžã¥ãŒã«ãæäŸããŠããŸãã ããã«ãããã·ã¹ãã ã¯ãããžã§ã¯ãã®ãªã³ã¯ã«å¿ èŠãªã©ã€ãã©ãªã®å¯çšæ§ãèªåçã«æ€åºã§ããŸãã
Debianã§ã¯ãã¢ãžã¥ãŒã«ã¯/usr/share/cmake-2.8/Modules/ã«ãããŸãïŒããŒãžã§ã³ã¯ç°ãªãå ŽåããããŸãïŒã FindNAME.cmakeãšåŒã°ããã©ã€ãã©ãªã¯ãã©ã€ãã©ãªæ€çŽ¢ãæ åœããŸããNAMEã¯ã©ã€ãã©ãªã®ååã§ãã
find_package(SDL REQUIRED) if(NOT SDL_FOUND) message(SEND_ERROR "Failed to find SDL") return() else() include_directories(${SDL_INCLUDE_DIR}) endif() ########################################################## find_package(LibXml2 REQUIRED) if(NOT LIBXML2_FOUND) message(SEND_ERROR "Failed to find LibXml2") return() else() include_directories(${LIBXML2_INCLUDE_DIR}) endif() ########################################################## find_package(Boost COMPONENTS thread-mt REQUIRED) if(NOT Boost_FOUND) message(SEND_ERROR "Failed to find boost::thread-mt.") return() else() include_directories(${Boost_INCLUDE_DIRS}) endif() ########################################################## target_link_libraries(${TARGET} ${SDL_LIBRARY} ${LIBXML2_LIBRARIES} ${Boost_LIBRARIES})
ãã®æå³ã¯æ確ã«ãã¹ãã ãšæããŸãã æåãš2çªç®ã®ãããã¯ã¯ã©ã€ãã©ãªæ€çŽ¢ã§ãã ã·ã¹ãã ã«ãªãå Žåããšã©ãŒã¡ãã»ãŒãžã衚瀺ãããcmakeãå®äºããŸãã 3çªç®ã®ãããã¯ã䌌ãŠããŸãããã©ã€ãã©ãªã®ããã±ãŒãžå šäœã§ã¯ãªããå¿ èŠãªã³ã³ããŒãã³ãã®ã¿ãæ¢ããŠããŸãã ãã®ãããªèªåæ€çŽ¢ã¯ãããããå®è¡åŸã«å°ãªããšã3ã€ã®å€æ°ã決å®ããŸãã
SDL_FOUNDãLIBXML2_FOUNDãBoost_FOUND-ã©ã€ãã©ãªã®ååšã®å åã
SDL_LIBRARYãLIBXML2_LIBRARIESãBoost_LIBRARIES-ãªã³ã¯çšã®ã©ã€ãã©ãªåã
SDL_INCLUDE_DIRãLIBXML2_INCLUDE_DIRãBoost_INCLUDE_DIRS-ããããŒãã¡ã€ã«ãžã®ãã¹ã
åè ãå€ããå°ãªããæ確ã§ããã°ãåŸè ãš3çªç®ã¯ç§ã«å€ãã®åé¡ãäžããŸãã-ååã¯åæ°åœ¢ã§ãååã¯è€æ°åœ¢ã§ããŸãã ãããã远跡ãç°¡åã§ããããšãå€æããŸããã åã¢ãžã¥ãŒã«ã§ã¯ãæåã«ã³ã¡ã³ãããããå®çŸ©ãããå€æ°ãããã«èšè¿°ãããŠããŸãã ããšãã°ã/ usr / share / cmake-2.8 / Modules / FindLibXml2.cmakeãåç §ããŠãã ãã
ã芧ã®ãšãããCMakeã¯å¿ èŠãªã©ã€ãã©ãªãšããããŒãã¡ã€ã«ã®ååšãšå Žæãç¹å®ã§ããŸãã ååãšããŠãèªåçµç«ã·ã¹ãã ã¯ãããè¡ãããšãã§ããã¯ãã§ãã
äŸ5.å€éšã©ã€ãã©ãªãšãªããžã§ã¯ããã¡ã€ã«ïŒ
ããªããããããããã®ããã«æžããŠãéªæªãªãããããããèªäœã®ã©ã€ãã©ãªãæãããœãŒã¹ã³ãŒããå ±æããããªãå Žåã圌ã¯å®æããã©ã€ãã©ãªãéãã®ã§ãããªãã¯ã¢ãã¬ã¹ã«ããŸãã
CMakeã®ãªããžã§ã¯ããã¡ã€ã«ã¯ãœãŒã¹ãšåçã§ã-ã³ã³ãã€ã«ãããã¡ã€ã«ã®ãªã¹ãã«ãªããžã§ã¯ããå«ããã ãã§ãã
ã©ã€ãã©ãªãããå³å¯ã«ã ãåãã®ããã«ãéçã©ã€ãã©ãªã¯ar-archiveã«ããããå éšã«ã¯éåžžã®ãªããžã§ã¯ããäœããã®æ¹æ³ã§æ¥ç¶ãããŠããŸããã ããªãã¯ããããç§ãæåã«ã©ã®ããã«è¡åãããããã§ã«æšæž¬ããã§ãããã ã¯ããç§ã¯ã¡ããã©å³æžé€šãç Žå£ããŸããã ãããããããšã¬ã¬ã³ããªæ¹æ³ãèŠã€ãããŸããïŒ
add_library(netutil STATIC IMPORTED) set_property(TARGET netutil PROPERTY IMPORTED_LOCATION Binary/game_client/libnetutil.a)
ãã€ã³ããŒãæžã¿ããšããèªã¯ãã©ã€ãã©ãªãå€éšããååŸãããããšã瀺ããŸãã
CMakeã§ã¯ãåã¿ãŒã²ããã«ãã©ã¡ãŒã¿ãŒããããset_propertyã䜿çšããŠãã©ã¡ãŒã¿ãŒãå€æŽã§ããŸãã
ãã®ãããªã©ã€ãã©ãªã¯æšæºãšããŠãªã³ã¯ãããŠããŸãïŒ
target_link_libraries(${TARGET} netutil)
åçã©ã€ãã©ãªã®å Žåããã¹ãŠãé¡äŒŒããŠãããã¿ã€ãã®ã¿ããSHAREDããæ¡åŒµåãã.soãã§ãã
æ®å¿µãªãããéã·ã¹ãã ã©ã€ãã©ãªã®ãµããŒãã¯å°ãæ éã«å®è£ ãããŠããŸãã ãããããæ£ããéžæè¢ãããããªãã ããªã®ã§ããéå£ãçªããããšãã§ããã°ããããã§ãã äžæ¹ãããã¯çåœç¶æã·ã¹ãã ãåãã掟æãªå€éªšæ Œã§ã¯ãªãã2æ¬ã®ç·ãããªãåçŽãªæŸèæã§ãã
ãžã§ãã¬ãŒã¿ãŒïŒ
åé ã§è¿°ã¹ãããã«ãCMakeã¯ããŸããŸãªçš®é¡ã®ãããžã§ã¯ããçæã§ããŸãã ããã¯äŸ¿å©ã§ãã»ãšãã©ãã¹ãŠã®äžè¬çãªIDEã§CMakeã䜿çšã§ããŸãã
ãã©ã¡ãŒã¿ãŒãªãã§cmakeãå®è¡ããå Žåã䜿çšå¯èœãªãžã§ãã¬ãŒã¿ãŒã«ã€ããŠã¯æåŸã«èª¬æããŸãã 次ã®ããã«äœ¿çšããŸãã
fshp @ panica-desktopïŒã/ tmp $ cmakeã/ cmake / example_3 / -G "KDevelop3-Unix Makefiles"
çµè«ïŒ
ããã¯ããã¥ã¢ã«ã®ç¿»èš³ã§ã¯ãªãã1ã€ã®åæ¥ãããžã§ã¯ãã§CMakeã䜿çšããçµæã§ãã ãã®èšäºãå°ãªããšãäžäººã®äººãå©ããŠãããã°å¬ããã§ãããã·ã¢èªã§ã¯ãã®ãããªææžã¯ããªãå°ããã§ãã
ç§ãå人çã«CMakeã奜ãã ã£ããã®ïŒ
- 1ã€ã®ãããžã§ã¯ã-1ã€ã®ãã¡ã€ã«ã å€æ°ã®èšå®ããã«ãããã®ä»ã®ãžã£ã³ã¯ã¹ã¯ãªãããä¿åããå¿ èŠã¯ãããŸããã
- autotoolsãšæ¯èŒããé床ã
- ã·ã³ãã«ã§ç解ããããæ§æããã¡ãããæçµçã«ã¯Pythonã®åªé ãã«å¯Ÿæããããšã¯ã§ããŸãããããã¬ã€ã³ãã¡ã¯ã¹ã§ã¯ãããŸããã
- å€ãã®IDEã®ããã³ããšã³ãã§ãã
- é²è¡ç¶æ³ã衚瀺ããããšã¯éåžžã«äŸ¿å©ã§ãã
- è²ã®çµè«-ç°è²ã®æ¥åžžç掻ã§ã¯ãå°ããªå¡æã¯å·ã€ããŸããã
Sublime Textã«ã¯ãCMakeæ§æã®åŒ·èª¿è¡šç€ºãè¿œå ãããã©ã°ã€ã³ããããŸããããã¯ãCMakeããšåŒã°ããŸãã
äŸ