afw-org / afw

Adaptive Framework
https://afw.tools
MIT License
4 stars 1 forks source link

Build errors with cmake 3.26 #88

Closed JeremyGrieshop closed 4 months ago

JeremyGrieshop commented 4 months ago

I discovered this some time ago with latest and greatest docker images for Alpine at the time, but procrastinated in resolving the issue, because of more pressing matters. But, I think more updates for distributions are now including cmake 3.26 and so the build errors need to be addressed:

Running cmake -S . -B build/cmake/
Running cmake --build build/cmake/
In file included from /usr/include/apr-1/apr_general.h:28,
                 from /afw/src/afw/include/afw_common.h:53,
                 from /afw/src/afw/version/afw_version.h:12,
                 from /afw/src/afw/include/afw_minimal.h:29,
                 from /afw/src/afw/include/afw.h:13,
                 from /afw/src/afw/include/afw_internal.h:23,
                 from /afw/src/afw/action/afw_action.c:14:
/usr/include/apr-1/apr.h:632:2: error: #error no decision has been made on APR_PATH_MAX for your platform
  632 | #error no decision has been made on APR_PATH_MAX for your platform
      |  ^~~~~
In file included from /afw/src/afw/include/afw.h:76,
                 from /afw/src/afw/include/afw_internal.h:23,
                 from /afw/src/afw/action/afw_action.c:14:
/afw/src/afw/utf8/afw_utf8.h: In function ‘afw_utf8_z_equal_ignore_case’:
/afw/src/afw/utf8/afw_utf8.h:824:13: error: implicit declaration of function ‘strcasecmp’; did you mean ‘strncmp’? [-Werror=implicit-function-declaration]
  824 |     return (strcasecmp((const char *)s1, (const char *)s2) == 0) ? true : false;
      |             ^~~~~~~~~~
      |             strncmp
cc1: all warnings being treated as errors

When building applications using --std=c11, which we do in order to support atomics, there are many functions that are not included by default, as they may be a part of other standards, such as POSIX. One example of this is the strcasecmp function. A hello world example illustrates this:

#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  printf("PATH_MAX = %d\n", PATH_MAX);

  strcasecmp("abc", "ABC");
}

To compile successfully, we can use cc -o test test.c.

However, if we specify the c11 standard, we get compile errors:

# cc --std=c11 -o test test.c
test.c: In function ‘main’:
test.c:9:29: error: ‘PATH_MAX’ undeclared (first use in this function)
    9 |   printf("PATH_MAX = %d\n", PATH_MAX);
      |                             ^~~~~~~~
test.c:9:29: note: each undeclared identifier is reported only once for each function it appears in
test.c:11:3: warning: implicit declaration of function ‘strcasecmp’; did you mean ‘strncmp’? [-Wimplicit-function-declaration]
   11 |   strcasecmp("abc", "ABC");
      |   ^~~~~~~~~~
      |   strncmp

This can be fixed by declaring the C macro _DEFAULT_SOURCE:

cc --std=c11 -D_DEFAULT_SOURCE -o test test.c

The _DEFAULT_SOURCE is a super macro of sorts, described here: https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html

It sets a few other macros, which allow system include headers to include other headers in order to provide some built-in "features" to be included in your compile.

CMake must have provided us some of these feature macros by default, prior to 3.26, but now no longer does.

JeremyGrieshop commented 4 months ago

Fixed.