| Home | Forums | What's new | Resources | |
| C++ Class Declaration on SS |
| shinhoshi - Oct 17, 2003 |
| shinhoshi | Oct 18, 2003 | ||
| Well, it wasn't as easy as I expected... I did a litle research and it seems it isn't so trivial to make C++ classes compile at the same time you use the SGL. | |||
| slinga | Oct 18, 2003 | ||
| Another option would be to use C structs. If I recall correctly, you can have functions inside the class as well (or was that only in C++ :huh ). | |||
| shinhoshi | Oct 18, 2003 | ||
| The fact is that I simply included Object3D.h from a file that already compiled succesfully. The problem I faced was that the SH compiler (last version from renesas) ignored the C++ class declaration. Then I realised that in GCC when you want to compile C++ code you have to use G++. I changed that in the makefile but then the problem was that the file that already compiled couldn't find the SGL functions that it was using. Curious... I have no problem using C structs instead of C++ classes. I only wanted to use C++ classes because it's easier to understand and organize your code. | |||
| antime | Oct 18, 2003 | ||
| Did you forget the 'extern "C"'? | |||
| shinhoshi | Oct 18, 2003 | ||
| the 'extern "C"' ? | |||
| antime | Oct 18, 2003 | ||||||
| In a C++ source file, if you want to use modules written in C you must declare the functions as C functions using 'extern "C"'. You can do it for a single function like so: Code:
The second way lets you include complete C headers: Code:
Variables can also be declared like that, see a C++ book for more info (eg. Stroustrup, The C++ Programming Language, ยง9.2.4) | |||||||
| shinhoshi | Oct 18, 2003 | ||
| Oh, yes I see (but I think that in the latest versions of C/C++ compilers this wasn't necessary). But it's not that a C++ file includes a C one but just the oposite: a C file that includes a C++ one, and I think that's possible and valid. At least I haven't had any problems when I have done it on PC. Probably it's a problem with the makefile and all the stuff GCC needs. As I have said in other topics I am not a pro with makefiles and all that stuff and I don't want to spend more time on it since it's frustrating and doesn't bring you to anywhere. I will code using classic C. | |||
| antime | Oct 19, 2003 | ||
| You need the extern "C" on any C++ functions called from C functions as well. Some compilers may let you leave it out, but it's not standard C++. EDIT: To be precise, the construct doesn't say that the function is a C function, but that it should be linked as one. This page... should illustrate how to mix the languages in practice. | |||
| shinhoshi | Oct 19, 2003 | ||
| I succeeded in compiling C++ code called from a C file. Well, not really since the thing to compile was only the include line. However, I haven't had any luck in compiling a C++ call from a C++ main. Meaning that all the code is C++. Probably due to the makefile and all the stuff. | |||