如何像在Autotools中一样检查CMake中的头文件和库功能?

如何像在Autotools中一样检查CMake中的头文件和库功能?

问题描述:

我目前正在将一个小型C项目从自动工具转换为

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

许多自动工具项目都使用AFAIK.

It's what many autotools projects do AFAIK.

尽管事实是编译器已经检查了必要的头文件,而链接程序也检查了库函数,但我的代码仍然需要在configure阶段进行这些检查,以正确设置#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_INCLUDE_FILE 等.另请参见

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.hsetjmp.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 :)