main.c:(.text+0x170): undefined reference to `sqrt' (used -lm) - c

ok, so i'm trying to compile my code using makefile, i've got only 2 .c file and 1 .h file, i used "sqrt()" function from math.h (only in main), here is my makefile:
a.out: GBST.o main.o
gcc GBST.o main.o
GBST.o: GBST.c GBST.h
gcc -c GBST.c
main.o: main.c
gcc -c main.c -lm
still, I get main.c:(.text+0x170): undefined reference to `sqrt' error, what can it be? (btw, i wrote -lm in GBST line before and it did not help, so i have deleted it)

You need to use -lm in the link line, not in the compile line.
a.out: GBST.o main.o
gcc GBST.o main.o -lm
# ^^^^ Need it here
GBST.o: GBST.c GBST.h
gcc -c GBST.c
main.o: main.c
gcc -c main.c
# ^^^^ Don't need it here

Related

Makefile Linker unable to find functions in static library

sorry for this question that may seem trivial, but I looked at a few tutorials and SO questions and still could not figure out what is wrong.
Anyway, when using gcc, the linker is not able to find functions in the static library I have included.
Error message:
arm-none-linux-gnueabi-gcc -I. -I./include yuv.c -c -o yuv.o
arm-none-linux-gnueabi-gcc -I. -I./include main.c -c -o main.o
arm-none-linux-gnueabi-gcc -L./lib -I. -I./include yuv.o main.o -lpthread -lrt -ljpeg -o grab.elf
main.o: In function `jpegWrite':
main.c:(.text+0x118): undefined reference to `jpeg_std_error'
main.c:(.text+0x134): undefined reference to `jpeg_CreateCompress'
main.c:(.text+0x144): undefined reference to `jpeg_stdio_dest'
main.c:(.text+0x17c): undefined reference to `jpeg_set_defaults'
main.c:(.text+0x198): undefined reference to `jpeg_set_quality'
main.c:(.text+0x1a8): undefined reference to `jpeg_start_compress'
main.c:(.text+0x1e4): undefined reference to `jpeg_write_scanlines'
main.c:(.text+0x200): undefined reference to `jpeg_finish_compress'
main.c:(.text+0x20c): undefined reference to `jpeg_destroy_compress'
collect2: ld returned 1 exit status
make: *** [grab.elf] Error 1
My file structure is as follows:
Makefile
main.c
In Folder lib
libjpeg.a
My Makefile reads:
GCC=gcc
INC_PATH= -I. -I./include
LIBS_PATH = -L./lib
HEADER_FILE=./yuv.h ./include/jpeglib.h
grab.elf: yuv.o main.o
$(CROSS_COMPILE)$(GCC) $(LIBS_PATH) $(INC_PATH) yuv.o main.o -lpthread -lrt -ljpeg -o grab.elf
yuv.o:yuv.c $(HEADER_FILE)
$(CROSS_COMPILE)$(GCC) $(INC_PATH) yuv.c -c -o yuv.o
main.o:main.c $(HEADER_FILE)
$(CROSS_COMPILE)$(GCC) $(INC_PATH) main.c -c -o main.o
I also tried:
nm libjpeg.a | grep jpeg_std
000001f0 T _jpeg_stdio_dest
00000140 T _jpeg_stdio_src
000001f0 T _jpeg_std_error
000012c0 R _jpeg_std_message_table
Would any kind soul care to help me? Thank you.
Could it be that libjpeg.a was compiled on the platform on which you are developing, whereas the target of grab.elf is different? i.e., you are developing on x86 environment and targeting ARM?

How to compile this lib for usage?

