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

无人久伴 提交于 2019-12-19 05:12:38

问题


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

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

It's what many autotools projects do 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.

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


回答1:


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


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.

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



来源:https://stackoverflow.com/questions/647892/how-to-check-header-files-and-library-functions-in-cmake-like-it-is-done-in-auto

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!