How to bind D and C - d

Can someone point me to a good tutorial of how to call C code from D ( and possibly to call a FORTRAN library from D)?
I've researched the topic online and keep finding incomplete examples. I would like someone to spell it out for me one time. For example, show some example bindings and explain the steps to compiling and linking them to work together.
I have both the DMD and GDC compilers on windows.
Some background: Programming isn't my main job, but I do it A LOT at work. I work with Python and Java a lot, my first language was C, and I've done some web programming too. For my next project I'd like to use D, as it seems the learning curve is much lower than C++. Also I want a compiled language, and I've never mixed two languages before.

have a look to Derelict.org, it contains dozen of C bindings using ffi and dynamic-linking (run-time loading and delegate assignation.)

Deimos is a huge repository of bindings and wrappers of C libraries to D. I suggest you take a look at some small binding, and see how people did it.

Related

Compile GNU C project with C++

I just joined a project and there's a bunch of code (which has been written by 4 or 5 different people over the last 5 years). It's all C at this point, but I want to migrate the project to C++ in order to take advantage of OOP.
Step 1 will be to simply leave all the existing code in place and just selectively convert very small pieces of the functionality to be object oriented.
It's my understanding that C can be compiled by C++, but maybe I'm wrong about this.
So, before I invest a whole lot of time in trying to actually do it, am I likely to be able to get the existing C code to compile using C++ relatively easily?
Or, is this likely to involve a massive migration before it will work?
What factors affect the complexity of the task I'm proposing?
So to be clear - I'm not asking how big a project it will be to convert all of the code to objects, functions to methods, etc - but rather, just what I'm likely to face getting the existing C code to compile using the C++ compiler instead of the C compiler.
Thanks in advance - I look forward to any help/guidance you can provide.
EDIT
Sounds like it's really not a good idea. I always thought of c++ as an expansion of C, but it sounds like they're different enough that this is not an advisable path. Ultimately, I wasn't thinking of them as Different Languages, and was hoping that c++ was more backwards compatible with c than it sounds like it is. My initial searching made it look like people had done it to varying degrees of success, but the responses here make me think I should really just consider them to be two different languages. Thanks!
I always thought of c++ as an expansion of C, but it sounds like they're different enough that this is not an advisable path.
C++ is not an expansion of C. They are completely different languages. Compiling C code with C++ is not simply enabling "OO-mode". When you compile anything but trivial C code with a C++ compiler, you are potentially opening yourself up to a world of hurt due to the differences between the two languages.
am I likely to be able to get the existing C code to compile using C++ relatively easily?
Again, you have a fundamental misunderstanding -- C++ is not some extended version of C. They are different languages.
The best course of action is to slowly migrate your code to C++, but using two different compilers. Compile the C code with a C compiler, and compile the C++ code with a C++ compiler. It is not a challenging feat since any modern compiler suite (like GCC and clang) can compile both C and C++ very well.
is this likely to involve a massive migration before it will work?
If you intend to convert your code to C++, then yes.
One thing is worth mentioning: if you are compiling your C code with a C++ compiler, and calling it programming in C++, you're missing the spirit of the C++ development environment. If you're programming in C++, but using C code with very little OO, then what's the point of using C++? Just skip the overhead of OO and code in C. Using a C++ compiler is not some magic process that will instantly make your code better.
I think you should have a look here to get a list of (possible) issues.
What I did in the past for a C to C++ mgration was to leave the C code working as it should (C code compiled with a C compiler) and - in parallel - rewrite the code from scratch in C++.

Any high-level languages that can use c libraries?

I know this question could be in vain, but it's just out of curiosity, and I'm still much a newb^^ Anyways I've been loving python for some time while learning it. My problem is obviously speed issues. I'd like to get into indie game creation, and for the short future, 2d and pygame will work.
But I'd eventually like to branch into the 3d area, and python is really too slow to make anything 3d and professional. So I'm wondering if there has ever been work to create a high-level language able to import and use c libraries? I've looked at Genie and it seems to be able to use certain libraries, but I'm not sure to what extent. Will I be able to use it for openGL programing, or in a c game engine?
I do know some lisp and enjoy it a lot, but there aren't a great many libraries out there for it. Which leads to the problem: I can't stand C syntax, but C has libraries galore that I could need! And game engines like irrlicht. Is there any language that can be used in place of C around C?
Thanks so much guys
Python can call functions in dynamically loaded C libraries (.so in unix, .dll in Windows) using the ctypes module.
There is also cython - a variation of python that compiles to C and can call C libraries directly. You can mix modules written in pure Python and cython.
You may also want to look at the numerous 3D game engines either written specifically for Python or with a python interface. The ones I have heard the most about (but not used) are Blender and Python-Ogre.
Panda3D is an engine which uses Python as it's "game logic" interface. You basically write everything in Python and the Panda3D backend (which I assume is mostly written in C or C++) is responsible for rendering.
Check out the gallery of projects that use Panda3D. It's not going to be AAA the-next-Gears-of-War level graphics, but it's still pretty impressive.
Using swig you can make C imports in various languages: lua, python, php, c# ...
See more info here about supported wrappers.
Python is able to use C libraries via the ctypes module. You'll have to write some Python code to import the C functions, but if the C API is clean and simple you'll have no trouble at all.
You might find these useful:
C functions from Python
Integrating Python, C and C++
I have been using PyOpenGL, it works great. Swig does its job if you want to call C/C++ libraries from Python.
I'm surprised that no-one has yet stated clearly that C++ is what you are looking for. Like you I have a distaste for C syntax, but that's a poor reason for avoiding C++ if you want to get into 3D gaming. Do you want to dive into 3D gaming, or do you want to sit on the edge of the pool crying that the water is too cold ?
I think you'll also find that C++ plays very well with OpenGL, which is probably not true of a lot of the alternatives that have already been suggested
To some extent, Cython might be what you are looking for. It allows you to use Python as a high level language, but then use C for the parts that need to be optimized.
But, at the end of the day, if you want to do 3D, just learning C or C++ may be the way to go. :-)
There are Python wrappers available for major open source game engines (Ogre, Irrlicht, etc.). Particularly Panda3D ought to have nice bindings.
If you'd like to have a look at .Net platform. You have the following solution:
Use C++/CLI to compile your C/C++ code into .Net assembly, the running time of this part would be as the same as your native C/C++ code.
Use any .Net language (C#, F#, IronPython) to develop high-level stuff using the low level library. For pure number crunching, C#/F# is usually 2-4 times slower than native C code, which is still far faster than Python. For non-number crunching tasks, C#/F# could sometimes match the speed of native code.

OpenCV Image Processing -- C++ vs C vs Python

I was thinking of trying OpenCV for a project and noticed that it had C, C++ and Python.
I am trying to figure out whether I should use C++, C or Python -- and would like to use whatever has the best OpenCV support.
Just from looking at the index page for the various documentation it looks like the C++ bindings might have more features than the others? Is this true?
If C++ has more bindings, it seems that would be a more obvious choice for me, but I was just curious if it really has more features, etc than the others?
Thanks!
Actually Python binding was finished starting with release 2.2. So i would go with python if i were you, because the performance will be almost the same anyway as you are using c libraries in both cases. However python will get you there faster, safely and you will spend more time on your algorithm rather than getting things to build and run not mentioning memory management hassles
The Python interface is still being developed whereas the C++ interface (especially with the new Mat class) is quite mature. If you're comfortable in C++, I would highly recommend using it - else, you can start using Python and contribute back any features you think OpenCV needs :)
I think it depends how proficient you are in C++. The Mat interface does appear more modern than the old IPLImage C interface. The problem I'm having is that most of the examples you'll find on the web, or even here on stackoverflow are for the C interface (e.g. cvCvtColor), not for the C++ interface. So I'm really struggling to port the C examples to C++.
Even if you're very proficient in C or C++, you should use python to speed up your development (I should guess a 4x factor). Performance are really quite the same.

Call MATLAB APIs in C/C++

I just heard from somewhere that for numerical computation, "MATLAB does offer some user-friendly APIs. If you call these APIs in your C/C++ code, you can speed up computation dramatically."
But I did not find such information in MATLAB documents like http://www.mathworks.com/support/tech-notes/1600/1622.html and http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/bp_kqh7.html. All I learned from these websites is that MATLAB can be called in C and C++ by Matlab engine or by compiling M-files into libraries by mcc. They don't mention any built-in numerical MATLAB APIs that can be called in C/C++.
Can someone please clarify?
Thanks and regards!
You want the "Engine" routines. This allows you to start up a background MATLAB process from C and execute calculations on it: relevant MATLAB documentation.
It works pretty well, have a look at the examples. I would say the most annoying thing getting it working is marshaling the data between C and MATLAB. But that's always a problem when doing this kind of thing.
It sounds like you're looking for the code generation tools in the embedded matlab toolbox or real time workshop.
Do doc eml and look for a the LMS (least mean square) equalizer demo.
The code generator is quite good, it will give you a make file that will build a static library. It's easy to use with your stand alone C/C++ code.
There could be a few things that quote is referencing, I assume that it is referring to the MATLAB Compiler. So going from MATLAB -> C++ you can use the compiler to build standalone "faster" applications. However, when speed testing the improvement, I've noticed it to be negligible. Honestly, you're probably far better off coding your work in C from the get-go, the code that the compiler generates is spaghetti and non-object oriented. I should also mention that this is an expensive extension to Matlab.
You can use the MCR in your own c++ project as a stand-alone library (details)... but you might get similar results using Numerical Recipes.
Disclaimer: I used this product 2-3 years ago, things could be different now.

How can I use a C++ class from Perl?

I have a set of classes written in C++. What would be best way to call them from a Perl script? Thanks.
I'm not particularly fond of SWIG and prefer to write the interfacing code myself. Perl comes with a sort of pseudo language called 'XS' for interfacing to C or C++. Unfortunately, in order to use it, you will need to know at least C, Perl, and then learn something about the interpreter API, too. If you already know Perl and C well, it's not such a big step. Have a look at the following core documents on XS:
perlxstut (XS tutorial)
perlxs (XS reference)
perlapi (Interpreter API)
Additionally, there's plenty of tutorials and how-tos on the internet.
Now, interfacing to C++ using XS requires some additional steps. It can be a bit frustrating to work out at first, but neatly falls into place once you get it. In this regard, the core documentation is sparse at best. But all is not lost. Mattia Barbon, the creator of the wxWidgets bindings for Perl, wrote a great tool "XS++" that makes this almost dead simple (or as simple as XS). It's included in Wx, but we're working on splitting it out into its own distribution. This is work in progress. You can find Mattia's XS++ code and a modified version of mine on github.
Barring a release of a standalone XS++ to CPAN, I would suggest learning to write XS for C++ from other resources:
Quite a long time ago, John Keiser wrote an excellent tutorial on XS and C++. It also includes further pointers to useful tools and documentation.
I learned XS&C++ from that tutorial and some examples I found on CPAN. I don't recall what I looked at then. But now I can point to my own work as a (good or bad, I don't know) example: Math::SymbolicX::FastEvaluator.
Similarly, the planned XS++ distribution contains a complete (albeit pointless) example of using XS++ to interface C++ and Perl. Since XS++ is translated to plain XS, you can use it to generate examples.
PS: There's also the Inline::CPP module. If that works, it is probably the easiest solution. I doubt it can handle templates, though.
Check http://www.swig.org :
"SWIG is a software development tool
that connects programs written in C
and C++ with a variety of high-level
programming languages. SWIG is used
with different types of languages
including common scripting languages
such as Perl, PHP, Python, Tcl and
Ruby."
I would normally choose XS, like tsee, but there is also Inline::C (or Inline::CPP in this case). I dislike SWiG and tend to avoid packages built around it.