I think this would be a common scenario where someone would combine C into C++: http://www.libsdl.org/
And combining code compiled with a C compiler with a C++ compiler... can you do that? Or am I misunderstanding what you're saying?
I think this would be a common scenario where someone would combine C into C++: http://www.libsdl.org/
And combining code compiled with a C compiler with a C++ compiler... can you do that? Or am I misunderstanding what you're saying?
You can, assuming it's the compilers agree on how things should be done when calling functions from one piece of code to the next. This is quite obviously the case if the compiler is produced by the same company. Usually, it also works when the compiler is intended for the same OS, but not always.
If you call C code from a C++ source, make sure the prototypes of C functions are enclosed byA single function can be done like this:Code:extern "C" { ... // functions declared here }
If you do not do that, then the C++ compiler will "decorate" the functions in a way that won't match what the C compiler did [C++ allows several functions with the same name but different parameters, C doesn't. To solve this, the C++ compiler will add "extra stuff" to the function name to make sure that the function that takes one integer is different from the one that take two integers, and that both of those are different from the function that takes a string, etc, etc].Code:extern "C" func(...);
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
C++ includes all the libraries that C89 had, so technically you could safely compile C89 as C++. But you would have to conform to C++'s stringent type system, which requires a cast to and from void* for example, and cannot rely on newer C-only features to do it without warnings or errors.And combining code compiled with a C compiler with a C++ compiler... can you do that? Or am I misunderstanding what you're saying?
Further, good IDEs will let you either compile as C or make a C project, and there are still lone C compilers (such as gcc), so that you should be able to choose the language you want instead of mixing and mashing.
So, yes and no. It depends on what you're doing.
Yes, that is the correct way to do something. You still need to tell the compiler when compiling test2.cpp that the functions in test.c are "C" functions rather than "C++" functions, otherwise the linker would be complaining about not being able to find the functions in test.o, since they are named differently from the ones called by test2.cpp [as I said in the previous post].
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
I tried a couple of tests on Linux with gcc but could not figure out how to do this. You wouldn't happen to know the options to pass to gcc?
As far as I have seen using gcc, you cannot do this... but I don't write in C++. A good example supporting this would be using the same library [runtime] but one is compiled for C and the other is for C++.
If this does not work with gcc/linux does it work on other compilers or systems?