program wont compile via Makefile when using ncurses library? - c

I'm trying to make a program using the ncurses library, but for some reason when I try to compile using my Makefile it says there's an error in the Makefile and all of the functions from ncurses are undefined. I included the library in the main.c file correctly since when I compile using gcc main.c -lncurses the program works correctly and doesn't give any errors or say that anything is undefined. I've tried messing around with the Makefile and I can't get it to work properly, does anyone have a solution?
Makefile:
CC = gcc
CFLAGS = -Wall --std=c99 -g
LDFLAGS = -lncurses
OBJECTS = main.o
ALL: space_invaders
space_invaders: $(OBJECTS)
$(CC) $(CFLAGS) -o space_invaders $(OBJECTS)
main.o: main.c
$(CC) $(CFLAGS) -c main.c -o main.o
clean:
rm space_invaders $(OBJECTS)
Output:
gcc -Wall --std=c99 -g -o space_invaders main.o
/usr/bin/ld: main.o: in function main': /home/kyle/space_invaders/main.c:9: undefined reference to initscr'
/usr/bin/ld: /home/kyle/space_invaders/main.c:10: undefined reference to noecho' /usr/bin/ld: /home/kyle/space_invaders/main.c:11: undefined reference to curs_set'
/usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr'
/usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:22: undefined reference to stdscr'
/usr/bin/ld: /home/kyle/space_invaders/main.c:23: undefined reference to stdscr' /usr/bin/ld: /home/kyle/space_invaders/main.c:23: undefined reference to wclear'
/usr/bin/ld: /home/kyle/space_invaders/main.c:24: undefined reference to mvprintw' /usr/bin/ld: /home/kyle/space_invaders/main.c:25: undefined reference to stdscr'
/usr/bin/ld: /home/kyle/space_invaders/main.c:25: undefined reference to `wrefresh'
collect2: error: ld returned 1 exit status
make: *** [Makefile:7: space_invaders] Error 1

LDFLAGS is defined but not used. Add it to the rule that links the final executable.
space_invaders: $(OBJECTS)
$(CC) $(CFLAGS) -o space_invaders $(OBJECTS) $(LDFLAGS)
Alternatively the Make implicit rules can be used to simplify the makefile:
CC = gcc
CFLAGS = -Wall --std=c99 -g
LDFLAGS = -lncurses
OBJECTS = main.o
ALL: space_invaders
space_invaders: $(OBJECTS)
clean:
rm space_invaders $(OBJECTS)

you have to specify the $(LDFLAGS) variable in the linking command:
space_invaders: $(OBJECTS)
$(CC) $(CFLAGS) $(LDFLAGS) -o space_invaders $(OBJECTS)
As you probably have seen, the linking command in the output of your run doesn't show the -lncurses option, so the library has not been including, making all the calls to library functions to be unresolved.
I don't recommend you to use the alternative given in another answer about using the implicit linking rule, as it is applicable only to GNU make, and so, it is not portable to other make's around.

Related

Linking a jpeglib to in makefile

Hi I am trying to use jpeglib in my code and i have trouble linking it with my Makefile
I downloaded it in tar.gz file then extracted it and did all the ./configure then the makes and all this stuff but now I have to link it in Makefile and I dunno how here is the Makefile
CFLAGS+= -Wall -Werror -fPIE -std=gnu99 -g
LDFLAGS= -pthread
HW=prgsem
BINARIES=prgsem
#LDFLAGS += -L/usr/local/lib -ljpeglib
#CXXFLAGS += -I/usr/local/include
CFLAGS+=$(shell sdl2-config --cflags)
LDFLAGS+=$(shell sdl2-config --libs) -lSDL2_image
all: ${BINARIES}
OBJS=${patsubst %.c,%.o,${wildcard *.c}}
prgsem: ${OBJS}
${CC} ${OBJS} ${CXXFLAGS} ${LDFLAGS} -o $#
${OBJS}: %.o: %.c
${CC} -c ${CFLAGS} $< -o $#
clean:
rm -f ${BINARIES} ${OBJS}
The commented stuff is what I tried and didnt work. Also I tried to change the #include itself. Tried #include "jpeglib.h" also #include <jpeglib.h> nothing worked.
EDIT: added make compile error message
cc xwin_sdl.o event_queue.o prg_io_nonblock.o gui.o main.o prgsem.o messages.o keyboard.o computation.o utils.o -pthread -L/usr/local/lib -Wl,-rpath,/usr/local/lib -Wl,--enable-new-dtags -lSDL2 -lSDL2_image -o prgsem
/usr/bin/ld: gui.o: in function `save_img':
/home/peter/Cprog/bab36prga-sem/gui.c:67: undefined reference to `jpeg_std_error'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:69: undefined reference to `jpeg_CreateCompress'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:74: undefined reference to `jpeg_stdio_dest'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:81: undefined reference to `jpeg_set_defaults'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:83: undefined reference to `jpeg_start_compress'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:90: undefined reference to `jpeg_write_scanlines'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:93: undefined reference to `jpeg_finish_compress'
/usr/bin/ld: /home/peter/Cprog/bab36prga-sem/gui.c:97: undefined reference to `jpeg_destroy_compress'
collect2: error: ld returned 1 exit status
make: *** [Makefile:19: prgsem] Error 1
Thanks for any answers.
Your problem is not during the compilation phase of your program, so changing the #include etc. won't help in this case.
Your problem is during the linking phase, so that means you have not added the library to your link line. If, for example, the library is named libjpeg.a or libjpeg.so, then you need to add -ljpeg to your link line. The easiest way is to add it to the end of LDFLAGS:
LDFLAGS+=$(shell sdl2-config --libs) -lSDL2_image -ljpeg

