Last active
April 22, 2022 01:17
-
-
Save ericzhng/6f6cb455fb35cea4e9059536d7da506f to your computer and use it in GitHub Desktop.
An example CMake Visual Studio Project. Both user and software (find_package) can decide on using certain 3rd party external library. Add USE_XXX as macro in either CMakeLists.txt and C/C++ programs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CMakeList.txt : CMake project for test_cmake, include source and define | |
# project specific logic here. | |
# | |
cmake_minimum_required (VERSION 3.8) | |
project ("test_cmake") | |
# Find OpenMP package | |
find_package(OpenMP) | |
set(CMAKE_CXX_STANDARD 14) | |
set(CMAKE_CXX_STANDARD_REQUIRED True) | |
# needs to setup VCPKG_ROOT in PATH before continuing | |
message("VCPKG_ROOT: " "$ENV{VCPKG_ROOT}") | |
set(SOFT_LIST "Boost;GSL;suitesparse;Armadillo;absl;TBB") | |
foreach(_LIB_ ${SOFT_LIST}) | |
message("===============================") | |
string(TOUPPER ${_LIB_} _LIB_UP_) | |
set(USE_FLAG USE_${_LIB_UP_}) | |
set(${USE_FLAG} TRUE CACHE BOOL "Use ${_LIB_} library") | |
find_package(${_LIB_}) | |
if(${_LIB_}_FOUND) | |
message(STATUS "${_LIB_} library is found; you may use ${_LIB_}") | |
# if not empty, print variables | |
foreach(VAR_SUF DIR;INCLUDE_DIRS;LIBRARY;LIBRARIES) | |
if (NOT "${${_LIB_}_${VAR_SUF}}" STREQUAL "") | |
MESSAGE(STATUS " -- ${_LIB_}_${VAR_SUF}:") | |
MESSAGE(STATUS " " ${${_LIB_}_${VAR_SUF}}) | |
endif() | |
endforeach() | |
else() | |
# set USE_FLAG to false | |
set(${USE_FLAG} OFF CACHE BOOL "Use ${_LIB_} library" FORCE) | |
MESSAGE(STATUS " ${_LIB_} package was NOT found, it will not be used" ) | |
endif() | |
message(STATUS "${USE_FLAG}: " "${${USE_FLAG}}") | |
message("===============================") | |
endforeach() | |
message("===============================") | |
foreach(_LIB_ ${SOFT_LIST}) | |
string(TOUPPER ${_LIB_} _LIB_UP_) | |
set(USE_FLAG USE_${_LIB_UP_}) | |
message(STATUS "${USE_FLAG}: " "${${USE_FLAG}}") | |
endforeach() | |
message("===============================") | |
# Add source to this project's executable. | |
add_executable (test_cmake "test_cmake.cpp" "test_cmake.h") | |
# TODO: Add tests and install targets if needed. | |
if(USE_ABSL) | |
# add extra definition for CPP | |
target_compile_definitions(test_cmake PRIVATE USE_ABSL) | |
target_link_libraries(test_cmake PRIVATE absl::strings) | |
endif() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": 3, | |
"configurePresets": [ | |
{ | |
"name": "windows-base", | |
"hidden": true, | |
"generator": "Ninja", | |
"binaryDir": "${sourceDir}/out/build/${presetName}", | |
"installDir": "${sourceDir}/out/install/${presetName}", | |
"cacheVariables": { | |
"CMAKE_C_COMPILER": "cl.exe", | |
"CMAKE_CXX_COMPILER": "cl.exe", | |
"CMAKE_TOOLCHAIN_FILE": { | |
"value": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake", | |
"type": "FILEPATH" | |
} | |
}, | |
"condition": { | |
"type": "equals", | |
"lhs": "${hostSystemName}", | |
"rhs": "Windows" | |
} | |
}, | |
{ | |
"name": "x64-debug", | |
"displayName": "x64 Debug", | |
"inherits": "windows-base", | |
"architecture": { | |
"value": "x64", | |
"strategy": "external" | |
}, | |
"cacheVariables": { | |
"CMAKE_BUILD_TYPE": "Debug" | |
} | |
}, | |
{ | |
"name": "x64-release", | |
"displayName": "x64 Release", | |
"inherits": "x64-debug", | |
"cacheVariables": { | |
"CMAKE_BUILD_TYPE": "Release" | |
} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please feel free to throw a message here in case you have any questions.