mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-21 06:10:16 +08:00

Since commit 07223c5c27
(Tutorial: Update Step 5 to work on Windows,
2020-02-18, v3.18.0-rc1~655^2) the logic does not work on non-Windows
platforms when cmake is re-run on an existing build tree. It is also
more complicated than we'd like for a tutorial example. Avoid the need
to consider the `m` library case by performing the check as C++.
Since `check_cxx_symbol_exists` cannot handle overloaded functions
like `exp` and `log`, check with `check_cxx_source_compiles` instead.
This also presents a more general-purpose example in the tutorial.
Fixes: #23524
35 lines
846 B
CMake
35 lines
846 B
CMake
add_library(MathFunctions mysqrt.cxx)
|
|
|
|
# state that anybody linking to us needs to include the current source dir
|
|
# to find MathFunctions.h, while we don't.
|
|
target_include_directories(MathFunctions
|
|
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
|
|
)
|
|
|
|
# does this system provide the log and exp functions?
|
|
include(CheckCXXSourceCompiles)
|
|
check_cxx_source_compiles("
|
|
#include <cmath>
|
|
int main() {
|
|
std::log(1.0);
|
|
return 0;
|
|
}
|
|
" HAVE_LOG)
|
|
check_cxx_source_compiles("
|
|
#include <cmath>
|
|
int main() {
|
|
std::exp(1.0);
|
|
return 0;
|
|
}
|
|
" HAVE_EXP)
|
|
|
|
# add compile definitions
|
|
if(HAVE_LOG AND HAVE_EXP)
|
|
target_compile_definitions(MathFunctions
|
|
PRIVATE "HAVE_LOG" "HAVE_EXP")
|
|
endif()
|
|
|
|
# install rules
|
|
install(TARGETS MathFunctions DESTINATION lib)
|
|
install(FILES MathFunctions.h DESTINATION include)
|