project_options
v0.41.0
通用CMAKE庫,可提供在最佳實踐後改善CMAKE體驗的功能。
project_options :package_project :通過find_package/target_link通過cmake的fetchcontent,vcpkg,find_package/target_link進行自動包裝/安裝項目,以用於無縫使用。run_vcpkg :VCPKG的自動安裝和項目依賴項run_conan :自動安裝柯南和項目依賴項dynamic_project_options :圍繞project_options包裝器,以動態更改飛行的選項target_link_system_libraries和target_include_system_directories :鏈接/包括外部依賴關係/無警告的標題target_link_cuda :將CUDA鏈接到目標完整的文檔可在此處提供:
https://aminya.github.io/project_options/
project_options功能在此模板存儲庫中的操作中,請參見project_options() 。 CPP_VCPKG_Project為生產就緒的C ++項目準備了所有最佳實踐。
project和project_options這是用法的示例:
cmake_minimum_required ( VERSION 3.20)
# set a default CXX standard for the tools and targets that do not specify them.
# If commented, the latest supported standard for your compiler is automatically set.
# set(CMAKE_CXX_STANDARD 20)
include (FetchContent)
if ( CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0" )
cmake_policy (SET CMP0135 NEW)
endif ()
# Add project_options from https://github.com/aminya/project_options
# Change the version in the following URL to update the package (watch the releases of the repository for future updates)
set (PROJECT_OPTIONS_VERSION "v0.41.0" )
FetchContent_Declare(
_project_options
URL https://github.com/aminya/project_options/archive/refs/tags/ ${PROJECT_OPTIONS_VERSION} .zip)
FetchContent_MakeAvailable(_project_options)
include ( ${_project_options_SOURCE_DIR} /Index.cmake)
# install vcpkg dependencies: - should be called before defining project()
run_vcpkg(
VCPKG_URL "https://github.com/microsoft/vcpkg.git"
VCPKG_REV "86a181505ac6460f98496a79abdee6a0f49905ec"
)
# Install conan dependencies: - should be called before defining project()
run_conan()
# Set the project name and language
project (myproject LANGUAGES CXX C)
# Build Features
option (FEATURE_TESTS "Enable the tests" OFF )
option (FEATURE_DOCS "Enable the docs" OFF )
# vcpkg test feature
if (FEATURE_TESTS)
list ( APPEND VCPKG_MANIFEST_FEATURES "tests" )
endif ()
# Enable sanitizers and static analyzers when running the tests
if (FEATURE_TESTS)
set (ENABLE_CLANG_TIDY "ENABLE_CLANG_TIDY" )
set (ENABLE_CPPCHECK "ENABLE_CPPCHECK" )
set (ENABLE_COVERAGE "ENABLE_COVERAGE" )
set (ENABLE_SANITIZER_ADDRESS "ENABLE_SANITIZER_ADDRESS" )
set (ENABLE_SANITIZER_UNDEFINED "ENABLE_SANITIZER_UNDEFINED" )
endif ()
# Enable doxgen for the docs
if (FEATURE_DOCS)
set (ENABLE_DOXYGEN "ENABLE_DOXYGEN" )
endif ()
# Initialize project_options variable related to this project
# This overwrites `project_options` and sets `project_warnings`
# uncomment to enable the options. Some of them accept one or more inputs:
project_options(
PREFIX "myproject"
ENABLE_CACHE
${ENABLE_CPPCHECK}
${ENABLE_CLANG_TIDY}
ENABLE_VS_ANALYSIS
# ENABLE_INTERPROCEDURAL_OPTIMIZATION
# ENABLE_NATIVE_OPTIMIZATION
${ENABLE_DOXYGEN}
${ENABLE_COVERAGE}
${ENABLE_SANITIZER_ADDRESS}
${ENABLE_SANITIZER_UNDEFINED}
# ${ENABLE_SANITIZER_THREAD}
# ${ENABLE_SANITIZER_MEMORY}
# ENABLE_SANITIZER_POINTER_COMPARE
# ENABLE_SANITIZER_POINTER_SUBTRACT
# ENABLE_CONTROL_FLOW_PROTECTION
# ENABLE_STACK_PROTECTION
# ENABLE_OVERFLOW_PROTECTION
# ENABLE_ELF_PROTECTION
# ENABLE_RUNTIME_SYMBOLS_RESOLUTION
# ENABLE_COMPILE_COMMANDS_SYMLINK
# ENABLE_PCH
# PCH_HEADERS
# WARNINGS_AS_ERRORS
# ENABLE_INCLUDE_WHAT_YOU_USE
# ENABLE_GCC_ANALYZER
# ENABLE_BUILD_WITH_TIME_TRACE
# ENABLE_UNITY
# LINKER "lld"
)然後將可執行文件或庫添加到項目:
add_executable (main main.cpp)
# link project_options/warnings
target_link_libraries (main
PRIVATE myproject_project_options myproject_project_warnings
)
# Find dependencies:
target_find_dependencies(main
PRIVATE_CONFIG
fmt
Eigen3
)
# Link dependencies
target_link_system_libraries(main
PRIVATE
fmt::fmt
Eigen3::Eigen
)
# Package the project
package_project(TARGETS main) add_library (my_lib "./src/my_lib/lib.cpp" )
# link project_options/warnings
target_link_libraries (my_lib
PRIVATE myproject_project_options myproject_project_warnings
)
# Includes:
target_include_interface_directories(my_lib " ${CMAKE_CURRENT_SOURCE_DIR} /include" )
# Find dependencies:
target_find_dependencies(my_lib
PRIVATE_CONFIG
fmt
Eigen3
)
# Link dependencies:
target_link_system_libraries(my_lib
PRIVATE
fmt::fmt
Eigen3::Eigen
)
# Package the project
package_project(
# Note that you must export `myproject_project_options` and `myproject_project_warnings` for `my_lib`
TARGETS my_lib myproject_project_options myproject_project_warnings
) add_library (my_header_lib INTERFACE )
# link project_options/warnings
target_link_libraries (my_header_lib
INTERFACE myproject_project_options myproject_project_warnings
)
# Includes:
target_include_interface_directories(my_header_lib " ${CMAKE_CURRENT_SOURCE_DIR} /include" )
# Find dependencies:
target_find_dependencies(my_header_lib
INTERFACE_CONFIG
fmt
Eigen3
)
# Link dependencies:
target_link_system_libraries(my_header_lib
INTERFACE
fmt::fmt
Eigen3::Eigen
)
# Package the project
package_project(
TARGETS my_header_lib myproject_project_options myproject_project_warnings
)該項目可以根據您的選擇,根據MIT許可證或不執行的條款使用。