Importing a C++ file

BlitzMax Forums/BlitzMax Programming/Importing a C++ file

Pineapple(Posted 2007) [#1]
I can't figure out why the Output shows this:
Building Console
Compiling:Console.cpp
Build Error: failed to compile C:/Documents and Settings/David/Desktop/Blitz Max/Console.cpp
Process complete

with this Bmax code:


and this being the Dev C++ file:



Why am I getting this error? The C++ compiler does just fine in compiling it. Help please?


grable(Posted 2007) [#2]
You need to wrap any C++ function in an extern "C" block, and also use char* instead of string (blitzmax strings are not compatible with C++ strings).
Next you have to add the functions to an extern block in blitzmax, and again not using String but either a Byte Ptr or $z (CString).

example of extern block in bmx:
Extern "C"
  Function ConsolePrint:Int( text$z)
EndExtern



Brucey(Posted 2007) [#3]
And an example of the extern "C" that you will need to add to the .cpp :
extern "C" {

    bool ConsolePrint (string text)
    {
         cout << text << endl;
         return true;
    } 

}

Otherwise the method signature will be mangled. Ask Google or your favourite search engine about C++ name mangling for more information :-)


Pineapple(Posted 2007) [#4]
Alright, looking at name mangling makes my head hurt. It was a challenge for me just to write that bit of C++ code you see I'm so bad at it. If it weren't for an extremely helpful book I've got it would've never been written.

Anyway, here is my new code and yet I still get the same errors. I download MinGW already and followed Mark's installation instructions but no fix. Again, exact same output results.

Bmax


C++



Dreamora(Posted 2007) [#5]
Although mentioned above: It will not work with C++ string class
You must use char* in the functions that interface with BM
BM does not play nicely with C++ classes


Pineapple(Posted 2007) [#6]
I'm afraid I don't understand. Where am I to use char* ? Will char* not cause the functions to return a single character rather than a string? I'm sorry, C++ is definately not my strong point, I was just hoping to enable some console commands in Bmax.


Azathoth(Posted 2007) [#7]
char * means pointer to character(s)


Pineapple(Posted 2007) [#8]
Alright, I'm confused. Please give me an example or something. I don't understand how I'm supposed to use char * for it to work. Also, do you mean literally "char*" or do you mean "*"?


grable(Posted 2007) [#9]
try this =)
     bool ConsolePrint (char *text)
     {
          cout << text << endl;
          return true;
     }



Pineapple(Posted 2007) [#10]
Now the C++ compiler gives me the error
[linker error] unidentified reference to 'WinMain@16'

Same Bmax output. Same Bmax code.



I suspect I may be missing an #include file?


Gabriel(Posted 2007) [#11]
You're trying to compile it separately with a C++ compiler? Don't do that. BlitzMax will compile it automatically when you import it as you have in the BlitzMax code.


Pineapple(Posted 2007) [#12]
No, I do that to make sure my code is right, the blitzmax output still says

Building Console
Compiling:Console.cpp
Build Error: failed to compile C:/Documents and Settings/David/Desktop/Blitz Max/Console.cpp
Process complete

no matter what I fix


ziggy(Posted 2007) [#13]
Try to build the module blitzapp, if you can't, your C compiler is not properly installed, as part of this module is written in MinGW cpp, and proved to work and compile properly.


Pineapple(Posted 2007) [#14]
Ehh... may I ask how I build blitzapp?


grable(Posted 2007) [#15]
open a console window and type this:
cd Program Files\BlitzMax\bin
bmk makemods brl.appstub

Dunno what blitzapp is, he probably meant appstub ;)

Or just do a "Rebuild All Modules" in the ide.


Pineapple(Posted 2007) [#16]
What console widow?

And does the fact that I have the demo make this not work or something?


Dreamora(Posted 2007) [#17]
Demo does not allow that
the windows demo is that outdated that it would not make any sense at all to try it (its 18+ months old)


Pineapple(Posted 2007) [#18]
Bah! At least I'm going to be getting full version for Cristmas.


Czar Flavius(Posted 2007) [#19]
What console widow?


start > run > cmd


Pineapple(Posted 2007) [#20]
All right, I had tried that but I needed to add C:\ to the beginning of the directory. I did that and it thought for a while but nothing seemed to happen.