1

I'm sorry if this is a basic question(I'm new to c/c++, but I'm a little confused at how to get the answer. stxxl is a c++ library but some of my code is in c. I know c++ can use c code(my c code is embedded in c++), but does it work the other way around so c can run c++ code?

Their site only mentions c++ but I'm wondering if there's something special that can be done to run c++ libraries within c?

Sorry the books I have read talk about using c code in c++ and the c book I read was written before c++ came out. Right now my c function is sending data to my c++ code which is using the c++ library and then sending results back so I'm thinking I want to test performance if I cut the middle man(c++).

Lostsoul
  • 25,013
  • 48
  • 144
  • 239
  • 1
    possible duplicate of [Using C++ library in C code](http://stackoverflow.com/questions/199418/using-c-library-in-c-code) – Stuart Golodetz May 18 '12 at 00:36
  • That book is what... two, three decades outdated now? – K-ballo May 18 '12 at 00:38
  • 1
    @K-ballo: K&R Second Edition is quite old but still the best book for learning C... age isn't everything. – sarnold May 18 '12 at 00:39
  • @sarnold: After C++ came out, both C and C++ standards have evolved quite a bit... Remember the weird function syntax in K&R C? Aren't those outdated by now? – K-ballo May 18 '12 at 00:40
  • 1
    @K-ballo: That's why I said _Second Edition_, to get the new function syntax. :) And yes, the standards are the best place to answer complicated questions, but as a learning guide, K&R II is far and away one of the best books ever written on any subject. :) – sarnold May 18 '12 at 00:41

2 Answers2

1

Here's a links that may help you: How to mix C and C++

bigdaveyl
  • 1,391
  • 2
  • 9
  • 17
  • 1
    Welcome to [SO] -- please note when giving an answer that consists of just an URL, it'll often be converted to a comment instead. Please consider summarizing the content of the page, so it'll still be useful even if the page goes dark. Thanks! – sarnold May 18 '12 at 00:40
1

You can link to a C++ library from C only when the C++ library has been designed to be used from C. Specifically, the functions the library provides need to be exported with extern "C" {} block to avoid name mangling, and the interface should be designed in a way to be usable from plain C (i.e. no classes or member functions, only functionless structs and plain functions).

It is worth mentioning that you can compile your C code with a C++ compiler, and it will for the most part be OK. This lets you pretend that your C code is a C++ code, and freely mix in functionality provided through C++ - specific interfaces.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523