Linkage Directives: extern "C"

Discussion in 'C++' started by usr2003, Sep 17, 2003.

  1. usr2003

    usr2003 Guest

    I wrote the following test program to test the linkage directives
    extern "C":



    #include <stdio.h>



    extern "C" {

    void f();

    void callingFWithParm();

    }



    void f()

    {

    printf("Function f()\n");

    }



    void callingFWithParm()

    {

    int i=0;

    f(i);

    }



    int

    main()

    {

    callingFWithParm();

    }



    I compiled with g++ on Red Hat Linux 9. But I got the following error:



    "mix.cc: In function `void callingFWithParm()':

    mix.cc:9: too many arguments to function `void f()'

    mix.cc:16: at this point in file"



    It's as if the linkage directive didn't work. If I take out the linkage
    directive and compile with gcc, it works fine.



    Could anybody explain this behavior? Any input would be greatly
    appreciated.


    --
    Posted via http://dbforums.com
     
    usr2003, Sep 17, 2003
    #1
    1. Advertising

  2. usr2003

    Mike Wahler Guest

    usr2003 <> wrote in message
    news:...
    >
    > I wrote the following test program to test the linkage directives
    > extern "C":
    >
    >
    >
    > #include <stdio.h>
    >
    >
    >
    > extern "C" {
    >
    > void f();
    >
    > void callingFWithParm();
    >
    > }


    Move the above line

    >
    >
    >
    > void f()
    >
    > {
    >
    > printf("Function f()\n");
    >
    > }


    To here.

    -Mike
     
    Mike Wahler, Sep 17, 2003
    #2
    1. Advertising

  3. usr2003 wrote:

    > I wrote the following test program to test the linkage directives
    > extern "C":
    >
    > #include <stdio.h>
    >
    > extern "C" {
    >
    > void f();


    f takes no arguments.

    >
    > void callingFWithParm();
    > }
    >
    > void f()
    > {
    > printf("Function f()\n");
    > }
    >
    > void callingFWithParm()
    > {
    > int i=0;
    > f(i);


    You call f with an argument.

    > }
    >
    > int
    > main()
    > {
    > callingFWithParm();
    > }
    >
    > I compiled with g++ on Red Hat Linux 9. But I got the following error:
    >
    > "mix.cc: In function `void callingFWithParm()':
    >
    > mix.cc:9: too many arguments to function `void f()'


    This is exactly the error I would expect if I were to attempt to call a
    function that takes 0 arguments and pass it 1 argument.

    >
    > mix.cc:16: at this point in file"
    >
    > It's as if the linkage directive didn't work. If I take out the linkage
    > directive and compile with gcc, it works fine.


    First, there's nothing here presenting any evidence that the linkage
    directive did or did not work. Second, the error still exists when you
    remove the linkage directive, so unless you've done something else
    differently (such as compiled in C mode, in which case it may compile
    but the behavior is undefined, IIRC) you should get the same error.

    >
    > Could anybody explain this behavior? Any input would be greatly
    > appreciated.


    extern "C" changes linkage only. You can't put C code inside the extern
    "C" block - it's still C++. An empty parameter list still means "no
    arguments", as it always does in C++. Having it inside extern "C" does
    not give it the C semantics.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Sep 17, 2003
    #3
  4. usr2003

    usr2003 Guest

    Thanks Kevin. Your explanation makes sense. I was trying to come up with
    a simple example to see how the linkage directive works. I wanted to set
    up a scenario where the program would only work correctly with the
    extern "C" linkage directive; in another word, it wouldn't work without.
    Obviously, my example didn't do what I intended to do.



    I still don't know when to use the linkage directive. If I compile the c
    code with a C compiler to get the .o file, then I don't need the linkage
    directive (Well, at least in my example).



    Any comment on when linkage directive should be used? Any example?



    Thanks,

    JF


    --
    Posted via http://dbforums.com
     
    usr2003, Sep 19, 2003
    #4
  5. usr2003

    usr2003 Guest

    My bad. It does require the extern "C" linkage directive when I try to
    use the .o compiled by a C compile. Now it all makes sense to me.



    Thanks again!



    Originally posted by usr2003

    > Thanks Kevin. Your explanation makes sense. I was trying to come up
    > with a simple example to see how the linkage directive works. I wanted
    > to set up a scenario where the program would only work correctly with
    > the extern "C" linkage directive; in another word, it wouldn't work
    > without. Obviously, my example didn't do what I intended to do.


    >


    > I still don't know when to use the linkage directive. If I compile the
    > c code with a C compiler to get the .o file, then I don't need the
    > linkage directive (Well, at least in my example).


    >


    > Any comment on when linkage directive should be used? Any example?


    >


    > Thanks,


    JF


    --
    Posted via http://dbforums.com
     
    usr2003, Sep 19, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: validateRequest=&quot;false&quot; not working in web.config or page directive

    Phil Winstanley [Microsoft MVP ASP.NET], May 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    646
    Phil Winstanley [Microsoft MVP ASP.NET]
    May 16, 2004
  2. Frank Schmitt
    Replies:
    0
    Views:
    495
    Frank Schmitt
    Oct 14, 2003
  3. Ahti Legonkov
    Replies:
    0
    Views:
    508
    Ahti Legonkov
    Dec 12, 2003
  4. Steve Kobes

    extern and linkage

    Steve Kobes, Jun 9, 2004, in forum: C Programming
    Replies:
    1
    Views:
    296
    Chris Torek
    Jun 9, 2004
  5. Replies:
    1
    Views:
    593
    Michael DOUBEZ
    Sep 12, 2008
Loading...

Share This Page