gcc compiling problems :extern "C" - Linux

This is a discussion on gcc compiling problems :extern "C" - Linux ; Hi 'body, I'm trying to compile out a lib from a c++ source and using it in C. I tried with sample files but I couldn't compile the C sourcecode... sample.h : #ifdef __cplusplus extern "C" #endif void myfun(); sample.cpp: ...

+ Reply to Thread
Results 1 to 11 of 11

Thread: gcc compiling problems :extern "C"

  1. gcc compiling problems :extern "C"

    Hi 'body, I'm trying to compile out a lib from a c++ source and
    using it in C.
    I tried with sample files but I couldn't compile the C sourcecode...

    sample.h :
    #ifdef __cplusplus
    extern "C"
    #endif
    void myfun();


    sample.cpp:
    #include
    void myfun()
    {
    std::cout<<"Hi there!";
    }


    sample.c:
    #include "sample.h"
    int main()
    {
    myfun();
    }


    In prompt I typed:
    c++ -pedantic -ansi -Wall -lstdc++ -O3 -c sample.cpp
    ar rcs libsample.a sample.o
    gcc -L. -lsample sample.c

    And it told me:
    /tmp/ccG1AP37.o: In function `main':
    sample.c.text+0x12): undefined reference to `myfun'
    collect2: ld returned 1 exit status

    What is wrong???


  2. Re: gcc compiling problems :extern "C"

    frakie writes:
    > sample.h :
    > #ifdef __cplusplus
    > extern "C"
    > #endif
    > void myfun();
    >
    >
    > sample.cpp:
    > #include
    > void myfun()
    > {
    > std::cout<<"Hi there!";
    > }
    >
    >
    > sample.c:
    > #include "sample.h"
    > int main()
    > {
    > myfun();
    > }
    >
    >
    > In prompt I typed:
    > c++ -pedantic -ansi -Wall -lstdc++ -O3 -c sample.cpp
    > ar rcs libsample.a sample.o
    > gcc -L. -lsample sample.c
    >
    > And it told me:
    > /tmp/ccG1AP37.o: In function `main':
    > sample.c.text+0x12): undefined reference to `myfun'
    > collect2: ld returned 1 exit status
    >
    > What is wrong???


    The linker searches object modules (and libraries) strictly
    left-to-right. Try swapping the -lsample and sample.c

  3. Re: gcc compiling problems :extern "C"

    Rainer Weikusat writes:

    > frakie writes:
    > > sample.h :
    > > #ifdef __cplusplus
    > > extern "C"
    > > #endif
    > > void myfun();
    > >
    > >
    > > sample.cpp:
    > > #include
    > > void myfun()
    > > {
    > > std::cout<<"Hi there!";
    > > }
    > >
    > >
    > > sample.c:
    > > #include "sample.h"
    > > int main()
    > > {
    > > myfun();
    > > }
    > >
    > >
    > > In prompt I typed:
    > > c++ -pedantic -ansi -Wall -lstdc++ -O3 -c sample.cpp
    > > ar rcs libsample.a sample.o
    > > gcc -L. -lsample sample.c
    > >
    > > And it told me:
    > > /tmp/ccG1AP37.o: In function `main':
    > > sample.c.text+0x12): undefined reference to `myfun'
    > > collect2: ld returned 1 exit status
    > >
    > > What is wrong???

    >
    > The linker searches object modules (and libraries) strictly
    > left-to-right. Try swapping the -lsample and sample.c


    That is not the only problem. `sample.cpp' doesn't include `sample.h'
    and therefore c++ compiler happily produces fully decorated c++ name.

    sample.cpp:
    #include
    #include "sample.h"
    void myfun()
    {
    std::cout<<"Hi there!";
    }

    And FWIW empty argument list does not have the same meaning in C and
    C++, though it's irrelevant here.

    --
    vale

  4. Re: gcc compiling problems :extern "C"

    Yes, you're right but now I obtain this:

    ../libsample.a(sample.o): In function `function()':
    sample.cpp.text+0x3e): undefined reference to `std::cout'

    :S
    I thing I need something more in cpp compilation prompt line..ARG!

    On 30 Lug, 12:46, Rainer Weikusat wrote:
    > frakie writes:
    > > sample.h :
    > > #ifdef __cplusplus
    > > extern "C"
    > > #endif
    > > void myfun();

    >
    > > sample.cpp:
    > > #include
    > > void myfun()
    > > {
    > > std::cout<<"Hi there!";
    > > }

    >
    > > sample.c:
    > > #include "sample.h"
    > > int main()
    > > {
    > > myfun();
    > > }

    >
    > > In prompt I typed:
    > > c++ -pedantic -ansi -Wall -lstdc++ -O3 -c sample.cpp
    > > ar rcs libsample.a sample.o




    > > gcc -L. -lsample sample.c

    >
    > > And it told me:
    > > /tmp/ccG1AP37.o: In function `main':
    > > sample.c.text+0x12): undefined reference to `myfun'
    > > collect2: ld returned 1 exit status

    >
    > > What is wrong???

    >
    > The linker searches object modules (and libraries) strictly
    > left-to-right. Try swapping the -lsample and sample.c- Nascondi testo tra virgolette -
    >
    > - Mostra testo tra virgolette -




  5. Re: gcc compiling problems :extern "C"

    frakie writes:
    > Yes, you're right but now I obtain this:
    >
    > ./libsample.a(sample.o): In function `function()':
    > sample.cpp.text+0x3e): undefined reference to `std::cout'
    >
    > :S
    > I thing I need something more in cpp compilation prompt line..ARG!


    You need to link with the C++ libraries, too. If you call the compiler
    as 'g++', it will include them automatically. Otherwise, you could try
    translating something with g++ -v and provide the libraries it uses to
    the gcc command by hand.

    NB: I do not really know if it will works this way. The last time I
    used C++ for anything was around 2000.

  6. Re: gcc compiling problems :extern "C"

    frakie wrote:

    > I'm trying to compile out a lib from a c++ source and using it in C.
    > I tried with sample files but I couldn't compile the C source code...


    The C++ FAQ might be helpful.
    http://www.parashift.com/c++-faq-lit...c-and-cpp.html

    I'm not sure (??) it is safe to call C++ code from a C program.

  7. Re: gcc compiling problems :extern "C"

    frakie writes:

    > Yes, you're right but now I obtain this:
    >
    > ./libsample.a(sample.o): In function `function()':
    > sample.cpp.text+0x3e): undefined reference to `std::cout'
    >
    > :S
    > I thing I need something more in cpp compilation prompt line..ARG!
    >


    [..snip..]

    >


    > > > gcc -L. -lsample sample.c


    Should be `g++ -L. sample.c -lsample'

    --
    vale

  8. Re: gcc compiling problems :extern "C"

    At last I found I just had to write down this command line:

    gcc sample.c -L. -lsample -lstdc++

    Thanks to all of you!


  9. Re: gcc compiling problems :extern "C"

    frakie wrote:
    > Hi 'body, I'm trying to compile out a lib from a c++ source and
    > using it in C.
    > I tried with sample files but I couldn't compile the C sourcecode...
    >
    > sample.h :
    > #ifdef __cplusplus
    > extern "C"
    > #endif
    > void myfun();
    >
    >
    > sample.cpp:
    > #include
    > void myfun()
    > {
    > std::cout<<"Hi there!";
    > }


    This code is compiled without the extern "C" declaration in view, so it will
    be strictly C++ linkage. Also note that the stream is not flushed, so you
    might see the output later or even not at all. Although it might not matter
    here, if the output operation threw an exception, you would get undefined
    and probably unpleasant behaviour if this crosses the border from C++ into
    C code.

    > sample.c:
    > #include "sample.h"
    > int main()
    > {
    > myfun();
    > }


    This now expects an extern "C" myfun() somewhere.

    > In prompt I typed:
    > c++ -pedantic -ansi -Wall -lstdc++ -O3 -c sample.cpp

    ^^^^^^^^

    1. When just compiling (-c) you don't have to add any libraries at all.
    2. You should not specify things like '-lstdc++' because the C++
    compiler/linker usually knows better which things are required.

    > ar rcs libsample.a sample.o
    > gcc -L. -lsample sample.c


    You must link C++ libraries with a C++ linker, i.e. g++.

    > And it told me:
    > /tmp/ccG1AP37.o: In function `main':
    > sample.c.text+0x12): undefined reference to `myfun'
    > collect2: ld returned 1 exit status


    This is the result of you not including sample.h in sample.cpp. If you fix
    that, you will have some problems with C++ libs and you can fix that by
    using g++ for linking.

    Uli


  10. Re: gcc compiling problems :extern "C"

    I thinkg the problem is in your sample.h file.
    Try like this:

    sample.h :
    #ifdef __cplusplus
    extern "C" {
    #endif

    void myfun();

    #ifdef __cplusplus
    }
    #endif


  11. Re: gcc compiling problems :extern "C"

    Very exaustive!
    Thank you for the effort!

    On 30 Lug, 17:10, Ulrich Eckhardt wrote:
    > frakie wrote:
    > > Hi 'body, I'm trying to compile out a lib from a c++ source and
    > > using it in C.
    > > I tried with sample files but I couldn't compile the C sourcecode...

    >
    > > sample.h :
    > > #ifdef __cplusplus
    > > extern "C"
    > > #endif
    > > void myfun();

    >
    > > sample.cpp:
    > > #include
    > > void myfun()
    > > {
    > > std::cout<<"Hi there!";
    > > }

    >
    > This code is compiled without the extern "C" declaration in view, so it will
    > be strictly C++ linkage. Also note that the stream is not flushed, so you
    > might see the output later or even not at all. Although it might not matter
    > here, if the output operation threw an exception, you would get undefined
    > and probably unpleasant behaviour if this crosses the border from C++ into
    > C code.
    >
    > > sample.c:
    > > #include "sample.h"
    > > int main()
    > > {
    > > myfun();
    > > }

    >
    > This now expects an extern "C" myfun() somewhere.
    >
    > > In prompt I typed:
    > > c++ -pedantic -ansi -Wall -lstdc++ -O3 -c sample.cpp

    >
    > ^^^^^^^^
    >
    > 1. When just compiling (-c) you don't have to add any libraries at all.
    > 2. You should not specify things like '-lstdc++' because the C++
    > compiler/linker usually knows better which things are required.
    >
    > > ar rcs libsample.a sample.o
    > > gcc -L. -lsample sample.c

    >
    > You must link C++ libraries with a C++ linker, i.e. g++.
    >
    > > And it told me:
    > > /tmp/ccG1AP37.o: In function `main':
    > > sample.c.text+0x12): undefined reference to `myfun'
    > > collect2: ld returned 1 exit status

    >
    > This is the result of you not including sample.h in sample.cpp. If you fix
    > that, you will have some problems with C++ libs and you can fix that by
    > using g++ for linking.
    >
    > Uli- Nascondi testo tra virgolette -
    >
    > - Mostra testo tra virgolette -




+ Reply to Thread