I'm new to C programming, and I'm trying to compile this Simple training example with GCC on Ubuntu 12.10.
Looks like fann.h should not be included (as stated on the file itself), so I included fixedfann.h instead.
First attempt (without include, just to see what the compiler will ask for):
$ gcc main.c -o output
/tmp/cckKyM92.o: In function `main':
main.c:(.text+0x62): undefined reference to `fann_create_standard'
main.c:(.text+0x7a): undefined reference to `fann_set_activation_function_hidden'
main.c:(.text+0x8e): undefined reference to `fann_set_activation_function_output'
main.c:(.text+0xba): undefined reference to `fann_train_on_file'
main.c:(.text+0xce): undefined reference to `fann_save'
main.c:(.text+0xda): undefined reference to `fann_destroy'
collect2: ld returned 1 exit status
fann_create_standard is on fann.h and fann.c. As fann.h is included by fixedfann.h, and fann.h should not be included directly, I believe I have to compile fann.c and fixedfann.c, and link then (tell me if I'm doing any mistake, I'm still not familiar with this "linking" stuff).
So I did:
$ gcc fann/fixedfann.c -o fann/fixedfann.o
fann/fixedfann.c:22:20: fatal error: config.h: No such file or directory
compilation terminated.
and then I did:
$ gcc fann/fixedfann.c -o fann/fixedfann.o -include fann/include/config.h
fann/fixedfann.c:22:20: fatal error: config.h: No such file or directory
compilation terminated.
Now, why it's not finding the config.h file here?
--update
Thanks #JonathanLeffler, I could make some steps here. But now I'm stuck at:
$ gcc fann/fixedfann.c -o fann/fixedfann.o -I./fann/include/ -lm
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status
and, with grep, I could not find any reference to main on the fann folder... Also no function _start, and I don't know who is linking this crt1.o... Any idea what's wrong here?
--update2
Ok, I got the .o files using Harmeet's Makefile, now I'm trying to link everything.
I created the main.o with gcc -c main.c, and I tried:
gcc -o output main.o fann/fixedfann.o -lm
(-lm for the libmath, that is needed) and I got:
main.c:(.text+0xba): undefined reference to `fann_train_on_file'
This fann_train_on_file is on fann_train_data.c, so I tried:
gcc -o output main.o fann/fixedfann.o fann/fann_train_data.o -lm
but I got lots of multiple definition of... errors... :/
Looks like fann_train_data.o is already included/linked, but if so, why it's not finding fann_train_on_file?
--update3
I'm still really stuck here... Any idea of which (if any) of this two lines should work?:
gcc -o output main.o hello.o fann/fixedfann.o fann/fann_train_data.o -lm
or
gcc -o output main.o hello.o fann/fixedfann.o -lm
--update for Harmeet
The output was:
$ make
gcc -L./fann -lfann main.o -o main
main.o: In function `main':
main.c:(.text+0x62): undefined reference to `fann_create_standard'
main.c:(.text+0x7a): undefined reference to `fann_set_activation_function_hidden'
main.c:(.text+0x8e): undefined reference to `fann_set_activation_function_output'
main.c:(.text+0xba): undefined reference to `fann_train_on_file'
main.c:(.text+0xce): undefined reference to `fann_save'
main.c:(.text+0xda): undefined reference to `fann_destroy'
collect2: ld returned 1 exit status
make: *** [main] Error 1
You can use ar to make a static library and work with that.
Create a Makefile under your hello-fann-3/fann/ folder with the following contents -
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
CFLAGS = -c -Iinclude
all: libfann.a
libfann.a: $(OBJECTS)
ar rcs $# $^
%.o: %.c
gcc $(CFLAGS) $^
Then use the make command in hello-fann-3/fann/ to build the static library. The above Makefile will generate libfann.a that you can link to your program.
Create a Makefile under your hello-fann-3/ folder with the following contents -
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)
CFLAGS = -c -I./fann/include
LFLAGS = -L./fann -lfann
main: $(OBJECTS)
gcc $(LFLAGS) $^ -o $#
%.o: %.c
gcc $(CFLAGS) $^
Then use the make command in hello-fann-3/ to build the main program.
In your main.c, you must include fan.h like -
#include "fann.h"
If you do not understand the Makefile, you can read about it here -
http://www.gnu.org/software/make/manual/html_node/index.html
You just need to link the fann library.
If you compile manually do this
gcc main.c -lfann -lm -o main
then simply run it like
./main
If you are on Ubuntu and you faced the following error
./main: error while loading shared libraries: libfann.so.2: cannot open shared object file: No such file or directory
Then run
sudo ldconfig
If you are using NetBeans, then simply Right click on your project -> Properties -> Build -> Linker,
then in the Libraries section click on the browse button [...] then in the new window click on Add Library...
Then add fann library (for example my fann library path is: /usr/local/lib/libfann.a) and click Ok
A fellow helped me, and we came to this line that compiled everything, and make the executable:
$ gcc fann/fann.c fann/fann_io.c fann/fann_train.c fann/fann_train_data.c fann/fann_error.c fann/fann_cascade.c main.c -Ifann/include -lm
And this is the answer.
That said, this is exactly what fixedfann.c is doing (include all this .c files). But if I try:
$ gcc fann/fixedfann.c main.c -Ifann/include -lm
..I get:
undefined reference to `fann_train_on_file'
This fann_train_on_file is on fann_train_data.c, which is included by fixedfann.c, so why it is undefined? I don't know... :/
--update
I realized that:
$ gcc fann/fixedfann.c main.c -Ifann/include -lm
will work if I comment the headers on fixedfann.c:
//#include "config.h"
//#include "fixedfann.h"
#include "fann.c"
#include "fann_io.c"
#include "fann_train.c"
#include "fann_train_data.c"
#include "fann_error.c"
#include "fann_cascade.c"

