Results 1 to 4 of 4
I just wrote a test code file in C-Language like this:
Code:
---- test.c ----
#include <stdio.h>
int test(a,b)
int a;
int b;
{
return a+b;
}
int main()
{
...
- 10-30-2008 #1
having problem when compile c file using g++
I just wrote a test code file in C-Language like this:
Code:---- test.c ---- #include <stdio.h> int test(a,b) int a; int b; { return a+b; } int main() { int a = 2; int b = 5; int c = test(a,b); printf("2+5=%d\n",c); return 0; }
and then I compiled and built with gcc, It works quit well!
but when I want to compile with g++, I get following errors:-------------
gcc test.c -o test
./test
2+5=7
It seems that g++ doesn't recognize C style function definition.------------
g++ test.c -o test
test.c:3: error: 'a' was not declared in this scope
test.c:3: error: 'b' was not declared in this scope
test.c:3: error: initializer expression list treated as compound expression
test.c:4: error: expected ',' or ';' before 'int'
test.c:6: error: expected unqualified-id before '{' token
test.c: In function 'int main()':
test.c:14: error: 'test' cannot be used as a function
unfortunately, I have to compile a large ansi-c project with g++, and there are too many c-stlye-defined functions in that project.
Please help!
What should I do to make this through?
- 10-30-2008 #2
help!
I go through gcc/g++ man page, but I still cannot solve this.
did I miss something?
- 10-30-2008 #3
There's an old style to function definitions in C, and a new style.
The old style can be shown in this example:
The new style:Code:int test(a,b) int a; int b; { return a+b; }
The new style was introduced when C became ANSI C. ANSI C still allows the old style; C++ does not.Code:int test(int a,int b) { return a+b; }
When you compile using g++, you're compiling your source code as though it were written in C++. That's ok for new style functions, but it doesn't allow old style functions.
If you wish to use the g++ command, you'll have to change the old-style functions. If you have quite a few of them, and they're all in a consistent format, you may wish to write a Perl script to change them. But check the output carefully. In fact, use the diff command to focus closely on the changes.--
Bill
Old age and treachery will overcome youth and skill.
- 10-30-2008 #4


Reply With Quote