CMakeã¯C / C ++çšã®ãã«ãã·ã¹ãã ã§ãããæ¯å¹ŽãŸããŸãæ®åããŠããŸãã å®éã«ã¯ãæ°ãããããžã§ã¯ãã®ããã©ã«ãã®ãœãªã¥ãŒã·ã§ã³ã«ãªããŸããã ãã ããCMakeã§ã¿ã¹ã¯ãå®è¡ããå€ãã®äŸã«ã¯ãå€é¢šã§ä¿¡é Œæ§ã®äœãè¥å€§åããã¢ã¯ã·ã§ã³ãå«ãŸããŠããŸãã CMakeã§ã¢ã»ã³ããªã¹ã¯ãªãããèšè¿°ããæ¹æ³ãããç°¡æœã«èª¬æããŸãã
å®éã®ãã³ããè©ŠããŠã¿ããå Žåã¯ãgithubã§äŸãåãäžããèšäºãèªãã§ãããæ¢çŽ¢ããŠãã ããïŒ https : //github.com/sergey-shambir/modern-cmake-sample
ãã³ãïŒ1ïŒCMakeã®é«ãæå°ããŒãžã§ã³ãæå®ãã
å€ãéçºç°å¢ãšã®äºææ§ãéèŠã§ããããããã®ã¢ããã€ã¹ã¯å
¬éã©ã€ãã©ãªãäœæãã人ã«ã¯é©çšãããŸããã ãŸããã¯ããŒãºããœãŒã¹ãããžã§ã¯ããŸãã¯é«åºŠã«å°éåããããªãŒãã³ãœãŒã¹ãœãããŠã§ã¢ãäœæããŠããå Žåã¯ããã¹ãŠã®éçºè
ã«CMakeã®ææ°ããŒãžã§ã³ã®ã€ã³ã¹ããŒã«ãèŠæ±ã§ããŸãã ããããªããšãå€ãã®èšäºã®ãã³ããæ©èœããŸããïŒ å·çæç¹ã§ãCMake 3.8ããããŸãã
cmake_minimum_required(VERSION 3.8 FATAL_ERROR)
ãã³ãïŒ2ïŒmakeãmake installãåŒã³åºããªãã§ãã ãã
ææ°ã®CMakeã¯ããã«ãã·ã¹ãã èªäœãåŒã³åºãããšãã§ããŸãã CMakeã®ããã¥ã¡ã³ãã§ã¯ããã®ã¢ãŒãã¯ãã«ãããŒã«ã¢ãŒããšåŒã°ããŸãã
Visual Studioãããžã§ã¯ããçæããŠããå Žåã¯ãç¹å®ã®æ§æã§ç¹å®ã®ãããžã§ã¯ãããã«ãã§ãããªã©ãã³ãã³ãã©ã€ã³ãããã«ãããããšãã§ããŸãã
cmake --build . \ --target myapp \ --config Release \ --clean-first
Linuxã§ã¯ãmake installã䜿çšããªãã§ãã ããã䜿çšãããšã·ã¹ãã ãè©°ãŸããŸãã ããã«ã€ããŠã¯å¥ã®èšäºããããŸãã æ®ã£ãŠæ®åœ±ãããããŸãã¯make installã䜿çšããªãçç±ã«é¢ããæè²ããã°ã©ã
ãã³ãïŒ3ïŒè€æ°ã®CMakeLists.txtã䜿çšãã
CMakeLists.txtã®ãã¹ãã¯åé¡ãããŸããã ãããžã§ã¯ãã3ã€ã®ã©ã€ãã©ãªã3ã€ã®ãã¹ãã¹ã€ãŒãã2ã€ã®ã¢ããªã±ãŒã·ã§ã³ã«åãããŠããå Žåãããããã«CMakeLists.txt
ãè¿œå ããŠCMakeLists.txt
ãŸãããïŒ æ¬¡ã«ãå¥ã®äžå€®CMakeLists.txt
ãäœæãããã®äžã®CMakeLists.txt
ãå®è¡ããå¿
èŠããããŸãã ããã¯ãäžå€®ã®CMakeListsã®ããã«èŠããå ŽåããããŸãã
cmake_minimum_required(VERSION 3.8 FATAL_ERROR) project(opengl-samples)
ãã³ã4ïŒã°ããŒãã«ã¹ã³ãŒããè©°ãŸãããªã
絶察ã«å¿
èŠã§ãªãéããã°ããŒãã«å€æ°ãéå§ããªãã§ãã ããã link_directories()
ã include_directories()
ã add_definitions()
ã add_compile_options()
ããã³ãã®ä»ã®åæ§ã®åœä»€ã䜿çšããªãã§ãã ããã
- target_link_librariesã䜿çšããŠãã¿ãŒã²ãããäŸåããéçããã³åçãå
éšããã³å€éšã©ã€ãã©ãªãè¿œå ããŸã
- include_directoriesã®ä»£ããã«target_include_directoriesã䜿çšããŠãã¿ãŒã²ãããäŸåããããããŒã®æ€çŽ¢ãã¹ãè¿œå ããŸã
- add_definitionsã®ä»£ããã«target_compile_definitionsã䜿çšããŠãã¿ãŒã²ãããããããšããŠãããã¯ããè¿œå ããŸã
- target_compile_optionsã䜿çšããŠãã¿ãŒã²ããã䜿çšããç¹å®ã®ã³ã³ãã€ã©ãã©ã°ãè¿œå ããŸã
ã©ã€ãã©ãªããããžã§ã¯ãå
ã«ãããããããŒæ€çŽ¢ãã¹ãtarget_include_directories(libfoo PUBLIC ...)
ã³ã³ã¹ãã©ã¯ããéããŠã©ã€ãã©ãªã«ã¢ã¿ãããããŠããå Žåã target_link_libraries
ãã©ã€ãã©ãªããããŒæ€çŽ¢ãã¹ãè¿œå ã§ããããšã«æ³šæããŠãã ããã
ã¢ãã³CMakeã®ãã¬ãŒã³ããŒã·ã§ã³/ Tobias Beckerã«ãã玹ä»ããæç²ããäŸåé¢ä¿å³ã®äŸããããŸãã
ãã³ãïŒ5ïŒæçµçã«C ++ 17ãŸãã¯C ++ 14ãæå¹ã«ããïŒ
è¿å¹ŽãC ++æšæºã¯é »ç¹ã«æŽæ°ãããŠããŸããC++ 11ãC ++ 14ãC ++ 17ã«ã¯é©ãã¹ãå€æŽãå ããããŠããŸãã å¯èœãªéããå€ãã³ã³ãã€ã©ãæŸæ£ããããã«ããŠãã ããã ããšãã°ãLinuxã®å ŽåãClangãšlibc ++ã®ææ°ããŒãžã§ã³ãã€ã³ã¹ããŒã«ããéçC ++ã©ã³ã¿ã€ã ã¬ã€ã¢ãŠãã§ãã¹ãŠã®ãããžã§ã¯ãã®ãã«ããéå§ããããšã劚ãããã®ã¯ãããŸããã
ã³ã³ãã€ã«ãã©ã°ã䜿çšããã«C ++ 17ãæå¹ã«ããæè¯ã®æ¹æ³ã¯ãCMakeã«å¿
èŠã§ããããšãæ瀺çã«äŒããããšã§ãã
target_compile_features
ã䜿çšãããšãC ++ 17ãŸãã¯C ++ 14ã§ã¯ãªããã³ã³ãã€ã©åŽã®ç¹å®ã®æ©èœãèŠæ±ã§ããŸãã ããç¥ãããŠããCMakeæ©èœã³ã³ãã€ã©ã®å®å
šãªãªã¹ãã¯ãããã¥ã¡ã³ãã«ãããŸã ã
ãã³ãïŒ6ïŒé¢æ°ã䜿çšãã
CMakeã§ã¯ãé¢æ°ãã¯ããšé¢æ°ã宣èšã§ããŸãã 2ã€ã®éãã¯1ã€ã ãã§ããé¢æ°å
ã«èšå®ãããå€æ°ã¯ããŒã«ã«ã§ãã
ã¢ã»ã³ããªã®ã«ã¹ã¿ãã€ãºã®çŸåšã®åé¡ã解決ããããå€ãã®ã¢ã»ã³ããªã®ç®æšãç°¡åã«è¿œå ãããããããã®é¢æ°ãäœæãããšäŸ¿å©ã§ãã 以äžã®äŸã¯ãC ++ 17ãããæ£ç¢ºã«æå¹ã«ããããã«æžãããŸããã
- CMake 3.8ã¯ã
/std:c++latest
ãæž¡ãæ¹æ³ããŸã ç¥ããªã/std:c++latest
C ++ 17ãæå¹ã«ããããã«Visual Studioã«/std:c++latest
ãã©ã° - Clang / libc ++ã§
std::experimental::filesystem
Experimental std::experimental::filesystem
ã䜿çšããå Žåã libc++experimental.a
ã«ã¯ãã¡ã€ã«ã·ã¹ãã ã¢ãžã¥ãŒã«ããªãããããããžã§ã¯ããlibc++experimental.a
ãšãªã³ã¯ããå¿
èŠãããããšããªã³ã«ãŒã«äŒããå¿
èŠããããŸãã ã¹ã¬ãã/ãã¥ãŒããã¯ã¹ã®å®è£
ãªã©ã®ãããpthreadãšãªã³ã¯ããå¿
èŠããããŸãã pthreadã«äŸå
åé¢æ°ã¯ãæ¬è³ªçã«CMakeèšèªãŸãã¯ãã®åäœããªãŒããŒã©ã€ãããããã«èšèšãããããã¯ã§ãã ä»ã®éçºè
ã«ãšã£ãŠããã®ãããã³ã°ã®æå³ã¯æ確ã§ã¯ãããŸããã ãããã£ãŠããã®ç®çãšæå³ã説æããé¢æ°å
ã®ååœä»€ã«ã³ã¡ã³ããè¿œå ããŠã¿ãŠãã ããã
KDEãªã©ã®å€§èŠæš¡ãªãªãŒãã³ãœãŒã¹ãããžã§ã¯ãã§ã¯ããããã®é¢æ°ã䜿çšããã®ã¯äžé©åãªåœ¢åŒã§ãã ä»ã®ãªãã·ã§ã³ãæ€èšããããšãã§ããŸãïŒãæ瀺çã§ããããšãæé»çã§ããããšããååã«åºã¥ããŠãã«ãã¹ã¯ãªãããæ瀺çã«èšè¿°ããããCMakeãããžã§ã¯ãã®äžæµã«ç¬èªã®é¢æ°ãè¿œå ããããšãææ¡ããããšãã§ããŸãã
ãã³ãïŒ7ïŒç©è°ãéžãïŒïŒãœãŒã¹ãã¡ã€ã«ãäžåºŠã«1ã€ãã€ãªã¹ãããªã
ç§ã®ååã¯ãOpenGLãGLESãDirectXãVulkanãä»ããŠã¢ãã«ãšã¢ãã¡ãŒã·ã§ã³ãå«ãã·ãŒã³ãã¬ã³ããªã³ã°ããããã®å°ããª3Dãšã³ãžã³ãéçºããŠããŸãã ãã®ãããžã§ã¯ãã«ã€ããŠåœŒãšè©±ãåã£ããšããã圌ã¯Visual Studioã䜿çšããŠãã¹ãŠã®ãã©ãããã©ãŒã ïŒWindowsãLinuxãAndroidïŒãæ§ç¯ããŠããããšãããããŸããïŒ åœŒã¯ãMicrosoftãAndroid NDKããã£ãã«æŽæ°ããªãããšã«äžæºãæ±ããŠãããã1ã€ã®ç°¡åãªçç±ã§MSBuildã«ãããã«ããæåŠããããªãã
圌ã¯ã2ã€ã®ãã«ãã·ã¹ãã ã®ã¢ã»ã³ããªã®ãã¡ã€ã«ã®ãªã¹ãã«åè¡ããããããŸããã
iOSããAndroidã«ã²ãŒã ã移æ€ããXcodeãããžã§ã¯ããèªã¿åãã Android.mk
ã®ãã¡ã€ã«ã®ãªã¹ããèªåçã«å±éããã¹ã¯ãªããã䜿çšããŠ2ã€ã®ãã«ãã·ã¹ãã ããµããŒããAndroid.mk
ã CMakeã䜿çšããå Žåãã¹ã¯ãªãããäœæããå¿
èŠãããããŸããã
CMakeã«ã¯aux_source_directory
é¢æ°ããããŸãããæ¬ ç¹ããããŸãïŒããããŒããªã¹ãã«è¿œå ããããIDEçšã«çæããããããžã§ã¯ãã«è¡šç€ºãããŸããã
file(GLOB ...)
ãå©ãã«ãªãããã¹ã¯ã§ãã¡ã€ã«ãã¹ãã£ã³ããŸã- æ°ããå€æ°ã§ã°ããŒãã«ã¹ã³ãŒãã
custom_add_executable_from_dir(name)
ããã«ãé¢æ°custom_add_executable_from_dir(name)
äœæããŸã - å¥ã®ãã¡ã€ã«ã«é
眮ãããé¢æ°ãçŸåšã®CMakeLists.txtãžã®ãã¹ãåç
§ãã€ã³ããšããŠäœ¿çšããããã«ãå€æ°
CMAKE_CURRENT_SOURCE_DIR
ã䜿çšããŸã
function(custom_add_executable_from_dir TARGET)
ã©ã€ãã©ãªã¿ãŒã²ããã®custom_add_library_from_dir
é¢æ°ãåãæ¹æ³ã§è¿œå ã§ããŸãã
ããªããæäœãã®ãã¡ã³ã§ããããå
Œ
±å³æžé€šãäœæããå Žåã¯ãããããäžåºŠã«1ã€ãã€ãã¡ã€ã«ãè¿œå ããæ¹ãããã§ãããã ãã®å Žåã target_sources
ã䜿çšããŠãã©ãããã©ãŒã åºæã®ãã¡ã€ã«ãè¿œå ããŸãã
add_library(libfoo Foo.h Foo_common.cpp) if(WIN32) target_sources(libfoo Foo_win32.cpp) endif(WIN32)
ãã³ãïŒ8ïŒbashãŠãŒãã£ãªãã£ãå®è¡ãããcmake -Eãå®è¡ãã
èªååã®ããã«cmakeããBashã³ãã³ããåŒã³åºããŠããã£ã¬ã¯ããªãäœæããããã¢ãŒã«ã€ãã解åããããmd5ã®éãèšç®ãããããããšã¯ééããããŸããã ãã ããã³ãã³ãã©ã€ã³ãŠãŒãã£ãªãã£ãåŒã³åºããšãã¯ãã¹ãã©ãããã©ãŒã ã®ãããžã§ã¯ãã奪ãããå¯èœæ§ããããŸãã ãã移æ€æ§ã®é«ãæ¹æ³ã¯ã ã³ãã³ãã©ã€ã³ããŒã«ã¢ãŒãã䜿çšããŠcmake -E
ãåŒã³åºãããšcmake -E
ã
ãã³ãïŒ9ïŒã©ã€ãã©ãªãŠãŒã¶ãŒã®æè»æ§ãé«ãã
å
¬ã«å©çšå¯èœãªã©ã€ãã©ãªãäœæããŠããå Žåã¯ãã¢ããã€ã¹ãé©çšãããŸãã ãã®å Žåã次ã®ã·ããªãªãç°¡çŽ åããå¿
èŠããããŸãã
- ããã°ã©ãã¯ã©ã€ãã©ãªããµãã¢ãžã¥ãŒã«ãšããŠè¿œå ãã
CMakeLists.txt
ãä»ããŠCMakeLists.txt
ãæå¹ã«ãadd_subdirectory
- ããã°ã©ããŒã¯ãã©ã€ãã©ãªãŒã®ã¢ã»ã³ããªãŒã§ã«ã¹ã¿ãã€ãºãè¡ãããã æãäžè¬çãªã®ã¯ãå®å
šã«éçãŸãã¯åçãªã¬ã€ã¢ãŠãããã¹ããšãµã³ãã«ãå«ãã¢ã»ã³ããªããŸãã¯ã©ã€ãã©ãªã®ã¿ã§ã
ã©ã€ãã©ãªãè¿œå ãããšãã«ãäžæã®å矩èªãäœæããŸãã
# - add_library(foo ${FOO_SRC}) # , add_library(MyOrg::foo ALIAS foo)
ã©ã€ãã©ãªãŠãŒã¶ãŒã«ã BUILD_SHARED_LIBS
ãªãã·ã§ã³ã䜿çšããŠãã©ã€ãã©ãªã®éçããŒãžã§ã³ãšåçããŒãžã§ã³ã®ã©ã¡ããBUILD_SHARED_LIBS
ããããéžæããBUILD_SHARED_LIBS
ãŸãã
ã¬ã€ã¢ãŠãèšå®ãèšå®ããããããŒãšã©ã€ãã©ãªã®ã³ã³ãã€ã«ãã©ã°ãæ€çŽ¢ããå ŽåãããŒã¯ãŒãPUBLICãPRIVATEãINTERFACEã䜿çšããŠãã©ã€ãã©ãªã«äŸåããã¿ãŒã²ãããå¿
èŠãªèšå®ãç¶æ¿ã§ããããã«ããŸãã
- PUBLICã¯ãã©ã€ãã©ãªäŸåãããžã§ã¯ãã«ããããã®ãªãã·ã§ã³ãå¿
èŠã§ããããšãæå³ããŸã
- PRIVATEã¯ãã©ã€ãã©ãªããã«ãããããã«ã®ã¿ãªãã·ã§ã³ãå¿
èŠã§ããããšãæå³ããŸã
- INTERFACEã¯ãã¢ã»ã³ããªã«ã¯ãªãã·ã§ã³ã¯å¿
èŠãªãããã©ã€ãã©ãªã䜿çšããã«ã¯ãªãã·ã§ã³ãå¿
èŠã§ããããšãæå³ããŸã
target_link_libraries(foobarapp PUBLIC MyOrg::libfoo PRIVATE MyOrg::libbar )
ãã³ãïŒ10ïŒCTestã§èªåãã¹ããç»é²ãã
CTestãµãã·ã¹ãã ã¯ãéåžžã®Boost.TestãCatchããŸãã¯Google Testsã®ä»£ããã«ããã¹ãã®ããã«ç¹å¥ãªã©ã€ãã©ãªã䜿çšããããšã匷å¶ããŸããã CMakeããã¹ãŠã®ãã¹ããŸãã¯éžæãããã¹ãã1ã€ã®ctest
ã³ãã³ãã§å®è¡ã§ããããã«ã ctest
ãã¹ãã®ã¿ãç»é²ããŸãã
ãããžã§ã¯ãå
šäœã§CTestãµããŒããæå¹ã«ããããã«ã enable_testing
CTestã®ãã¹ãã«å®è¡å¯èœãã¡ã€ã«ãç»é²ããã«ã¯ã add_test
ãåŒã³åºãå¿
èŠããããŸãã
ä»ã«èªããã®ã¯äœã§ããïŒ
OpenSourceãããžã§ã¯ãçšã«CMakeãšCTestãæ§æããå Žåãç¶ç¶ççµ±åãæå¹ã«ã§ããŸããCMakeã¢ã»ã³ããªã䜿çšããGitHub C / C ++ãããžã§ã¯ãã®ç¶ç¶ççµ±åïŒCIïŒ
èšäºãäœæããåã«ãããã€ãã®è±èªã®è³æãèªã¿ããã¹ãããåèããŸããã
ãããã®ãœãŒã¹ããã®ããã€ãã®ãã³ãã¯ãèšäºã«ã¯åæ ãããŠããŸããã ãããã£ãŠãããããèªãã åŸãããªãã¯ééããªãCMakeã«ç¿çããã§ãããã