c++ - Which Boost libraries are compatible with C? - Stack Overflow

admin2025-04-15  2

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
Share Improve this question edited Feb 11 at 2:10 Dharman 33.5k27 gold badges101 silver badges148 bronze badges asked Feb 4 at 15:00 sleeptightAnsiCsleeptightAnsiC 8217 silver badges19 bronze badges 9
  • 6 No, most Boost libraries are designed specifically for C++ and rely heavily on C++ features such as templates, classes, and exception handling. However, Boost.Predef is one of the very few Boost libraries that can be used in pure C because it consists entirely of preprocessor macros. – R.F. Commented Feb 4 at 15:20
  • 3 It is not designed for "C" – Pepijn Kramer Commented Feb 4 at 15:22
  • 1 I showed that there is a module that can be used from C and asked if there are any more like it. Why do you think there are no more BOOST libraries like this one? If there are not and you have a proof for that, then why don't you just answer this question? I don't see why my question is not considered valid. It clearly proofs my point. I does NOT seek any recommendation. It describes the problem. – sleeptightAnsiC Commented Feb 4 at 15:40
  • 1 Boost.preprocessor probably. – HolyBlackCat Commented Feb 4 at 16:52
  • 3 @HolyBlackCat Thanks. It appears that even 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
 |  Show 4 more comments

1 Answer 1

Reset to default 0

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:

  • boost.preprocessor - According to documentation, this library officially supports C99 and depends only on C preprocessor with variadic macros. This library can be used for complicated macro expansion and meta-programming.
  • boost.predef - This library does not explicitly state that it supports C, however, a lot of work has been done internally in order to support compilers running in C and ObjectiveC only modes. There are no C++specific code paths that wouldn't be guarded with #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.
  • boost.vmd - This is an extension to boost.preprocessor. Although it does not explicitly mention a support for C, its documentation (the very bottom of page) says it only depends boost.preprocessor library. This library is fully written in C preprocessor and does not use any C++specific keywords, however, I've noticed that it wraps comma operators in a lot of places, like (..., ..., ...,), which might behave a bit differently in C (example).
  • boost.config - This is probably the least C-friendly library out of the ones listed here, however, it's still possible to use it from C without any modifications. A lot of macros will still expand to C++specific code so those cannot be used. The best usage for this library is that it wraps compiler specific keywords, such as GCC's __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.

转载请注明原文地址:http://www.anycun.com/QandA/1744711692a86562.html