Makefile for C program using libpcap (dependency problems)

I'm building a Makefile for a libpcap-based program. This Makefile will be used to compile the program in the OpenWrt SDK and then transfer the executable to my router. This is the Makefile:
path = /home/foo/Desktop/sdk/openwrt-sdk-18.06.4-x86-64_gcc- 7.3.0_musl.Linux-x86_64/staging_dir/target-x86_64_musl/usr/lib
pcap_retrans: pcap_retrans.o
$(CC) $(LDFLAGS) -o pcap_retrans pcap_retrans.o -L$(path) -lpcap -L -libc
pcap_retrans.o: pcap_retrans.c
$(CC) $(CFLAGS) -c pcap_retrans.c cbuffer.c
However, the following errors appear when I "make":
cc -c pcap_retrans.c cbuffer.c
cc -o pcap_retrans pcap_retrans.o -L/home/inesmll/Desktop/sdk/openwrt-sdk-18.06.4-x86-64_gcc-7.3.0_musl.Linux-x86_64/staging_dir/target-x86_64_musl/usr/lib -lpcap -L -libc
/usr/bin/ld: warning: libc.so, needed by
/home/inesmll/Desktop/sdk/openwrt-sdk-18.06.4-x86-64_gcc-7.3.0_musl.Linux-x86_64/staging_dir/target-x86_64_musl/usr/lib/libpcap.so, not found (try using -rpath or -rpath-link)
pcap_retrans.o: In function `got_packet':
pcap_retrans.c:(.text+0x30a): undefined reference to `cbuffer_put'
pcap_retrans.c:(.text+0x334): undefined reference to `cbuffer_getretrans'
pcap_retrans.c:(.text+0x364): undefined reference to `cbuffer_getnumretrans'
pcap_retrans.o: In function `main':
pcap_retrans.c:(.text+0x818): undefined reference to `cbuffer_init'
pcap_retrans.c:(.text+0x87c): undefined reference to `cbuffer_free'
/usr/bin/ld: pcap_retrans: hidden symbol `atexit' in /usr/lib/x86_64-linux-gnu/libc_nonshared.a(atexit.oS) is referenced by DSO
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'pcap_retrans' failed
make: *** [pcap_retrans] Error 1
I believe it has to do with the way I'm linking the cbuffer.c in the Makefile (this file is in the same folder as pcap_retrans.c), however I don't know how to fix it. Any ideas?
$(CC) $(CFLAGS) -c pcap_retrans.c cbuffer.c looks suspect.
You may like to compile pcap_retrans.c and cbuffer.c into separate object files. And make pcap_retrans depend on pcap_retrans.o and cbuffer.o. E.g.
pcap_retrans: pcap_retrans.o cbuffer.o
$(CC) $(LDFLAGS) -o $# $^ -L$(path) -lpcap
%.o: %.c
$(CC) -c $(CFLAGS) -o $# $<
Just realized that -libc actually links a library called libibc.so or libibc.a, not the standard C library. If that library is in directory <dir>, you can link it in a couple of ways:
Pass the full path to the library in the linker command, e.g. $(CC) $(LDFLAGS) -o $# $^ -L$(path) -lpcap <dir>/libibc.so.
Specify the linker paths in the linker command, e.g. $(CC) $(LDFLAGS) -o $# $^ -L$(path) -lpcap -L<dir> -Wl,-rpath=<dir> -libc. You can specify multiple -Wl,-rpath= and this is where ld.so (the runtime dynamic linker) will search for libibc.so at run-time.

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?

What is missing in my makefile?

