CMake:如何检查头文件和库函数像Autotools? [英] CMake: How to check header files and library functions like in Autotools?

查看:1133
本文介绍了CMake:如何检查头文件和库函数像Autotools?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在将小型C项目从 autotools 转换为 CMake

I'm currently converting a small C project from autotools to CMake.

在旧的 configure.in 我使用以下行检查每个标题和库函数是否存在:

In the old configure.in I checked every header and library function for existence using the following lines:

# Checks for header files
AC_HEADER_STDC
AC_CHECK_HEADERS([stdlib.h time.h math.h sys/stat.h errno.h unistd.h fcntl.h signal.h])

# Checks for library functions
AC_FUNC_FORK
AC_CHECK_FUNCS([time localtime mktime gmtime exit fork chdir atol signal])
AC_FUNC_STRFTIME

这是许多autotools项目做AFAIK。

It's what many autotools projects do AFAIK.

尽管编译器已经检查必要的头文件和链接器检查库函数,我的代码仍然需要这些检查在配置阶段正确设置它的编译标志为 #ifdef HAVE_FOOBAR 和类似。

Despite the fact that the compiler already checks for necessary header files and the linker checks for library functions, my code still needs these checks done at configure stage to properly setup it's compile flags for #ifdef HAVE_FOOBAR and alike.

在这种情况下,使用CMake检查头/函数的最佳做法是什么?

In this case, what is the best practice to check for headers/functions with CMake?

推荐答案

您可以直接使用 CHECK_FUNCTION_EXISTS CHECK_INCLUDE_FILE CHECK_TYPE_SIZE 等。有关建议,请参阅 CMake_HowToDoPlatformChecks

You can easily port that directly with CHECK_FUNCTION_EXISTS, CHECK_INCLUDE_FILE, CHECK_TYPE_SIZE, etc. Also see CMake_HowToDoPlatformChecks for some advice.

在此样式中配置增加了可移植性(即你可以检查 ucontext.h setjmp.h 并使用现有的,使用 #ifdef HAVE_UCONTEXT #ifdef HAVE_SETJMP )。

Configuring in this style adds portability (ie you can check for ucontext.h and setjmp.h and use the one present, modifying your code with #ifdef HAVE_UCONTEXT or #ifdef HAVE_SETJMP).

此外,当您分发应用程序时,希望避免编译错误(对于用户),因此使用良好的构建系统,分发您的应用程序之前的差异。

Moreover, when you distribute your application, you wish to avoid having compile error (for users) and thus with a good build system, you can handle most of architecture differences before distributing your app.

非程序员更容易理解,如果检查gtk +头 - 失败,他们必须安装gtk,一个编译错误行,说同样的事情,但不可读的大多数:)

It is easier for non-programmer to understand that if "check for gtk+ header - failed", they have to install gtk, rather than having a buch of compile error lines that say the same thing, but not readable for most of them :)

这篇关于CMake:如何检查头文件和库函数像Autotools?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