Linking a jpeglib to in makefile - c

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

Related

how to compile netcdf

Hello I am using given code from the examples to test if I have installed netcdf correctly. I installed the prebuilt netCDF4.8.1-NC4-DAP-64 and put it in the environment variable path (I checked to make sure it worked, it did. Next I did #include "netcdf.h" in the main c file and then made the make file as such:
CC = gcc
EXECUTABLES = try
INCLUDES = -IC:/Program\ Files/netCDF\ 4.8.1/include/
CFLAGS = $(INCLUDES)
LIBS = -LC:/Program\ Files/netCDF\ 4.8.1/lib -lnetcdf -lmfhdf -ljpeg -lhdf5_hl -lhdf5 -lz -lm
all: $(EXECUTABLES)
try: try.c
$(CC) $(CFLAGS) -o try try.c $(LIBS)
clean:
rm core $(EXECUTABLES) *.o
I tried to look at forums and added diffrent flags such as all of
-lnetcdf -lpnetcdf -lmfhdf -ldf -ljpeg -lhdf5_hl -lhdf5 -lz -lsz -lm
but it would throw
gcc -IC:/Program\ Files/netCDF\ 4.8.1/include/ -o try try.c -LC:/Program\ Files/netCDF\ 4.8.1/lib -lnetcdf -lpnetcdf -lmfhdf -ldf -ljpeg -lhdf5_hl -lhdf5 -lz -lsz -lm
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -lpnetcdf
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -ldf
C:/Program Files (x86)/mingw-w64/i686-8.1.0-posix-dwarf-rt_v6-rev0/mingw32/bin/../lib/gcc/i686-w64-mingw32/8.1.0/../../../../i686-w64-mingw32/bin/ld.exe: cannot find -lsz
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:10: try] Error 1
so I removed theose flags and kept the rest but got the error:
gcc -IC:/Program\ Files/netCDF\ 4.8.1/include/ -o try try.c -LC:/Program\ Files/netCDF\ 4.8.1/lib -lnetcdf -lmfhdf -ljpeg -lhdf5_hl -lhdf5 -lz -lm
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0xa3): undefined reference to `nc_create'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0xc3): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x109): undefined reference to `nc_def_dim'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x129): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x16f): undefined reference to `nc_def_dim'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x18f): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x204): undefined reference to `nc_def_var'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x224): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x24f): undefined reference to `nc_enddef'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x26f): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x2ad): undefined reference to `nc_put_var_int'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x2cd): undefined reference to `nc_strerror'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x2f8): undefined reference to `nc_close'
C:\Users\NPAT~1\AppData\Local\Temp\ccAbmx3N.o:try.c:(.text+0x318): undefined reference to `nc_strerror'
collect2.exe: error: ld returned 1 exit status
make: *** [makefile:10: try] Error 1
Any help is appreciated, Thank You
Update!! I think that because the files I was trying to link were .lib, gcc was not linking it. I changed form vscode to vs community and added the .lib and it started to work properly.
Instead of compiling with gcc I compiled with MSVC.

program wont compile via Makefile when using ncurses library?

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.

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?

g++ linker error “undefined reference to "Class::function"”

I'm using the makefile to compile my C++ project in a linux IDE.
The makefile is as follows:
#-------------------------------
SRCDIR=src
ICDDIR=include
TESTDIR=test
CC=g++
OBJS = main.o Complex.o TestComplex.o
DEBUG = -g
CFLAGS = -Wall -c $(DEBUG)
LFLAGS = -Wall $(DEBUG)
#-------------------------------
test : $(OBJS)
$(CC) $(LFLAGS) $(OBJS) -o output -I $(ICDDIR)
main.o : $(SRCDIR)/main.cpp
$(CC) $(CFLAGS) $(SRCDIR)/main.cpp -I $(ICDDIR)
Complex.o : $(SRCDIR)/Complex.cpp $(ICDDIR)/Complex.h
$(CC) $(CFLAGS) $(SRCDIR)/Complex.cpp -I $(ICDDIR)
TestComplex.o : $(TESTDIR)/TestComplex.cpp $(ICDDIR)/TestComplex.h $(ICDDIR)/Complex.h
$(CC) $(CFLAGS) $(TESTDIR)/TestComplex.cpp -I $(ICDDIR)
And the terrible error message:
make
g++ -Wall -c -g src/main.cpp -I include
g++ -Wall -c -g src/Complex.cpp -I include
g++ -Wall -c -g test/TestComplex.cpp -I include
g++ -Wall -g main.o Complex.o TestComplex.o -o output -I i
nclude
main.o: In function `main':
proj1/src/main.cpp:11: undefined reference to
`Complex::Complex()'
proj1/src/main.cpp:11: undefined reference to
`Complex::Complex()'
proj1/src/main.cpp:13: undefined reference to
`Complex::Complex(double, double)'
proj1/src/main.cpp:14: undefined reference to
`Complex::Complex(double, double)'
TestComplex.o: In function `TestComplex::TestComplex()':
proj1/test/TestComplex.cpp:8: undefined refer
ence to `Complex::Complex()'
TestComplex.o: In function `TestComplex::compute(Complex, Complex)':
proj1/test/TestComplex.cpp:34: undefined refe
rence to `operator+(Complex const&, Complex const&)'
proj1/test/TestComplex.cpp:37: undefined refe
rence to `operator-(Complex const&, Complex const&)'
proj1/test/TestComplex.cpp:40: undefined refe
rence to `operator*(Complex const&, Complex const&)'
proj1/test/TestComplex.cpp:43: undefined refe
rence to `operator/(Complex const&, Complex const&)'
proj1/test/TestComplex.cpp:46: undefined refe
rence to `operator==(Complex const&, Complex const&)'
collect2: error: ld returned 1 exit status
The structure of my project:
src/
Complex.cpp
main.cpp
include/
Complex.h
TeseComplex.h
test/
TestComplex.cpp
Makefile
All my header files and sources files are implemented correctly.
I guess this is a linker problem, but I do link all .o together.
I have no idea what's going wrong,I hope someone could help me with it.
All my header files and sources files are implemented correctly.
No, they are not.
I guess this is a linker problem,
Your guess is incorrect.
Unfortunately, you have not provided sufficient info to help you further. Showing the include/Complex.h and src/Complex.cpp will likely help.

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)

Resources