program links properly on school machine, but not on personal one: undefined reference to log - c

I can ssh to my schools Ubuntu 11.04 server where I have to submit my code and this links fine:
ME#SCHOOL:~/309/project2$ make
gcc -lm treesort.c -o treesort
ME#SCHOOL:~/309/project2$
My local machine is running Ubuntu 12.04 and here is the compiler output for the same code, but using the NetBeans makefile:
gcc -lm -o dist/Release/GNU-Linux-x86/treesort build/Release/GNU-Linux-x86/sortFns.o build/Release/GNU-Linux-x86/treesort.o
build/Release/GNU-Linux-x86/treesort.o: In function `processargs':
treesort.c:(.text+0x144): undefined reference to `log'
collect2: ld returned 1 exit status
Using the command line on my local machine with the same code and makefile from the first example:
ME#MYCOMPUTER:~/Documents/CSCI/309/project2$ make
gcc -lm treesort.c -o treesort
/tmp/ccY5GqF1.o: In function `processargs':
treesort.c:(.text+0x2b5): undefined reference to `log'
collect2: ld returned 1 exit status
make: *** [all] Error 1
ME#MYCOMPUTER:~/Documents/CSCI/309/project2$
This is really quite irritating, and I can't figure out why it works on the server and not on my local machine. I found this question, but it didn't help.

Put -lm at the end of the link line. The order of sources,objects and (archive) libraries on the link line matters, and yours is wrong.
If putting -lm at the end of the link line doesn't help, you have some bogus library called libm.{a,so} somewhere on your system, and you should figure out where it came from, and get rid of it.
You cam find out which libm your linker is finding with gcc -Wl,-t ...

Related

Linking fftw-3.3.6-pl2 while compiling my file using gcc

I'm trying to run a simple code that includes the fftw library. I know the code is right as it is provided as a test code by the authors. This is what I typed during compilation:
gcc my file.c -L/home/ankit/Desktop/fftw-3.3.6-pl2/lib/
-I/home/ankit/Desktop/fftw-3.3.6-pl2/include/ -lfftw -lm
I get the errors:
myfile.c: (.Text+0x2c):. undefined reference to 'fftw_plan_dft_2d'
collect2: ld returned 1 exit status
It wasn't a linking problem, installation was faulty and I had to use 'sudo make install' to get the permission for the installation to be successful. I could link it with 'gcc test.c -lfftw3 -lm' after. Thanks for your suggestions!

undefined reference to `sqlcxt'

Trying to compile some C code (a simple select * from dual with oracle (11g) db connection on Linux, generated from .pc thru proc command) through the gcc command and I always get an error.
undefined reference to 'sqlcxt'
I tried to link to libraries I saw on different forums around the web, but got nothing (error still occurs):
gcc -o proc_test proc_test.c -L$ORACLE_HOME/lib/libclntsh.so
and I still get
/tmp/ccAW4Imj.o: In function `main':
proc_test.c:(.text+0x3ea): undefined reference to `sqlcxt'
proc_test.c:(.text+0x57c): undefined reference to `sqlcxt'
proc_test.c:(.text+0x60d): undefined reference to `sqlcxt'
collect2: ld returned 1 exit status
Can anyone help with that?
The answer is:
gcc -o proc_test proc_test.c -L$ORACLE_HOME/lib -lclntsh -m32
I just needed to put -m32 cause libs and linux have different bits

DSO missing from command line although it is available

I am working with c++ code for a physics simulation, which uses a lot of external libraries (like GSL and cern`s ROOT). Trying to recompile project I encountered problems with linking. When running compilation of final file via:
g++ -fno-inline -O2 -fpic -o main.out ${ROOTINCS} main.o ext.o ${ROOTLIBS} $(objects2)
with :
objects2= many .o files made by us
ROOTLIBS=-L/usr/local/lib/root -lTree -lRIO -lNet -lHist -lMathCore -lCore -lGraf -lGraf3d -lGpad -lMatrix -lThread -lCint -lPhysics -lPostscript -lRint -lSpectrum -lg
ROOTINCS=-pthread -m64
I get annoying error:
/usr/bin/ld: /usr/local/lib/root/libHist.so: undefined reference to symbol 'gRandom'
/usr/local/lib/root/libMathCore.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
The problem is nm -C run on libMathCore states 'gRandom' is declared there. Also -lMathCore is present in my command line.
When I run ld to check if it understands the flag:
ld -L/usr/local/lib/root -lMathCore --verbose 2>/dev/null
it does not complain and tries to link properly.
According to https://stackoverflow.com/a/24675715/3602168 order of libraries is correct in my linking (libHist uses libMathCOre and therefore is stated first).
Compilation runs under g++ 4.8.2 on ubuntu 14.04, 64 bit
Converting comment to answer:
Have you tried moving $(objects2) before ${ROOTLIBS}? I think the issue may be that you have libraries specified before the object files that use them.

