How to add c++ files

BlitzMax Forums/BlitzMax Programming/How to add c++ files

Twinprogrammer(Posted 2012) [#1]
Hello,

I've been trying to add a c++ header file to my game, but I don't know how. Could you guys show me how to successfully add a c++ header and source file ?


AdamRedwoods(Posted 2012) [#2]
this can be a complicated topic. the best way to learn is to look at brucey's modules.

generally, c++ headers and source are imported via
Import "header.h"
Import "source.cpp"


then, depending on if you are using cpp classes, you need to write "glue" code, which creates a managed blitzmax class that calls the original cpp class.

the bridge between cpp and bmax are variable pointers.

generally, the cpp glue maintains the class constructor, destructor, and pointers to the original class's information. sometimes you need class methods to pass data as well. it can get complicated and lengthy when trying to port a large API.


Twinprogrammer(Posted 2012) [#3]
When I add my header file, I get an error saying a .h is a illegal file type.


Yasha(Posted 2012) [#4]
Not something I've done myself, but I was under the impression that you don't add header files: instead, you import only the .cpp, and put the declarations in the BlitzMax code using "Extern". The BlitzMax compiler has no use for declarations written in a foreign language to it.


AdamRedwoods(Posted 2012) [#5]
if on Windows, make sure mingw is installed. on mac, xcode.


col(Posted 2012) [#6]
Hiya,

From my own experiences...

Don't worry about importing the .h header files into BMax. There's no need, just #include the headers from within the appropriate c++ file that needs the declarations and Import the .cpp file.

If you need the values from the .h inside BMax for whatever reason then you'll need to write them out manually in BMax.
Structs are simple Types and can be passed into and out of the c++ using a Byte Ptr, but you need to make sure that the size of the whole Type in BMax is the same size as the Struct in C. This includes the size of any of structs that the c++ struct uses as a parameter.
Non virtual Classes are different but not difficult and will need wrapping with a pointer to the Class that's passed in to the glue code. There are different ways to wrap them, and can depend on whether the c++ methods are virtual or not as to what method you use.

I've found it easier to just wrap everything in a BMax Type thats will call a C style 'accessing' API of your own which will call the c++ class code ( if ALL of the C++ methods are virtual then you'll only need to write the c++ constructor and destructor in C and use the returned pointer directly as an extern'd BMax Type to access the C++, as in the docs.)