I am trying to create my first makefile. I tested my program by using the following commands:
Command 1: gcc -Wall -ggdb3 -std=c99 -o file1 file1.c -lm -lpthread -l
Command 2: gcc -Wall -ggdb3 -std=c99 -o file2 file2.c -lm -lpthread
Everything works great. Then I created a makefile (please see below). I keep getting an error message. Can someone take a look at my code and give me a hint on what the problem is?
file2.o: In function `seed_primes':
file2.c:(.text+0x461): undefined reference to `sqrt'
file2.c:(.text+0x466): undefined reference to `sqrt'
file2:(.text+0x533): undefined reference to `sqrt'
file2.o: In function `create_threads':
file2.c:(.text+0x668): undefined reference to `pthread_create'
file2.c:(.text+0x6b5): undefined reference to `pthread_join'
file2.o: In function `next_seed':
file2.c:(.text+0x860): undefined reference to `sqrt'
collect2: ld returned 1 exit status
make: *** [file2] Error 1
Here is my makefile:
CC=gcc
DEBUG=-ggdb3
CFLAGS=#(DEBUG) -Wall -lm -lpthread -lrt -l
PROGS=file1 file2
all: $(PROGS)
file1: file1.o
$(CC) $(CFLAGS) -o file1 file1.o
file1.o: file1.c
$(CC) $(CFLAGS) -c file1.c
file2: file2.o
$(CC) $(CFLAGS) -o file2 file2.o
file2.o: file2.c
$(CC) $(CFLAGS) -c file2.c
clean:
rm -f $(PROGS) *.o *~
You've set CFLAGS to an empty string because of the # comment character (you probably intended to use a $ instead).
You should not set libraries into CFLAGS; they belong in LDLIBS.
You don't need the file1: rule, the file2: rule, or the object file rules.
CC = gcc
DEBUG = -ggdb3
CFLAGS = $(DEBUG) -Wall
LDLIBS = -lm -lpthread -lrt -l
PROGS = file1 file2
all: $(PROGS)
clean:
rm -f $(PROGS) *.o *~
NB: LDLIBS and the related LDFLAGS are not 100% uniform across variants of make. LDFLAGS should be used for library paths; LDLIBS is for the library names (-lxyz etc).
If you need different libraries for the two programs, you will need to create separate build rules (as you had originally), or use conditional macro assignments (GNU make).
You put all of your flags in CFLAGS which makes them appear before the object files in the command line. Notice that your test commands didn't do that.
Change your flags:
CFLAGS=$(DEBUG) -Wall
LDFLAGS=-lm -lpthread -lrt
And then in the recipes:
$(CC) $(CFLAGS) -o file1 file1.o $(LDFLAGS)

Undefined reference to exp on Ubuntu (including math.h and linking with -lm)

I'm having some trouble trying to compile a program that uses exp function on Ubuntu. I get this error from gcc:
selied#Apolo:~/Dropbox/practicas UAM/Neuro/practica3$ make
gcc -lm -o retropropagacion retropropagacion.o
retropropagacion.o: In function `main':
/home/selied/Dropbox/practicas UAM/Neuro/practica3/retropropagacion.c:177: undefined reference to `exp'
/home/selied/Dropbox/practicas UAM/Neuro/practica3/retropropagacion.c:186: undefined reference to `exp'
/home/selied/Dropbox/practicas UAM/Neuro/practica3/retropropagacion.c:297: undefined reference to `exp'
/home/selied/Dropbox/practicas UAM/Neuro/practica3/retropropagacion.c:306: undefined reference to `exp'
collect2: ld devolvió el estado de salida 1
make: *** [retropropagacion] Error 1
Here I show you my makefile.
CC = gcc
LDLAGS = -lm
CFLAGS = -Wall -g
EXE = retropropagacion normalizar
OBJ =
INC =
compile : $(EXE)
clean :
#echo Borrando archivos temporales...
rm -f *~ *.o core $(EXE)
help :
#echo
backpropagation :
./retropropagacion entrada.txt 0 0 salida.txt
and :
./retropropagacion and.dat 0 0 salida_and.txt
$(EXE) : % : %.o $(OBJ)
$(CC) $(LDLAGS) -o $# $#.o $(OBJ)
%.o : %.c $(INC)
$(CC) $(CFLAGS) -c $<
Also I have include at the top of my header file and it works on another computer.
Do you know what's happening?
$(CC) $(LDLAGS) -o $# $#.o $(OBJ)
should be
$(CC) -o $# $#.o $(OBJ) $(LDLAGS)
Whether -l flags can be given before object files depends on the GCC version.
Never mind. For further interested about this issue or struggling also too long, also line
LDLAGS = -lm
should be written as
LDLIBS = -lm
because LDLIBS are put after the object files, unlike the LDFLAGS, which ends in front of them in default make template, as documentation hints.

Resources