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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul

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

    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 ! ! !
  • Gianni Mariani

    #2
    Re: Invoking c function in a c plus plus function...

    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.

    Comment

    • Rahul

      #3
      Re: Invoking c function in a c plus plus function...

      On Mar 24, 4:47 pm, Gianni Mariani <gi4nos...@mari ani.wswrote:
      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?

      Comment

      • Andrey Tarasevich

        #4
        Re: Invoking c function in a c plus plus function...

        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

        Comment

        • James Kanze

          #5
          Re: Invoking c function in a c plus plus function...

          On Mar 24, 2:45 pm, Andrey Tarasevich <andreytarasev. ..@hotmail.com>
          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:james.kan ze@gmail.com
          Conseils en informatique orientée objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

          Comment

          Working...