Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


A Guide to C++ and C Interoperability

Using C++ code in C apps is difficult and tricky, though not impossible, contrary to popular belief. Find out how to share class declarations between the two languages and how to leverage C apps with high-end C++ features behind your C compiler's back.

orting C code to C++ is no big deal: change the source file's extension from .c to .cpp and recompile. By contrast, porting C++ source code and binaries to C requires more elbow grease and midnight oil. The following sections show how the two languages can share a single declaration of a class with member functions and other C++ goodies. Then you'll see how C apps can utilize state-of-the-art C++ algorithms and containers with a little help from the linker.

You have a class that declares member functions but you want to use it in a C app as well. Additionally, you want to simplify the implementation of certain tasks in your C app by utilizing C++ algorithms and containers. How do you accomplish these goals?

Use conditional compilation to hide C++ features from a C compiler and wrap compiled C++ code in a C-callable function.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date