Jump to content

Mixing C and C++


xtal256

Recommended Posts

I just downloaded the allegro library for game development and it is in C. I want to make a game in C++ since it will be a lot easier to create classes and stuff. Allegro came with some MSVC stuff, which is good 'cos i plan to use Visual Studio. I even downloaded a C++ wrapper class for Allegro, but i also got an OpenGL extension for Allegro which is in C.

What i want to know is can i use the C code with C++ (in Visual Studio)? I know that C will compile with a C++ compiler but certain things can't. i.e. the other day i tried to assign an int to a pointer, like

 

int *addr;

addr = 0x417;

 

but it wouldn't let me in C++. I could do it in C but is it possible in C++?

Link to comment
Share on other sites

It's because of the type checking. All you need is a typecast.

 

int *adr;adr=(int*)0x417;

 

But there is a way to instruct C++ compiler to treat code as if it is C. It is necessary when you are compiling DLLs.

 

//Some C++ code.extern "C"{  //Some C code.}//Some more C++ code.

 

Edit: If you use the C header in C++, you can even do this with an include.

 

extern "C"{#include "c-library.h"}

 

Prior to filing a bug against any of my code, please consider this response to common concerns.

Link to comment
Share on other sites

Ah, i knew there was a way. I have seen the extern C construct before but i forgot about it. I was thinking along the lines of using a compiler flag with microsofts 'cl' compiler.

And casting to a pointer, i completely forgot about casts. thanks

Link to comment
Share on other sites

G twenty-nine

Now that the problem is solved, I shall make a few suggestions for optimal programming/living:

 

1. Drop C++, go with C instead. It is much more efficient in terms of compilation and execution of optimized code and such, plus you get a better understanding of how the program works. Either that or do the important parts in C. I cannot stand it when people write or try to do parsing in C++, it is very unnecessary. The only time I ever really use C++ is for Qt program development (usually current open source applications), and throw in C for the important bits.

 

2. Drop Windows. Seriously, it's a hazard to your programming health. Especially MSVC. Only a few compliers for windows actually conform to C standards, but also the system calls tend to be about 3 times slower than with win32 api calls. That is not OK with me, and shouldn't be with any programmer. If you want to use windows, then fine go ahead and write code that has a 99% chance of not being cross-platform. Otherwise, I'd suggest Linux or FreeBSD/OpenBSD. My personal favorite distribution would have to be archlinux, but go with whatever you like.

 

3. Learn of how to make your own libraries. If you are planning on making a game, this is almost a must. There are really only two kinds: static, which just adds all the objects into the executable (not recommended), and shared, which is loaded at run-time and can be shared among multiple applications (most common).

 

These are just suggestions, I won't rape you if you do not listen tounge.gif

Link to comment
Share on other sites

 

1. Drop C++, go with C instead. It is much more efficient in terms of compilation and execution of optimized code and such

Well i will use the Allegro library which is C, all the graphics code and important stuff is done using allegro. But i want to use C++ classes, it will make things a whole lot easier. I can make a Player object and use inheritance and stuff. I know that C is necessary in order to get the fastest code but i prefer to use an Object Oriented point of view, at least for the higher level stuff.

 

 

2. Drop Windows. Seriously, it's a hazard to your programming health. Especially MSVC.

lol. I only use MSVC because i have it. I don't have Linux and am most familiar with Windows so i'll stick with it. I don't like the sound of 3 times slower system calls. Why would MSVC do that? Surely they would try to make it as fast as possible.

 

 

3. Learn of how to make your own libraries.

I would like to make some of my own DLLs. However, i could probably just make a game in a single .exe, probably no more that a few Mb in size.

 

Thanks for all your suggestions.

Link to comment
Share on other sites

1. Drop C++, go with C instead.

Are you suggesting dropping C++ because of the compiler, or because of OO? You can do OO in C, it just takes a little more work, so I guess if you just don't like compilers, you can do that. But OO is helpful in some places. It's overused by many people, for sure, but if you know how objects work, you can stay efficient with OO.

Prior to filing a bug against any of my code, please consider this response to common concerns.

Link to comment
Share on other sites

  • 1 User Currently Viewing
    0 members, 0 Anonymous, 1 Guest

×
×
  • Create New...

Important Information

By using GTAForums.com, you agree to our Terms of Use and Privacy Policy.