I know that Boost libraries are designed for C++, however, I recently stumbled upon boost.predef which is only dependent on C processor, has no C++ specific code and thus works perfectly fine in C code. boost.io lists this module as C++98
, yet I'm able to include and use it with any C99
compiler [see below].
Digging a little into its source also reveals that some work has been done in order to support C compilation (examples: 1, 2, 3, 4) so I'm quite curious how many of those "libraries" would work with C just like boost.predef does.
By similar to boost.predef, I mean the ones that are e.g. C-preprocessor-only or does not require any C++ functionality to compile, or allow part of the module to be used from C without compiling C++ sources.
Are there any more Boost libraries that can be used from C? Is there any way to detect which of those libraries are compatible with C?
Possibly related: Which Boost libraries are header-only?
# proof that boost.predef compiles and works with C
# get the source tree and create minimal compatible program
$ git clone .git
$ echo '#include "boost/predef.h"' > main.c
$ echo 'int main(void) {return 0;}' >> main.c
# compile with very strict C99
$ cc -x c -std=c99 -Wpedantic -Werror -I./predef/include main.c
$ cc --version
cc (GCC) 14.2.1 2024091
# show every available macro
$ cc -x c -std=c99 -Wpedantic -Werror -I./predef/include -dM -E main.c | grep BOOST
I know that Boost libraries are designed for C++, however, I recently stumbled upon boost.predef which is only dependent on C processor, has no C++ specific code and thus works perfectly fine in C code. boost.io lists this module as C++98
, yet I'm able to include and use it with any C99
compiler [see below].
Digging a little into its source also reveals that some work has been done in order to support C compilation (examples: 1, 2, 3, 4) so I'm quite curious how many of those "libraries" would work with C just like boost.predef does.
By similar to boost.predef, I mean the ones that are e.g. C-preprocessor-only or does not require any C++ functionality to compile, or allow part of the module to be used from C without compiling C++ sources.
Are there any more Boost libraries that can be used from C? Is there any way to detect which of those libraries are compatible with C?
Possibly related: Which Boost libraries are header-only?
# proof that boost.predef compiles and works with C
# get the source tree and create minimal compatible program
$ git clone https://github.com/boostorg/predef.git
$ echo '#include "boost/predef.h"' > main.c
$ echo 'int main(void) {return 0;}' >> main.c
# compile with very strict C99
$ cc -x c -std=c99 -Wpedantic -Werror -I./predef/include main.c
$ cc --version
cc (GCC) 14.2.1 2024091
# show every available macro
$ cc -x c -std=c99 -Wpedantic -Werror -I./predef/include -dM -E main.c | grep BOOST
Despite BOOST being targeted at C++ there are few modules/libraries that can work from C. This is possible because these only depend on C preprocessor and have few conditional build paths that make it compatible with C-only compilers. Some of them use *.hpp
headers but this does NOT prevent the usage from C.
The list is as follows:
#ifdef __cplusplus
. boost.predef can be used for detecting host's CPU architecture, compiler, platform, OS and language standard, all at compile time. Here is a good example on how to use it in order to detect Endian Byte Order in C-only code.(..., ..., ...,)
, which might behave a bit differently in C (example).__attribute__
, and swaps them depending on compiler.Note that every single BOOST library lives in it's own repository and many of them are independent of each other. You don't need the whole meta package in order to use them. It is possible to vendor only few as git-submodule.
boost.preprocessor
docks claim C99 support: "The library supports both C++ and C compilation. [...] Since variadic macros are a C99 or C++11 feature the library ostensibly requires at least compilation at those C or C++ standard levels." [ref] I've taken a look at macro expansion of this library and don't see anything C++ specific so it's quite right. Not sure why they're using*.hpp
headers though. – sleeptightAnsiC Commented Feb 4 at 17:20