-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] CMake: 10

, 14 2017 . 16:57 +

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 Build Tool Mode.


#    myproj  myproj-build
mkdir ../myproj-build && cd ../myproj-build

#       ../myproj
cmake -DCMAKE_BUILD_TYPE=Release ../myproj

#     
cmake --build .

#  ,   '-j4'   .
cmake --build . -- -j4

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, add_subdirectory. CMakeLists:


cmake_minimum_required(VERSION 3.8 FATAL_ERROR)

project(opengl-samples)

# :    CMakeLists 
#     ,   add_subdirectory
include(scripts/functions.cmake)

add_subdirectory(libs/libmath)
add_subdirectory(libs/libplatform)
add_subdirectory(libs/libshade)

#  enable_testing    BUILD_TESTING,
#    BUILD_TESTING=ON.
#  `cmake -DBUILD_TESTING=OFF projectdir`   ,
#      .
enable_testing()

if(BUILD_TESTING)
    add_subdirectory(tests)
endif()

# .. ..

4:


. link_directories(), include_directories(), add_definitions(), add_compile_options() .



#  -
add_library(mylibrary \
    ColorDialog.h ColorDialog.cpp \
    ColorPanel.h ColorPanel.cpp)

# !  -   !
#       /usr/include/wx-3.0
#   find_package     .
target_include_directories(mylibrary /usr/include/wx-3.0)

, target_link_libraries , target_include_directories(libfoo PUBLIC ...).

, Modern CMake / an Introduction Tobias Becker:



5: , , C++17 C++14!


C++ : C++11, C++14, C++17. . , Linux Clang libc++ C++ runtime.


C++17 — CMake, .


#  :     cxx_std_17
target_compile_features(${TARGET} PUBLIC cxx_std_17)

#  :    
set_target_properties(${TARGET} PROPERTIES
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED YES
    CXX_EXTENSIONS NO
)

target_compile_features C++17 C++14, . CMake .


6:


CMake . : , , .


, . C++17 - ,


  • CMake 3.8 Visual Studio /std:c++latest C++17
  • std::experimental::filesystem Clang/libc++ , libc++experimental.a, libc++.a filesystem ; pthread, thread/mutex .. pthread

#    CMake     C++17   .
#    .
function(custom_enable_cxx17 TARGET)
    #  C++17 ,  CMake .
    target_compile_features(${TARGET} PUBLIC cxx_std_17)
    #   C++latest  Visual Studio
    if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
        set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "/std:c++latest")
    #    libc++, libc++experimental  pthread  Clang
    elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
        set_target_properties(${TARGET} PROPERTIES COMPILE_FLAGS "-stdlib=libc++ -pthread")
        target_link_libraries(${TARGET} c++experimental pthread)
    endif()
endfunction(custom_enable_cxx17)

— , CMake . . , .


, KDE, . : "Explicit is better then implicit", upstream CMake.

7 ():


3D OpenGL, GLES, DirectX Vulkan. , , (Windows, Linux, Android) Visual Studio! , Microsoft Android NDK, MSBuild .


.


- iOS Android, , XCode Android.mk. CMake, .


CMake aux_source_directory, : IDE.


  • file(GLOB ...),
  • , custom_add_executable_from_dir(name)
  • , , CMakeLists.txt, CMAKE_CURRENT_SOURCE_DIR

function(custom_add_executable_from_dir TARGET)
    #     
    file(GLOB TARGET_SRC "CMAKE_CURRENT_SOURCE_DIR/*.cpp" 
    #   
    add_executable(${TARGET} ${TARGET_SRC})
endfunction()

custom_add_library_from_dir - .


— , , , . 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 , Command-Line Tool Mode.


9:


, . :


  • CMakeLists.txt add_subdirectory
  • ; — ,

, :


#  -
add_library(foo ${FOO_SRC})

#  ,     
add_library(MyOrg::foo ALIAS foo)

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 ctest.


CTest , enable_testing


#  enable_testing    BUILD_TESTING,
#    BUILD_TESTING=ON.
#  `cmake -DBUILD_TESTING=OFF projectdir`   ,
#      .
enable_testing()

if(BUILD_TESTING)
    add_subdirectory(tests/libhellotest)
    add_subdirectory(tests/libgoodbyetest)
endif()

CTest, add_test.


#    -     
add_executable(${TARGET} ${TARGET_SRC})

#     CMake   .
#      ,       .
add_test(${TARGET} ${TARGET})

?


, :



. CMake.

Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/330902/

:  

: [1] []
 

:
: 

: ( )

:

  URL