3

I'm working on a project which is written in C, but I would like to use Boost. I have included the Boost libraries in my main file as follows:

#define GNU_SOURCE
#define _GNU_SOURCE
#include <stdlib.h>
#include "initialize.h"
#include <stdio.h>
#include <math.h>
#include <boost/random/linear_congruential.hpp>

I am compiling with gcc -W -Wall -I/usr/local/boost_1_76_0 main.c -o executable -lm -lboost_random.

main.c:8:10: fatal error: iostream: No such file or directory
    8 | #include <iostream>

I guess that the Boost libraries are using <iostream>, but since it's not a C library, I am not sure how to deal with that issue... Should I compile with C++ instead?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pulsar_hh
  • 125
  • 2
  • 9
  • 1
    Use C++ as "C with boost"? – Aykhan Hagverdili May 12 '21 at 13:00
  • 1
    Do you want a generic solution or do you just need an [LCG](https://en.wikipedia.org/wiki/Linear_congruential_generator)? – Ted Lyngmo May 12 '21 at 13:00
  • @AyxanHaqverdili what do you mean exactly? – pulsar_hh May 12 '21 at 13:02
  • @pulsar_hh I added an answer explaining that – Aykhan Hagverdili May 12 '21 at 13:02
  • @TedLyngmo I normally use the GSL, but I wanted to try boost – pulsar_hh May 12 '21 at 13:03
  • 1
    You can wrangle C linkage for a function: **extern "c" {...}** - but you will need to compile the interface code using C++. – Brett Hale May 12 '21 at 13:10
  • @pulsar_hh Ok. FYI: The particular `boost` class you use in your example is actually now part of the standard library in C++ so you don't need `boost` for that. If you only need a few bits and pieces of C++ it's certainly possible (and done) to wrap those up in a nice C API like the answers suggests. It's usually the other way around (C++ classes on top of a C library), but when I create a library aimed for both, I code in C++ and worry about C later, then wrap the necessary parts into a somewhat crippled C library. – Ted Lyngmo May 12 '21 at 15:14

4 Answers4

5

Boost is very much a C++ library, and as such can't be used in a C program.

You'll need to use C++ if you want to use Boost.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dbush
  • 205,898
  • 23
  • 218
  • 273
4

Boost is strictly for C++. Even if bits of it compile with a C compiler, there's no guarantee that will always be the case.

One approach would be to build a dll / so in C++ using Boost, exporting functions using C-style linkage.

Then link to that library using C.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
3

Your C project likely compiles fine with a C++ compiler. Use a C++ compiler and then you have access to Boost and all other C++ libraries. That's the kind of transition C++ was initially designed to handle.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • 2
    There is certainly merit in this approach and is therefore worth looking at. Have an upvote. – Bathsheba May 12 '21 at 13:06
  • I tried but got an error like error: invalid conversion from ‘void*’ to ‘double*’ [-fpermissive] 16 | part->Pinit = malloc(sizeof(*part->Pinit) * 2500); – pulsar_hh May 12 '21 at 13:06
  • @pulsar_hh: Yes you need to cast malloc on the right hand side in C++. (You can do it in C too but it's unnecessary and occasionally harmful.) – Bathsheba May 12 '21 at 13:08
  • @pulsar_hh you need to cast result of `malloc`. That's about the only thing you will have to change fwiw. You can use a C-style cast if you want it to be valid C and C++. – Aykhan Hagverdili May 12 '21 at 13:11
  • @Bathsheba in what ways could it be harmful? If you use proper warnings, I doubt that'll ever mess up anything. – Aykhan Hagverdili May 12 '21 at 13:13
  • @AyxanHaqverdili: Chiefly for the benefit of other readers; see https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc Although perhaps my comment is anachronistic (as am I?). – Bathsheba May 12 '21 at 13:16
  • @Bathsheba I've seen that question, but I don't agree with the top answer since the only tangible argument it has is related to an ancient C standard. I like [this comment](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc#comment69286315_605858) better. FWIW, using a C++ compiler might help catch a couple of bugs since the type system is more picky. – Aykhan Hagverdili May 12 '21 at 13:20
1

Boost can't be used with C as it uses OOP features from C++. It may be technically possible to develop a wrapper for it. Check Developing C wrapper API for Object-Oriented C++ code.

Boost is very C++ oriented, and there isn't any guarantee that even this will work. There's really no point of using Boost in C. Firstly because Boost is mostly for "boosting" OOP (and also for other utility), and secondly because, why not use C++? They are really similar and you can use C standard library with C++ and you can also use procedural programming with C++. So, you better use C++ with C standard library (but I can't give any guarantee that it will work flawlessly) and not use OOP features of C++.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shambhav
  • 813
  • 7
  • 20