How to mix C code in a C++ program

Discussion in 'C++' started by Marko.Cain.23@gmail.com, Mar 19, 2007.

  1. Guest

    Hi,

    i have a c++ program and I want to call a c function (some legacy
    code).
    So I have this in my Producer.c
    int initproducer() {
    printf (" init producer \n");
    }

    And I want to call 'initproducer()' in my File.cpp .

    But when I runs it I get this error:

    ../Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
    asncfsys.so: undefined symbol: _Z12initproducerv

    Thank you for any help.
     
    , Mar 19, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > i have a c++ program and I want to call a c function (some legacy
    > code).
    > So I have this in my Producer.c
    > int initproducer() {
    > printf (" init producer \n");
    > }
    >
    > And I want to call 'initproducer()' in my File.cpp .


    Declare it with 'extern "C" ':

    extern "C" int initproducer();

    in C++

    >
    > But when I runs it I get this error:
    >
    > ./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
    > asncfsys.so: undefined symbol: _Z12initproducerv


    That's because the C++ compiler doesn't know the function comes
    from a C module. Tell it (see above).

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 19, 2007
    #2
    1. Advertising

  3. Guest

    > i have a c++ program and I want to call a c function (some legacy
    > code).


    functions, defined in C, must be declared in C++ as extern "C":

    //some.c
    void f()
    {
    printf("hello, world!\n";
    }

    //main.cpp
    extern "C" void f();

    int main()
    {
    f();
    }
     
    , Mar 19, 2007
    #3
  4. On 2007-03-19 21:27, wrote:
    > Hi,
    >
    > i have a c++ program and I want to call a c function (some legacy
    > code).
    > So I have this in my Producer.c
    > int initproducer() {
    > printf (" init producer \n");
    > }
    >
    > And I want to call 'initproducer()' in my File.cpp .
    >
    > But when I runs it I get this error:
    >
    > ./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
    > asncfsys.so: undefined symbol: _Z12initproducerv


    In the header-file of the c-code you need to sorround the
    function-declarations with this:

    #ifdef __cplusplus
    extern "c" {
    #endif

    /* function declarations */

    #ifdef __cplusplus
    }
    #endif

    This is because C++ mangles the function-names to allow overloading and
    stuff which C does not. This also tells the compiler/linker to use the C
    calling convention when calling these functions.

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Mar 19, 2007
    #4
  5. Guest

    On Mar 19, 3:35 pm, wrote:
    > > i have a c++ program and I want to call a c function (some legacy
    > > code).

    >
    > functions, defined in C, must be declared in C++ as extern "C":
    >
    > //some.c
    > void f()
    > {
    > printf("hello, world!\n";
    >
    > }
    >
    > //main.cpp
    > extern "C" void f();
    >
    > int main()
    > {
    > f();
    >
    > }


    Thanks. I resolve my earlier linker error by adding this in my .cpp
    file:
    extern "C" {
    #include "buffer.h"
    #include "globalerror.h"
    #include "sharedsum.h"
    }

    But now i get a new one:

    ../Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
    asncfsys.so: undefined symbol: seterror

    But in this case, only my c files uses that seterror function (defined
    in globalerror.h"). So why I still get 'undefined symbol: seterror'.

    Thank for your any help.

    $ grep -r seterror *.*
    globalerror.h:int seterror(int error);
    randconsumer.c: seterror(error);
    randconsumer.c: return (seterror(error));
    randproducer.c: seterror(error);
    randproducer.c: return (seterror(error));
    sharedsum.c: return seterror(error);
    sharedsum.c: return seterror(error);
    sharedsum.c: return seterror(error);
    sharedsum.c: return seterror(error);
    sharedsum.c: return seterror(error);
    sharedsum.c: return seterror(error);
     
    , Mar 19, 2007
    #5
  6. Jim Langston Guest

    <> wrote in message
    news:...
    > On Mar 19, 3:35 pm, wrote:
    >> > i have a c++ program and I want to call a c function (some legacy
    >> > code).

    >>
    >> functions, defined in C, must be declared in C++ as extern "C":
    >>
    >> //some.c
    >> void f()
    >> {
    >> printf("hello, world!\n";
    >>
    >> }
    >>
    >> //main.cpp
    >> extern "C" void f();
    >>
    >> int main()
    >> {
    >> f();
    >>
    >> }

    >
    > Thanks. I resolve my earlier linker error by adding this in my .cpp
    > file:
    > extern "C" {
    > #include "buffer.h"
    > #include "globalerror.h"
    > #include "sharedsum.h"
    > }
    >
    > But now i get a new one:
    >
    > ./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
    > asncfsys.so: undefined symbol: seterror
    >
    > But in this case, only my c files uses that seterror function (defined
    > in globalerror.h"). So why I still get 'undefined symbol: seterror'.
    >
    > Thank for your any help.
    >
    > $ grep -r seterror *.*
    > globalerror.h:int seterror(int error);
    > randconsumer.c: seterror(error);
    > randconsumer.c: return (seterror(error));
    > randproducer.c: seterror(error);
    > randproducer.c: return (seterror(error));
    > sharedsum.c: return seterror(error);
    > sharedsum.c: return seterror(error);
    > sharedsum.c: return seterror(error);
    > sharedsum.c: return seterror(error);
    > sharedsum.c: return seterror(error);
    > sharedsum.c: return seterror(error);


    I don't know, that's fairly linux specific (dealing with g++'s linker). If
    no one here responds, try a linux newsgroup.
     
    Jim Langston, Mar 20, 2007
    #6
  7. wrote:
    > Thanks. I resolve my earlier linker error by adding this in my .cpp
    > file:
    > extern "C" {
    > #include "buffer.h"
    > #include "globalerror.h"
    > #include "sharedsum.h"
    > }
    >
    > But now i get a new one:
    >
    > ./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
    > asncfsys.so: undefined symbol: seterror
    >
    > But in this case, only my c files uses that seterror function (defined
    > in globalerror.h"). So why I still get 'undefined symbol: seterror'.
    >
    > Thank for your any help.
    >
    > $ grep -r seterror *.*
    > globalerror.h:int seterror(int error);
    > randconsumer.c: seterror(error);
    > randconsumer.c: return (seterror(error));
    > randproducer.c: seterror(error);
    > randproducer.c: return (seterror(error));
    > sharedsum.c: return seterror(error);
    > sharedsum.c: return seterror(error);
    > sharedsum.c: return seterror(error);
    > sharedsum.c: return seterror(error);
    > sharedsum.c: return seterror(error);
    > sharedsum.c: return seterror(error);


    I'm missing:
    globalerror.c: int seterror(int error)

    Obviously, you forgot to define the function.
     
    Ralph D. Ungermann, Mar 20, 2007
    #7
  8. On Mon, 19 Mar 2007 20:50:35 GMT, =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    <Erik-wikstrom@...com> wrote:
    >On 2007-03-19 21:27, wrote:
    >> i have a c++ program and I want to call a c function (some legacy
    >> code).
    >> But when I runs it I get this error:
    >> ./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
    >> asncfsys.so: undefined symbol: _Z12initproducerv

    >
    >In the header-file of the c-code you need to sorround the
    >function-declarations with this:
    >
    >#ifdef __cplusplus
    >extern "c" {
    >#endif


    Only in the special case when a C library is linked to a C++ program
    and a C header is used. Since C++ is mostly a superset of C, code
    written in C compiles as C++ provided that the incompatibilities
    between the two languages are amended.

    Best regards,
    Roland Pibinger
     
    Roland Pibinger, Mar 20, 2007
    #8
  9. Ian Collins Guest

    Roland Pibinger wrote:
    > On Mon, 19 Mar 2007 20:50:35 GMT, =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    > <Erik-wikstrom@...com> wrote:
    >
    >>On 2007-03-19 21:27, wrote:
    >>
    >>>i have a c++ program and I want to call a c function (some legacy
    >>>code).
    >>>But when I runs it I get this error:
    >>>./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
    >>>asncfsys.so: undefined symbol: _Z12initproducerv

    >>
    >>In the header-file of the c-code you need to sorround the
    >>function-declarations with this:
    >>
    >>#ifdef __cplusplus
    >>extern "c" {
    >>#endif

    >
    >
    > Only in the special case when a C library is linked to a C++ program
    > and a C header is used.
    >

    Well that special case covers just about every C library!

    > Since C++ is mostly a superset of C, code
    > written in C compiles as C++ provided that the incompatibilities
    > between the two languages are amended.


    But C libraries generally get compiled as C, with a C compiler.

    --
    Ian Collins.
     
    Ian Collins, Mar 20, 2007
    #9
  10. (Roland Pibinger) writes:

    > On Mon, 19 Mar 2007 20:50:35 GMT, =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    > <Erik-wikstrom@...com> wrote:
    >>On 2007-03-19 21:27, wrote:
    >>> i have a c++ program and I want to call a c function (some legacy
    >>> code).
    >>> But when I runs it I get this error:
    >>> ./Bin/hlxserver: symbol lookup error: /home/cain/install/Plugins/
    >>> asncfsys.so: undefined symbol: _Z12initproducerv

    >>
    >>In the header-file of the c-code you need to sorround the
    >>function-declarations with this:
    >>
    >>#ifdef __cplusplus
    >>extern "c" {
    >>#endif

    >
    > Only in the special case when a C library is linked to a C++ program
    > and a C header is used.


    Given that the error given above is almost certainly the result of not
    doing so in just such a case... what's your point?

    sherm--

    --
    Web Hosting by West Virginians, for West Virginians: http://wv-www.net
    Cocoa programming in Perl: http://camelbones.sourceforge.net
     
    Sherm Pendley, Mar 20, 2007
    #10
  11. david.baird Guest

    On Mar 19, 2:50 pm, Erik Wikström <> wrote:
    > In the header-file of the c-code you need to sorround the
    > function-declarations with this:
    >
    > #ifdef __cplusplus
    > extern "c" {
    > #endif
    >
    > /* function declarations */
    >
    > #ifdef __cplusplus}
    >
    > #endif
    >
    > This is because C++ mangles the function-names to allow overloading and
    > stuff which C does not. This also tells the compiler/linker to use the C
    > calling convention when calling these functions.


    I agree. This is the answer.

    All header files containing C functions should be written this way.
     
    david.baird, Mar 21, 2007
    #11
  12. On Wed, 21 Mar 2007 09:35:20 +1200, Ian Collins wrote:
    >Roland Pibinger wrote:
    >> Only in the special case when a C library is linked to a C++ program
    >> and a C header is used.
    >>

    >Well that special case covers just about every C library!


    The original question was:
    On 19 Mar 2007 13:27:24 -0700, Marko.Cain.23@...com wrote:
    >>>i have a c++ program and I want to call a c function (some legacy
    >>>code).
    >>>So I have this in my Producer.c
    >>>int initproducer() {
    >>> printf (" init producer \n");
    >>>}
    >>>
    >>>And I want to call 'initproducer()' in my File.cpp .


    You don't need extern "C" to compile this source code with a C++
    compiler.

    Best wishes,
    Roland Pibinger
     
    Roland Pibinger, Mar 21, 2007
    #12
  13. Jorgen Grahn Guest

    On Tue, 20 Mar 2007 18:14:01 -0400, Sherm Pendley <> wrote:
    > (Roland Pibinger) writes:
    >
    >> On Mon, 19 Mar 2007 20:50:35 GMT, =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    >> <Erik-wikstrom@...com> wrote:

    ....
    >>>In the header-file of the c-code you need to sorround the
    >>>function-declarations with this:
    >>>
    >>>#ifdef __cplusplus
    >>>extern "c" {
    >>>#endif

    >>
    >> Only in the special case when a C library is linked to a C++ program
    >> and a C header is used.

    >
    > Given that the error given above is almost certainly the result of not
    > doing so in just such a case... what's your point?


    I assume his point is that the first, obvious solution to the question
    in the subject line ("How to mix C code in a C++ program") is "compile
    it with a C++ compiler".

    Linking to a C library is usually not referred to as "mixing C code",
    and some people regularly borrow code at the source level -- so IMHO
    it makes sense to mention this case too.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
    \X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!
     
    Jorgen Grahn, Mar 26, 2007
    #13
    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. UJ
    Replies:
    1
    Views:
    401
    Nick Hodge
    Apr 17, 2006
  2. Toondalis

    Mix C++ DLL with C main program

    Toondalis, Nov 14, 2003, in forum: C Programming
    Replies:
    1
    Views:
    328
    Mark A. Odell
    Nov 14, 2003
  3. dolphin
    Replies:
    45
    Views:
    922
    John Bode
    Apr 3, 2007
  4. Aussie Rules

    Mix the ASP.net code behind and the HTML

    Aussie Rules, Jun 4, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    656
    Steven Cheng[MSFT]
    Jun 5, 2007
  5. Jean Dubois
    Replies:
    6
    Views:
    115
    Jean Dubois
    Dec 19, 2013
Loading...

Share This Page