Invoking c function in a c plus plus function...

Discussion in 'C++' started by Rahul, Mar 24, 2008.

  1. Rahul

    Rahul Guest

    Hi Everyone,

    I want to invoke a c function ( object file created by cc ) from a
    cpp function ( object file is created using g++ ). When i try to link,
    both the object files using g++

    g++ cfile.o cppfile.o

    i get an error saying,

    : undefined reference to `fun()'
    collect2: ld returned 1 exit status

    Note that the fun() is the c function defined in cfile.c. I can't use
    cc to link both the object files as the master file is a cpp source
    file.

    Is there anyway to get this done? I know the method of using extern
    "C" but that works only when a cpp function ( object file created in g+
    + ) is to be invoked from a c function ( object file created in cc ).

    Thanks in advance ! ! !
    Rahul, Mar 24, 2008
    #1
    1. Advertising

  2. * Rahul:
    >
    > I want to invoke a c function ( object file created by cc ) from a
    > cpp function ( object file is created using g++ ). When i try to link,
    > both the object files using g++
    >
    > g++ cfile.o cppfile.o
    >
    > i get an error saying,
    >
    > : undefined reference to `fun()'
    > collect2: ld returned 1 exit status
    >
    > Note that the fun() is the c function defined in cfile.c. I can't use
    > cc to link both the object files as the master file is a cpp source
    > file.
    >
    > Is there anyway to get this done? I know the method of using extern
    > "C" but that works only when a cpp function ( object file created in g+
    > + ) is to be invoked from a c function ( object file created in cc ).


    Well that is incorrect.


    > Thanks in advance ! ! !


    This is a FAQ.

    See the FAQ.


    Cheers, & hth.,

    - Alf

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 24, 2008
    #2
    1. Advertising

  3. Rahul wrote:
    > Hi Everyone,
    >
    > I want to invoke a c function ( object file created by cc ) from a
    > cpp function ( object file is created using g++ ). When i try to link,
    > both the object files using g++
    >
    > g++ cfile.o cppfile.o
    >
    > i get an error saying,
    >
    > : undefined reference to `fun()'
    > collect2: ld returned 1 exit status
    >
    > Note that the fun() is the c function defined in cfile.c. I can't use
    > cc to link both the object files as the master file is a cpp source
    > file.
    >
    > Is there anyway to get this done? I know the method of using extern
    > "C" but that works only when a cpp function ( object file created in g+
    > + ) is to be invoked from a c function ( object file created in cc ).


    I believe that's exactly what extern "C" is for. Show us the
    chunk-o-code that exhibits this problem.
    Gianni Mariani, Mar 24, 2008
    #3
  4. Rahul

    Rahul Guest

    On Mar 24, 4:39 pm, "Alf P. Steinbach" <> wrote:
    > * Rahul:
    >
    >
    >
    >
    >
    > > I want to invoke a c function ( object file created by cc ) from a
    > > cpp function ( object file is created using g++ ). When i try to link,
    > > both the object files using g++

    >
    > > g++ cfile.o cppfile.o

    >
    > > i get an error saying,

    >
    > > : undefined reference to `fun()'
    > > collect2: ld returned 1 exit status

    >
    > > Note that the fun() is the c function defined in cfile.c. I can't use
    > > cc to link both the object files as the master file is a cpp source
    > > file.

    >
    > > Is there anyway to get this done? I know the method of using extern
    > > "C" but that works only when a cpp function ( object file created in g+
    > > + ) is to be invoked from a c function ( object file created in cc ).

    >
    > Well that is incorrect.
    >
    > > Thanks in advance ! ! !

    >
    > This is a FAQ.
    >
    > See the FAQ.
    >
    > Cheers, & hth.,
    >
    > - Alf
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?


    I know that it is incorrect, i was looking for a work around...
    Rahul, Mar 24, 2008
    #4
  5. Rahul

    Rahul Guest

    On Mar 24, 4:47 pm, Gianni Mariani <> wrote:
    > Rahul wrote:
    > > Hi Everyone,

    >
    > > I want to invoke a c function ( object file created by cc ) from a
    > > cpp function ( object file is created using g++ ). When i try to link,
    > > both the object files using g++

    >
    > > g++ cfile.o cppfile.o

    >
    > > i get an error saying,

    >
    > > : undefined reference to `fun()'
    > > collect2: ld returned 1 exit status

    >
    > > Note that the fun() is the c function defined in cfile.c. I can't use
    > > cc to link both the object files as the master file is a cpp source
    > > file.

    >
    > > Is there anyway to get this done? I know the method of using extern
    > > "C" but that works only when a cpp function ( object file created in g+
    > > + ) is to be invoked from a c function ( object file created in cc ).

    >
    > I believe that's exactly what extern "C" is for. Show us the
    > chunk-o-code that exhibits this problem.


    cfile.c

    int fun()
    {
    return (0);
    }

    cppfile.cpp

    #include <cstdio>

    extern int fun();

    int main()
    {
    int a = fun();
    printf("integer is %d\n",a);
    }

    extern "C" just avoids name mangling. Are you suggesting to have it
    around the function call in main() of cppfile.cpp?
    Rahul, Mar 24, 2008
    #5
  6. * Rahul:
    > On Mar 24, 4:47 pm, Gianni Mariani <> wrote:
    >> Rahul wrote:
    >>> Hi Everyone,
    >>> I want to invoke a c function ( object file created by cc ) from a
    >>> cpp function ( object file is created using g++ ). When i try to link,
    >>> both the object files using g++
    >>> g++ cfile.o cppfile.o
    >>> i get an error saying,
    >>> : undefined reference to `fun()'
    >>> collect2: ld returned 1 exit status
    >>> Note that the fun() is the c function defined in cfile.c. I can't use
    >>> cc to link both the object files as the master file is a cpp source
    >>> file.
    >>> Is there anyway to get this done? I know the method of using extern
    >>> "C" but that works only when a cpp function ( object file created in g+
    >>> + ) is to be invoked from a c function ( object file created in cc ).

    >> I believe that's exactly what extern "C" is for. Show us the
    >> chunk-o-code that exhibits this problem.

    >
    > cfile.c
    >
    > int fun()
    > {
    > return (0);
    > }
    >
    > cppfile.cpp
    >
    > #include <cstdio>
    >
    > extern int fun();
    >
    > int main()
    > {
    > int a = fun();
    > printf("integer is %d\n",a);
    > }
    >
    > extern "C" just avoids name mangling.


    That is, again, incorrect.

    Last time you responded that you knew your statement was incorrect.

    Please stop making statements that you "know" are incorrect.


    > Are you suggesting to have it
    > around the function call in main() of cppfile.cpp?


    This is covered by the FAQ.

    Please see that FAQ, instead of having people here use time to spoon-feed you.


    Cheers, & hth.,

    - Alf


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Mar 24, 2008
    #6
  7. Rahul

    Rahul Guest

    On Mar 24, 5:00 pm, "Alf P. Steinbach" <> wrote:
    > * Rahul:
    >
    >
    >
    > > On Mar 24, 4:47 pm, Gianni Mariani <> wrote:
    > >> Rahul wrote:
    > >>> Hi Everyone,
    > >>> I want to invoke a c function ( object file created by cc ) from a
    > >>> cpp function ( object file is created using g++ ). When i try to link,
    > >>> both the object files using g++
    > >>> g++ cfile.o cppfile.o
    > >>> i get an error saying,
    > >>> : undefined reference to `fun()'
    > >>> collect2: ld returned 1 exit status
    > >>> Note that the fun() is the c function defined in cfile.c. I can't use
    > >>> cc to link both the object files as the master file is a cpp source
    > >>> file.
    > >>> Is there anyway to get this done? I know the method of using extern
    > >>> "C" but that works only when a cpp function ( object file created in g+
    > >>> + ) is to be invoked from a c function ( object file created in cc ).
    > >> I believe that's exactly what extern "C" is for. Show us the
    > >> chunk-o-code that exhibits this problem.

    >
    > > cfile.c

    >
    > > int fun()
    > > {
    > > return (0);
    > > }

    >
    > > cppfile.cpp

    >
    > > #include <cstdio>

    >
    > > extern int fun();

    >
    > > int main()
    > > {
    > > int a = fun();
    > > printf("integer is %d\n",a);
    > > }

    >
    > > extern "C" just avoids name mangling.

    >
    > That is, again, incorrect.
    >
    > Last time you responded that you knew your statement was incorrect.
    >
    > Please stop making statements that you "know" are incorrect.
    >
    > > Are you suggesting to have it
    > > around the function call in main() of cppfile.cpp?

    >
    > This is covered by the FAQ.
    >
    > Please see that FAQ, instead of having people here use time to spoon-feed you.
    >
    > Cheers, & hth.,
    >
    > - Alf
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?


    By the way, this is working,

    cppfile.cpp

    #include <cstdio>

    extern "C"
    {
    extern int fun();

    int main()
    {
    int a = fun();
    printf("integer is %d\n",a);
    }
    }

    cc -c cfile.c
    g++ -c cppfile.cpp
    g++ file.o cppfile.o
    Rahul, Mar 24, 2008
    #7
  8. Rahul wrote:
    >
    > By the way, this is working,
    >
    > cppfile.cpp
    >
    > #include <cstdio>
    >
    > extern "C"
    > {
    > extern int fun();
    >
    > int main()
    > {
    > int a = fun();
    > printf("integer is %d\n",a);
    > }
    > }
    >


    'extern "C"' is supposed to be applied to declarations. There's no much point in
    wrapping your entire translation unit into 'extern "C"'

    extern "C" int fun();

    int main()
    {
    int a = fun();
    printf("integer is %d\n",a);
    }

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Mar 24, 2008
    #8
  9. Rahul

    James Kanze Guest

    On Mar 24, 2:45 pm, Andrey Tarasevich <>
    wrote:
    > Rahul wrote:


    > > By the way, this is working,


    > > cppfile.cpp


    > > #include <cstdio>


    > > extern "C"
    > > {
    > > extern int fun();


    > > int main()
    > > {
    > > int a = fun();
    > > printf("integer is %d\n",a);
    > > }
    > > }


    > 'extern "C"' is supposed to be applied to declarations.
    > There's no much point in wrapping your entire translation unit
    > into 'extern "C"'


    It's frequent to wrap a set of declarations (or even an entire
    header file) in `extern "C"'---there are even some types of
    declarations (e.g. typedef's) which can't be done otherwise.

    On the other hand, I'm not too sure that declaring main to be
    `extern "C"' is legal. (If it is, the `extern "C"' is ignored.)
    In the OP's case, applying the `extern "C"' to just the
    declaration is quite appropriate.

    > extern "C" int fun();


    And of course, this really belongs in a separate header file.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Mar 25, 2008
    #9
  10. James Kanze wrote:
    > ...
    >> 'extern "C"' is supposed to be applied to declarations.
    >> There's no much point in wrapping your entire translation unit
    >> into 'extern "C"'

    >
    > It's frequent to wrap a set of declarations (or even an entire
    > header file) in `extern "C"'---there are even some types of
    > declarations (e.g. typedef's) which can't be done otherwise.
    > ...


    Well, the reason I wanted to emphasize that 'extern "C"' is supposed to
    be used with declarations is because in one of the previous messages OP
    appeared to come up with an idea of applying 'extern "C"' to the
    "function call in main".

    I assumed that wrapping the entire body of 'main' into the 'extern "C"'
    in his previous message was an attempt to do exactly that, and that the
    success ("this is working") was incorrectly attributed specifically to
    the actual call being located within 'extern "C"' region.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Mar 25, 2008
    #10
    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. Spartanicus
    Replies:
    3
    Views:
    424
    Spartanicus
    Sep 30, 2005
  2. Manuel
    Replies:
    6
    Views:
    351
    John Isaacks
    Jul 19, 2003
  3. The LoxFather

    C plus plus vs C Sharp

    The LoxFather, Aug 9, 2003, in forum: C++
    Replies:
    23
    Views:
    9,158
  4. The LoxFather

    C plus plus vs C Sharp

    The LoxFather, Aug 9, 2003, in forum: C Programming
    Replies:
    23
    Views:
    1,348
  5. furqan shaikh

    c plus plus code comparator

    furqan shaikh, Nov 7, 2008, in forum: C++
    Replies:
    6
    Views:
    799
    red floyd
    Nov 12, 2008
Loading...

Share This Page