ld: undefined reference to symbol 'log2##GLIBC_2.2.5' - c

Whats wrong with
for (level = 1; level <= log2((double)size); level++)
^
Seems like its from using log2() but whats wrong? I am using it with OpenMPI code actually, but commenting this line fixes things.
Full Source(http://pastie.org/7559178) see line 40
[jiewmeng#JM Assign3]$ mpicc -o cpi cpi.c && mpirun -np 16 cpi
/usr/bin/ld: /tmp/cca9x4he.o: undefined reference to symbol 'log2##GLIBC_2.2.5'
/usr/bin/ld: note: 'log2##GLIBC_2.2.5' is defined in DSO /usr/lib/libm.so.6 so try adding it to the linker command line
/usr/lib/libm.so.6: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
Seems like log2(4) will work but I cant pass in a variable?

In in order to link libm you need to add -lm argument, as this document; MPI under Linux in the Math Department says:
If your code includes mathematical functions (like exp, cos, etc.),
you need to link to the mathematics library libm.so. This is done,
just like for serial compiling, by adding -lm to the end of your
compile command, that is,
mpicc -o sample sample.c -lm

Related

Unable to use exp() with a variable [duplicate]

Whats wrong with
for (level = 1; level <= log2((double)size); level++)
^
Seems like its from using log2() but whats wrong? I am using it with OpenMPI code actually, but commenting this line fixes things.
Full Source(http://pastie.org/7559178) see line 40
[jiewmeng#JM Assign3]$ mpicc -o cpi cpi.c && mpirun -np 16 cpi
/usr/bin/ld: /tmp/cca9x4he.o: undefined reference to symbol 'log2##GLIBC_2.2.5'
/usr/bin/ld: note: 'log2##GLIBC_2.2.5' is defined in DSO /usr/lib/libm.so.6 so try adding it to the linker command line
/usr/lib/libm.so.6: could not read symbols: Invalid operation
collect2: error: ld returned 1 exit status
Seems like log2(4) will work but I cant pass in a variable?
In in order to link libm you need to add -lm argument, as this document; MPI under Linux in the Math Department says:
If your code includes mathematical functions (like exp, cos, etc.),
you need to link to the mathematics library libm.so. This is done,
just like for serial compiling, by adding -lm to the end of your
compile command, that is,
mpicc -o sample sample.c -lm

Problem compiling C progtam from command line gcc with math.h

I am new to the command line and I am trying to run a C program containing the function log10.
If I give
gcc -o -lm randomVamp random\ vampire.c
I get the error
gcc: error: randomVamp: No such file or directory
but randomVamp is the name I wanted to give to the executable, of course it doesn't exist yet.
If I prompt just
gcc -o -lm random\ vampire.c
then I get the error
/usr/bin/ld: /tmp/ccbWivPU.o: in function `main':
random vampire.c:(.text+0x312): undefined reference to `log10'
collect2: error: ld returned 1 exit status
Anyone knows what's going on?
I don't know if it's relevant, but the program also includes stdlib.h and time.h should I use some flag or link them in some way?
-o means that the next argument is the output file name. Replace -o -lm randomVamp with something like -o randomVamp -lm.
Also, note that -l... have no effect if specified before the .c/.cpp/.o/... files. So, the command could look like this:
gcc -o randomVamp random\ vampire.c -lm

Why my shared object does not compile without -fPIC

I wrote a simple shared lib which contains the usage of stdout in stdio.h.
#include <stdio.h>
...
fflush(stdout);
...
There was no compilation issue before I added the fflush(stdout) with the command below
$gcc -shared -o a.so a.c
But after adding the fflush(stdout), compiler complains:
/usr/bin/ld: /tmp/ccK4npwc.o: relocation R_X86_64_PC32 against symbol `stdout##GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
Can someone explain the rationale behind this with as much detail as possible? why does it have to be -fPIC here?
EDIT:
Some comments suggest I read on PIC, but that is missing my question. My question is why do I need PIC for this program. In my program, I also uses puts which is from libc. I can compile it fine without -fPIC. But why is -fPIC required with the stdout variable?

Linker does not want to use libexpat.so (undefined symbol)

I'm trying to compile a program (not written by me) in Kubuntu 12.04 and it fails with the following:
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libwx_baseu_xml-2.8.so: undefined reference to symbol 'XML_SetUserData'
/usr/bin/ld: note: 'XML_SetUserData' is defined in DSO /usr/lib/x86_64-linux-gnu/libexpat.so so try adding it to the linker command line
/usr/lib/x86_64-linux-gnu/libexpat.so: could not read symbols: Invalid operation
collect2: ld returned 1 exit status
I fixed a couple of mistakes in the Makefile, but I still don't understand what's the problem here, as the command line does include -lexpat, and apparently at the correct location:
g++ [...] -L/usr/lib/x86_64-linux-gnu [...] -lwx_baseu_xml-2.8 [...] -lm -lexpat [...]
How could I fix/debug this?
Adding -v -Wl,-v to the flags allowed me to see the command lines for collect2 and ld.
For some reason, the original Makefile was putting the libraries (-L and -l options) before most of the object files. I put the libraries at the end of the command line and now it compiles.

Program compiles on one server, doesn't on another

I'm getting this error when I try and compile my program on my school's external server.
Undefined first referenced
symbol in file
pow /var/tmp//ccWbipvM.o
sqrt /var/tmp//ccWbipvM.o
ld: fatal: Symbol referencing errors. No output written to assign1
collect2: ld returned 1 exit status
The problem is I don't get it when I compile it locally - it runs fine. Can anyone give me some advice as to what the problem is here?? Thanks!
PS: math.h has been included.
Try linking your program with the math library by using the -lm flag:
gcc -o prg -lm prg.c

Resources