Is it possible to embed a c source code in c++

Started by
7 comments, last by Enigma 18 years, 10 months ago
Hi I got a question I am writing a game in c++, I presently found a sound source code but it is in c. Is it possible to embed a c source code in c++ or do I have to rewrite it in c++? Thank you for your time! Jason
Advertisement
You can compile it with a C++ compiler.
Google for extern "C". It's a reserved C++ expression that treats a piece of specified/included code as being pure C instead of C++ code. That way, the compiler wont bug you with warnings.
Quote:Original post by evolutional
You can compile it with a C++ compiler.


void main(){int a,b,c;a=b+c;}


That will give an error on the MinGW compiler if you call it a .CPP file.
Quote:Original post by Thevenin
void main(){int a,b,c;a=b+c;}

That will give an error on the MinGW compiler if you call it a .CPP file.

That should give an error on any compliant C or C++ compiler. main must have a return type of int (although a return statement is not required).

There are a few differences between C and C++ (variadic macros among others), but for the most part C++ is a superset of C.

Enigma
Quote:Original post by Thevenin
Quote:Original post by evolutional
You can compile it with a C++ compiler.


*** Source Snippet Removed ***

That will give an error on the MinGW compiler if you call it a .CPP file.


Is it supposed to compile on a C compiler?!?!? In C, the precedence of = is greater than the precedence of +?

Weird...

Quote:Original post by Enigma
Quote:That will give an error on the MinGW compiler if you call it a .CPP file.

That should give an error on any compliant C or C++ compiler. main must have a return type of int (although a return statement is not required).

Not in C. C describe main() as the default entry point for a program. It also defines the possible parameters - which are optional - but AFAIK it doesn't enforce the return type of main().
And when it comes to C++: most compilers allow you to create an ill-formed main function. They should not do, of course. But they also should support partial specialization :).

Regards,
Quote:Original post by Enigma
Quote:Original post by Thevenin
void main()
{
int a,b,c;
a=b+c;
}
That will give an error on the MinGW compiler if you call it a .CPP file.

That should give an error on any compliant C or C++ compiler. main must have a return type of int (although a return statement is not required).

There are a few differences between C and C++ (variadic macros among others), but for the most part C++ is a superset of C.

Enigma

Not in C89, in C99 it had to have a return statement but not in C89.

Quote:Original post by Enigma
That should give an error on any compliant C or C++ compiler. main must have a return type of int (although a return statement is not required).

I've never understood that. If you aren't going to require a return statement, why not take the logical next step and allow void returns all together? Is there some reason they couldn't just say that
void main(){   //whatever}

is functionally equivilent to
int main(){   //whatever   return 0;}

?

CM
Quote:C89 draft standard:
2.1.2.2 Hosted environment

A hosted environment need not be provided, but shall conform to the following specifications if present.

"Program startup"

The function called at program startup is named main . The implementation declares no prototype for this function. It can be defined with no parameters:

int main(void) { /*...*/ }

or with two parameters (referred to here as argc and argv , though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /*...*/ }

If they are defined, the parameters to the main function shall obey the following constraints:

* The value of argc shall be nonnegative.

* argv[argc] shall be a null pointer.

* If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both upper-case and lower-case, the implementation shall ensure that the strings are received in lower-case.

* If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name ;argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters .

* The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

Quote:C99 draft standard:
5.1.2.2.1 Program startup

#1

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;8) or in some other implementation-defined manner.

#2

If they are declared, the parameters to the main function shall obey the following constraints:

-- The value of argc shall be nonnegative.

-- argv[argc] shall be a null pointer.

-- If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to

the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.

-- If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

-- The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

I don't have the final standards, but I would be very suprised if this was changed in them.

Enigma

This topic is closed to new replies.

Advertisement