简体   繁体   English

如何在CMake中检查头文件和库函数,就像在Autotools中一样?

[英]How to check header files and library functions in CMake like it is done in Autotools?

I'm currently converting a small C project from autotools to CMake . 我目前正在将一个小型C项目从autotools转换为CMake

In the old configure.in I checked every header and library function for existence using the following lines: 在旧的configure.in我使用以下行检查每个头和库函数是否存在:

# 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

It's what many autotools projects do AFAIK. 这就是许多autotools项目做AFAIK的事情。

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. 尽管编译器已经检查了必要的头文件并且链接器检查了库函数,但我的代码仍然需要在配置阶段完成这些检查,以正确设置#ifdef HAVE_FOOBAR等的编译标志。

In this case, what is the best practice to check for headers/functions with CMake? 在这种情况下,使用CMake检查标头/功能的最佳做法是什么?

You can easily port that directly with CHECK_FUNCTION_EXISTS , CHECK_INCLUDE_FILE , CHECK_TYPE_SIZE , etc. Also see CMake_HowToDoPlatformChecks for some advice. 您可以使用CHECK_FUNCTION_EXISTSCHECK_INCLUDE_FILECHECK_TYPE_SIZE等直接轻松移植它。另请参阅CMake_HowToDoPlatformChecks以获取一些建议。


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 ). 以这种方式配置增加了可移植性(即你可以检查ucontext.hsetjmp.h并使用当前的那个,用#ifdef HAVE_UCONTEXT#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. 此外,当您分发应用程序时,您希望避免编译错误(对于用户),因此使用良好的构建系统,您可以在分发应用程序之前处理大多数体系结构差异。

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 :) 非程序员更容易理解,如果“检查gtk +头 - 失败”,他们必须安装gtk,而不是有一大堆编译错误行说同样的事情,但大多数人都无法读取:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM