C++ importing 101

BlitzMax Forums/BlitzMax Programming/C++ importing 101

Twinprogrammer(Posted 2013) [#1]
Hello,

I was wondering if anyone has a simple tutorial to importing C++ header and source files.


Brucey(Posted 2013) [#2]
There's a "C for Blitzers" tutorial in the Tutorials section. Not sure if it is exactly what you're looking for, but it might set you in the right direction.


Twinprogrammer(Posted 2013) [#3]
If it's the "Interfacing with C" tutorial, I still don't understand. Do you have to rewrite all of the header and source files, or does BlitzMax handle that already?


Yasha(Posted 2013) [#4]
If I understand correctly (no guarantee that I do), BlitzMax doesn't read the source files and doesn't use the headers at all, so as long as they form an appropriate library they shouldn't need to be rewritten. Rather it passes them blind to the compiler where they are treated as normal C or C++ and built into object files.

The part you need to do is make appropriate Extern declarations in your BlitzMax code for the functions (or classes in the case of C++) that you want to use; these Extern blocks are a replacement for header files, in that they provide name declarations that BlitzMax can then use to find and link against the right functions and classes in the compiled C or C++ code.

So:
-- .c/.cpp files get passed straight to GCC
-- BlitzMax only reads .bmx files
-- .bmx files can make reference to the contents of C/++ files using Extern declarations, which identify by name the code it needs to find because it won't find it automatically

Contrast this with a .bmx module which doesn't need Extern declarations because the compiler knows how to read that code anyway, and can just build the relevant links automagically.


Assuming that's actually correct, maybe the process overview makes it clearer how the bits fit together?