help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Linking Problem


From: mearvk
Subject: Re: Linking Problem
Date: Thu, 7 Feb 2008 12:51:25 -0800 (PST)
User-agent: G2/1.0

On Feb 7, 2:55 pm, mearvk <mea...@gmail.com> wrote:
> On Feb 7, 1:59 am, Paul Pluzhnikov <ppluzhnikov-...@gmail.com> wrote:
>
>
>
>
>
> > mearvk <mea...@gmail.com> writes:
> > > "g++ -c lip.C" gives me lip.o.
> > > "g++ -c test.C" gives me test.o
> > > "g++ -o out test.o lip.o -lm" should give me 'out' as an executable
> > > but it doesn't, instead giving me:
>
> > > lip.o(.text+0xf88d): In function `zfread(_IO_FILE*, long**)':
> > > : undefined reference to `log(double)'
>
> > The problem is that lip.o references C++ mangled function
> > 'log(double)', but it should be referencing 'extern "C"' function
> > 'log'.
>
> > Here is an example showing the same problem:
>
> >     $ cat t.c
> >     double log(double); /* problem */
> >     int main() { double d = log(100); return 0; }
>
> > When above code is compiled in "C" mode, it works:
>
> >   $ gcc -fno-builtin t.c -lm
>
> > But when compiled as C++, it doesn't:
>
> >     $ g++ -fno-builtin t.c -lm
> >     /tmp/cc0o3mP7.o(.text+0x20): In function `main':
> >     : undefined reference to `log(double)'
> >     collect2: ld returned 1 exit status
>
> > The problem is that 'double log(double);' is correct for C, but it
> > is *not* correct for C++. It should be:
>
> >   #ifdef __cplusplus
> >   extern "C"
> >   #endif
> >   double log(double);
>
> > I am guessing that your 'lip.C' should actually be named 'lip.c',
> > and should be compiled with 'gcc' instead of 'g++'.
>
> > Cheers,
> > --
> > In order to understand recursion you must first understand recursion.
> > Remove /-nsp/ for email.
>
> Does this mean I should be wrapping the log(double) function in the
> math library with
>
> #ifdef __cplusplus
> extern "C" {
> #endif
>
> or that I should be doing this in lip.c's functions that reference the
> math library functions?
>
> Thanks,
>
> Max- Hide quoted text -
>
> - Show quoted text -

For anyone interested I just had to wrap ' #include lip.h ' (in
test.C) in an ' extern "C" {} ' wrapper.

' g++ -c test.C ' gives me test.o
' gcc -c lip.c ' gives me lip.o
' g++ -o out test.o lip.o -lm -lgsl -lgslcblas ' links the two
together without complaining!

Reference: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

<happy dance>

Thanks again!




reply via email to

[Prev in Thread] Current Thread [Next in Thread]