c++ - CMake 无法检测 gcc 内置函数,如 sqrt、pow、exp 等

标签 c++ c gcc cmake g++

我只是想用CMake来检测GCC的一些内置函数是否存在。所以我使用 check_function_exists 命令来做一些检查工作。这是我在 CMakeLists.txt 中的代码片段。

include (CheckFunctionExists)
set (CMAKE_REQUIRED_INCLUDES math.h)
set (CMAKE_REQUIRED_LIBRARIES m)
check_function_exists(sqrt HAVE_SQRT)
check_function_exists (pow HAVE_POW)
check_function_exists (exp HAVE_EXP)
check_function_exists (log HAVE_LOG)

Code snippet in TutorialConfig.h.in

// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR  @Tutorial_VERSION_MINOR@
#cmakedefine USE_MYMATH
#cmakedefine HAVE_SQRT
#cmakedefine HAVE_POW
#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP

但是,我在源目录下执行Cmake命令时的目录。我得到了这样的错误,很奇怪。

-- The C compiler identification is GNU 7.0.0
-- The CXX compiler identification is GNU 7.0.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for sqrt
CMake Error: The source directory "../build/CMakeFiles/CMakeTmp" does not exist.
Specify --help for usage, or press the help button on the CMake GUI.
CMake Error at /usr/share/cmake/Modules/CheckFunctionExists.cmake:70 (try_compile):
  Failed to configure test project build system.
Call Stack (most recent call first):
  CMakeLists.txt:23 (check_function_exists)

但是,错误信息中声称的目录确实存在。

$ ls -la ../build/CMakeFiles/CMakeTmp 
  total 0
  drwxrwxrwx 1 root root 0 Apr 14 21:41 .
  drwxrwxrwx 1 root root 0 Apr 14 20:32 ..

我对CMake不是很了解,所以我无法弄清楚症结所在。

这里是my log file outputed by CMake

希望熟悉CMake的 friend 帮帮我。提前致谢。

最佳答案

错误信息

CMake Error: The source directory "../build/CMakeFiles/CMakeTmp" does not exist.

如果将相对 路径用作构建目录,则这是可能的。

虽然在文档中没有明确说明(也没有被 CMake 本身检查),但最好使用绝对路径作为构建目录


因为 CMake 项目的 build 目录没有被设计成移动到其他地方(甚至在其他机器上),所以没有理由使用相对路径

关于c++ - CMake 无法检测 gcc 内置函数,如 sqrt、pow、exp 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43412943/

相关文章:

为 ARM 进行 Clang 交叉编译?

c - 理解递归函数时遇到问题

char* str ="..."与 char str[] ="..."奇怪的行为

c++ - supc++ 的链接顺序真的很重要吗?

c++ - 如何从输入字符串中获取字符数?

c - 使用汇编语言为 C 程序编写函数

c++ - 编译器在前向结构声明中给出错误

c++ - 使用 vector 按分隔符拆分字符串 - 如何按换行符拆分?

C++模板鸭子类型与纯虚拟基类继承

c++ - 如果没有 sudo,则无法在 Qt 应用程序中使用键盘