0

I have got a header file that includes boost libraries and I need to include this header file in a source code written in C. Is it possible to do this?

Thanks!

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
zuubs
  • 149
  • 4
  • 18
  • 6
    You'll need to implement a wrapper around your C++ library in order to present a C API that you can call from your C code. – Paul R Jul 09 '14 at 11:41
  • 1
    @zuubs You can be pretty sure that Paul R. understood that, he's not wrong. You need to wrap it. – unwind Jul 09 '14 at 12:18

3 Answers3

4

No, you can't generally include C++ headers in a C program. You'll need to separate out the declarations that C programs might use into a separate header, and make sure these have the correct language linkage in C++:

#ifdef __cplusplus
extern "C" {
#endif

// C-compatible declarations here

#ifdef __cplusplus
}
#endif
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • How can I wrap inclusions such as #include ? – zuubs Jul 09 '14 at 11:47
  • @zubs: Define incomplete types for the C++ classes (Concrete classes of compatible layout for advanced uses), and declare a forwarder in the header for each C++ method you need to call. Those need to be implemented in C++ of course. – Deduplicator Jul 09 '14 at 12:06
  • 1
    @zuubs: Just follow the duplicate link. – Deduplicator Jul 09 '14 at 12:19
  • 1
    @zuubs: You don't. You design a C-compatible API for your library, and declare that in the C header. Only include Boost and other C++-only headers from C++ source files. – Mike Seymour Jul 09 '14 at 12:39
0

Use adapter pattern (http://en.wikipedia.org/wiki/Adapter_pattern) this post already discusses simmilar issue: Using C++ library in C code)

Community
  • 1
  • 1
0

You can not use Boost library in C.

But You can find many altternative of it like GLIB and APR.

Jayesh Bhoi
  • 24,694
  • 15
  • 58
  • 73
  • I think your URL for boost is broken. I need boost for Cassandra's C++ driver https://github.com/datastax/cpp-driver and there is no other way to use something else. – zuubs Jul 09 '14 at 12:00
  • @zubs Check again updated it. Also boost is a collection of `C++` libraries that help you avoid writing custom code to solve common problems.And you can't modified it. – Jayesh Bhoi Jul 09 '14 at 12:05