views:

342

answers:

5

So, one commonly heard comment when talking about performance is that you write your code with whatever language gets the job done fastest. If performance in specific areas is a problem, then rewrite those bits in C/C++.

But, what if you're starting with a native C++ app? What options do you have if you want to write the easy bits, or refactor the old bits, in a language like Python, Ruby, C#, or whatever? Keep in mind that transferring data between the native and other sides is a must. Being able to simply call a function written in an "easier" language, while passing C++ classes as data, would be beautiful.

We've got a crusty Win32 app that would benefit greatly if we could crank out new code, or refactor old code, in C# or something. Very little of it requires the complexity of C++, and dealing with the little fiddly bits is dragging down the programming process.

+1  A: 

If you want to work between C++ and Python, than Boost Python is what you're looking for. You can write C Python bindings by hand for Cython, but that limits in many ways how you're going to write your code. This is the easiest way, as seen in some snippets from this tutorial:

A simple function that performs a hello world:

char const* greet()
{
   return "hello, world";
}

The Boost python code needed to expose it to python:

#include <boost/python.hpp>

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

How to use this code from python:

>>> import hello_ext
>>> print hello.greet()
hello, world

Going in the opposite direction is bit tougher, since python doesn't compile to native code. You have to embed the python interpreter into your C++ application, but the work necessary to do that is documented here. This is an example of calling the python interpreter and extracting the result (the python interpreter defines the object class for use in C++):

object result = eval("5 ** 2");
int five_squared = extract<int>(result);
Douglas Mayle
+1  A: 

Well, it really depends on the language. Python interfacing, for instance, is most easily done with Boost Python, and many other languages will require you to interface them as you would with C, using their C library and declaring your callbacks to be extern "C" (unfortunate that you can't use the C++ class definitions in other languages usually).

But I would also ask what you intend to use it for as C++ is a complex language, but once you get familiar with it, it is very powerful and not very much harder to code than other languages. The only really good exception I could think of is if you plan on using a powerful library that exists only in one language and there isn't a decent C++ alternative (graphics libraries are probably the best example of this because you have to be very familiar with them to use them effectively).

It's also worth pointing out that if you interface C++ code to another language, you lose out on the inter-platform compatibility granted by that language.

coppro
A: 

In the .NET world you always have the option of crreating a COM/ActiveX interop layer for your C#/VB.NET assembly.

You can then use the normal COM API from your C++ application to create an instance of this COM server that actually wraps your .NET assembly.

Good thing about this is that simple parameters such as int, bool, string, float etc are mapped to their COM equivalent for you. However to my knowledge it is not possible to easily pass full .NET objects (instances of classes you create).

Also be aware that COM interop calls are relatively slow. You should not be calling a COM interop method continually from your C++ code in a tight loop.

COM/ActiveX have traditionally relied on the Windows Registry, not ideal as it is a big dependency. However it is also possible to to use Registration-Free COM interop to avoid this dependency.

This article covers the steps required to register a .NET assembly for COM interop.

Ash
+1  A: 

You can change the common Language run time support in your c++ project to /clr. From this point you can use any .net functionality right in your c++ code. This includes creating winforms in your project as well. You can also add a c# library that handles ui and other functionality.

Aaron Fischer
+1  A: 

As Aaron Fischer suggests, try recompiling your C++ application with the /clr option turned on and then start leveraging the .Net platform.

CLI/C++ is pretty easy to pick up if you know C# and C++ already and it provides the bridge between the .Net world and native C++.

If your current C++ code can't compile cleanly with /clr turned on then I'd suggest trying to build your application as a static lib (without /clr enabled) and then have your main() be in a CLI/C++ project that calls your legacy app entry point. That way you can at least start leveraging .Net for new functionality.

For examples of "legacy" C/C++ apps that have been "ported" to .Net CLI/C++ check out the .Net ports of Quake 2 and Quake 3: Arena.

orj