Am I writing this makefile correctly?

I am learning how to write C code in Linux and I am learning makefiles at a very beginner level.
I am having problems when making shared libraries.
The exercise is to make a simple function calculator C program with files:
main.c
add.c
subt.c
mult.c
div.c
The names of the files define the function they do.
The function in the file subt.c is in the static library:
libsubstatic.a
The function in the file mult.c is in the shared library:
libmultshared.so
For this program, I write the following makefile:
calc.exe: main.o add.o div.o libsubstatic.a libmultshared.so
gcc -o calc.exe main.o add.o div.o libsubstatic.a -Wl,-rpath,/home/ahmed/Desktop/labTask3 -lmultshared.so
main.o: main.c header.h
gcc -c main.c
add.o: add.c header.h
gcc -c add.c
libsubstatic.a: subt.o
ar cr libsubstatic.a subt.o
subt.o: subt.c header.h
gcc -c subt.c
libmultshared.so: mult.o
gcc -shared -fPIC -o libmultshared.so mult.o
mult.o: mult.c header.h
gcc -c -fPIC mult.c
div.o: div.c header.h
gcc -c div.c
The path where the code and makefile is placed:
/home/ahmed/Desktop/labTask3
I get the following message after I type "make" in the terminal:
gcc -o calc.exe main.o add.o div.o libsubstatic.a -Wl, -rpath, /home/ahmed/Desktop/labTask3 -lmultshared.so
gcc: error: unrecognized command line option ‘-rpath,’
make: *** [calc.exe] Error 1
What am I missing? Did I write this makefile correctly?
Please explain shared libraries, my concept might be faulty.
Please help.
Note that, I'm new to linux and I don't have much experience in makefiles.
EDIT: I removed the spaces as directed in the first answer. Now the terminal says:
gcc -o calc.exe main.o add.o div.o libsubstatic.a -Wl,-rpath,/home/ahmed/Desktop/labTask3 -lmultshared.so
/usr/bin/ld: cannot find -lmultshared.so
collect2: error: ld returned 1 exit status
make: *** [calc.exe] Error 1
Should I do something with the "-lmultshared.so"? What should I do?
-Wl, -rpath, /home/ahmed/Desktop/labTask3
Get rid of the spaces. This should all be one long argument.
-Wl,-rpath,/home/ahmed/Desktop/labTask3
See this excellent answer by #KerrekSB for a detailed explanation about passing arguments to the linker with -Wl.

ar introduces undefined reference

I'm trying to figure out how to use static libraries, but the most trivial example fails:
//foo.c
int func(int i) {
return i+1;
}
//main.c
int func(int i);
int main() {
return func(41);
}
Compiling foo.c and main.c works:
gcc -Wall -o foo.o -c foo.c
gcc -Wall -o main.o -c main.c
Archiving foo.o does not complain either:
ar rcs libfoo.a foo.o
But Linking fails with an undefined reference to func:
ld libfoo.a main.o
ld -L. -lfoo main.o
both give me:
ld: warning: cannot find entry symbol _start; defaulting to 00000000004000b0
main.o: In function `main':
main.c:(.text+0xa): undefined reference to `func'
I get a similar error if I take the detour via gcc to link:
gcc libfoo.a main.o
gcc -L. -lfoo main.o
give me:
main.o: In function `main':
main.c:(.text+0xa): undefined reference to `func'
collect2: ld returned 1 exit status
What am I doing wrong here? According to all manuals and search engines I read/used this is the way to use static libraries.
Edit: Mind that gcc foo.o main.o works perfectly fine.
After a lot of trying stupid stuff, the most stupid idea was the solution: ld wants the object files first, then the archives. Yay!
gcc libfoo.a main.o // fails
gcc main.o libfoo.a // works
The same goes if you specify the library with -L. and -lfoo: Where you put -L doesn't matter apparently, but where you put -lfoo matters to the same extent as if you specify the .a file directly.

GCC linker issue with -lm flag

I'm having issues with the GCC linker, specifically using the -lm flag since I'm using some functions from math.h. I get the following errors:
main.c:(.text+0x8e5): undefined
reference to `floor'
main.c:(.text+0x901): undefined
reference to `ceil'
Here's the relevant portion of my makefile:
myprogram: main.o
gcc -Wall -pedantic -o myprogram main.o
main.o: main.c foo.h bar.h
gcc -Wall -pedantic -lm main.c
Probably something silly I'm overlooking, but I'm definitely not an expert when it comes to makefiles.
Furthermore, library specifications have to come after the objects referencing them (cf. Linker flags in wrong place ).
-lm is a linker flag, so you should add it to the linking rule above (i.e., you added it to the wrong rule).

Resources