tags:

views:

915

answers:

1

I'm currently converting a small C project (galarm) 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.

But why is it necessary?

The compiler already checks for necessary header files and the linker checks for library functions. Checking for headers and functions is redundant and makes maintaining the project more difficult: One must not forget to add the header check if a new header is added to the code.

There might be a good reason to do it…
In that case, what is the best-practice to check for headers/functions with CMake? CHECK_FUNCTION_EXISTS and CHECK_INCLUDE_FILE?

+1  A: 

It 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 more easy for non programmer to understand that if "check for gtk+ header - failed" they have to install gtk2, rather than having a buch of compile error lines that say the same thing to you but not readable for most of them :)

I do you CHECK_FUNCTION_EXISTS, CHECK_INCLUDE_FILE, CHECK_TYPE_SIZE and some other module (see this link).

claferri