Find the answer to your Linux question:
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() { ...
  1. #1
    Content Team liio's Avatar
    Join Date
    Oct 2008
    Posts
    5

    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!
    -------------
    gcc test.c -o test
    ./test
    2+5=7
    but when I want to compile with g++, I get following errors:
    ------------
    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
    It seems that g++ doesn't recognize C style function definition.
    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?

  2. #2
    Content Team liio's Avatar
    Join Date
    Oct 2008
    Posts
    5
    help!
    I go through gcc/g++ man page, but I still cannot solve this.
    did I miss something?

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    There's an old style to function definitions in C, and a new style.

    The old style can be shown in this example:
    Code:
    int test(a,b)
    int a;
    int b;
    {
        return a+b;
    }
    The new style:
    Code:
    int test(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.

    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.

  4. #4
    Content Team liio's Avatar
    Join Date
    Oct 2008
    Posts
    5
    yes!
    Perl script will solve my problem.
    thank you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...