############################################################ # Create a library ############################################################
#Generate the shared library from the library sources # add_library函数用于从一些源文件创建共享库 add_library(hello_library SHARED src/Hello.cpp )
# 创建一个别名目标 hello::library,它指向实际的库目标 hello_library。 add_library(hello::library ALIAS hello_library)
target_include_directories(hello_library PUBLIC ${PROJECT_SOURCE_DIR}/include )
############################################################ # Create an executable ############################################################
# Add an executable with the above sources add_executable(hello_binary src/main.cpp )
# link the new hello_library target with the hello_binary target # 链接共享库与链接静态库是相同的。在创建可执行文件时,使用 `target_link_library()` 函数指向你的库。 target_link_libraries( hello_binary PRIVATE hello::library )
# 输出消息,提示用户将构建类型设置为RelWithDebInfo,因为没有指定任何构建类型。 message("Setting build type to 'RelWithDebInfo' as none was specified.")
# 将构建类型设置为RelWithDebInfo,并将其缓存为一个字符串类型的变量。FORCE选项确保即使该变量之前被设置,也会被覆盖。 set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING"Choose the type of build." FORCE)
if [ -d "$ROOT_DIR/$dir/build.clang" ]; then echo "deleting $dir/build.clang" rm -r $dir/build.clang fi
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#run_tesh.sh #!/bin/bash # Ubuntu supports multiple versions of clang to be installed at the same time. # The tests need to determine the clang binary before calling cmake clang_bin=`which clang` clang_xx_bin=`which clang++`
if [ -z $clang_bin ]; then clang_ver=`dpkg --get-selections | grep clang | grep -v -m1 libclang | cut -f1 | cut -d '-' -f2` clang_bin="clang-$clang_ver" clang_xx_bin="clang++-$clang_ver" fi
echo "Will use clang [$clang_bin] and clang++ [$clang_xx_bin]"
mkdir -p build.clang && cd build.clang && \ cmake .. -DCMAKE_C_COMPILER=$clang_bin -DCMAKE_CXX_COMPILER=$clang_xx_bin && make
生成ninja构建文件
1 2 3 4 5 6 7 8 9 10
# Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version cmake_minimum_required(VERSION 3.5)
# Set the project name project (hello_cmake)
# Add an executable add_executable(hello_cmake main.cpp)
# check results and add flag if(COMPILER_SUPPORTS_CXX11)# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") elseif(COMPILER_SUPPORTS_CXX0X)# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x") else() message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.") endif()
# Add an executable add_executable(hello_cpp11 main.cpp)
2
1 2 3 4 5 6 7 8 9 10 11 12 13
# Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version cmake_minimum_required(VERSION 3.1)
# Set the project name project (hello_cpp11)
# set the C++ standard to C++ 11 set(CMAKE_CXX_STANDARD 11)
# Add an executable add_executable(hello_cpp11 main.cpp)
3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version cmake_minimum_required(VERSION 3.1)
# Set the project name project (hello_cpp11)
# Add an executable add_executable(hello_cpp11 main.cpp)
# set the C++ standard to the appropriate standard for using auto target_compile_features(hello_cpp11 PUBLIC cxx_auto_type)
# Print the list of known compile features for this version of CMake message("List of compile features: ${CMAKE_CXX_COMPILE_FEATURES}")