I can't compile this code of mongoose web server's sample example

Short Intro :- (GCC version 4.6.3, OS-Ubuntu 12.04 ,working around mongoose web server program so when I run "make" command to compile and install mongoose , it has done the task fine ).
[Part 1 of question]
This question is in reference to this post on stackowerflow.
mongoose web server helloworld program
Valenok has answered on this post by giving a link to hello sample program.
basically, I am trying to compile the sample hello program code given on this link :-
http://code.google.com/p/mongoose/source/browse/examples/hello.c
and put this code in already compiled directory of mongoose.(directory has mongoose.h file)
Following is command line output for my compilation of hello program.
akshay#akshay-Inspiron-N5010:~$ gcc mongoose/hello.c -o mongoose/hello
/tmp/ccroC5Z6.o: In function `callback':
hello.c:(.text+0x32): undefined reference to `mg_get_request_info'
hello.c:(.text+0x96): undefined reference to `mg_printf'
/tmp/ccroC5Z6.o: In function `main':
hello.c:(.text+0xee): undefined reference to `mg_start'
hello.c:(.text+0x103): undefined reference to `mg_stop'
collect2: ld returned 1 exit status
akshay#akshay-Inspiron-N5010:~$
[Part 2 of question]
Now , I find implementations of mg_stop , mg_start,mg_printf and mg_get_request_info in mongoose.c file , so I compile mongoose.c file with -c option as :
gcc -c -o mongoose.o mongoose.c
I think my question is similar to :-
undefined reference to function declared in *.h file
but then when I link libmongoose.so with -L option on gcc I get following errors:-
(libmongoose.so is present in same directory ,my cwd)
akshay#akshay-Inspiron-N5010:~/mongoose$ gcc -L libmongoose.so -o hello hello.o mongoose.o
mongoose.o: In function `mg_start_thread':
mongoose.c:(.text+0x1369): undefined reference to `pthread_create'
mongoose.o: In function `load_dll':
mongoose.c:(.text+0xa955): undefined reference to `dlopen'
mongoose.c:(.text+0xa9b4): undefined reference to `dlsym'
collect2: ld returned 1 exit status
also , I continue to get above ^^ errors when I compile without using libmongoose.so
[EDIT] : added -pthread option on gcc, still shows errors :-
mongoose.o: In function load_dll':
mongoose.c:(.text+0xa955): undefined reference todlopen'
mongoose.c:(.text+0xa9b4): undefined reference to `dlsym'
collect2: ld returned 1 exit status
For part 1 and part 2 of my question : I want to get rid of these errors and successfully run hello.c program sample successfully.
Thanks in advance .
The -L option is not used for linking against a library, it's used for specifying a search path for dynamic libraries. To link against a specific library, use -l. However, you don't need to link against both of mongoose.o and libmongoose.so, either one is sufficient.
On Linux, you also have to link against the pthread and the dynamic loading library as well, because despite being part of the C standard library, they're not present in libc.so. One more thing to pay attention to is that recent versions of binutils (specifically, of ld) require that the libraries and object files be specified in the order the symbols depend on each other, i. e. libraries must go to the end of the command line.
All in all, use one of the following commands:
gcc -o hello hello.o mongoose.o -ldl -lpthread
or
gcc -L. -o hello hello.o -lmongoose -ldl -lpthread

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