Linking the static version of the library instead of the dynamic

I am trying to use libjpeg in my application. Creating a project gives libjpeg.a in the .libs folder. I would like to use this file at the build stage. I tried the following: I copied libjpeg.a to the folder where my C code is located. Trying to contact

gcc libjpeg.a mycode.c -o executable_name 

fails. If I do gcc -ljpeg mycode.c, compilation will succeed when I change the title to specify instead of "libjpeg.h", but this is obviously due to the system-wide dynamic version of the library.

An attempt to establish a connection with a relative or absolute path also fails:

 gcc ./libjpeg.a mycode.c -o executable_name 

I also tried a static parameter:

 gcc -static libjpeg.a mycode.c -o executable_name 

The linker error is as follows:

 Linking... gcc -std=c99 -Wall -Wextra -g -pedantic ./libjpeg.a ./libjpeg.a -lm obj/read_jpeg.o obj/utils.o -o test_jpeg obj/read_jpeg.o: In function `read_JPEG_file': /home/ustun/Downloads/jpeg_test/read_jpeg.c:37: undefined reference to `jpeg_std_error' /home/ustun/Downloads/jpeg_test/read_jpeg.c:45: undefined reference to `jpeg_CreateDecompress' /home/ustun/Downloads/jpeg_test/read_jpeg.c:46: undefined reference to `jpeg_stdio_src' /home/ustun/Downloads/jpeg_test/read_jpeg.c:47: undefined reference to `jpeg_read_header' /home/ustun/Downloads/jpeg_test/read_jpeg.c:48: undefined reference to `jpeg_start_decompress' /home/ustun/Downloads/jpeg_test/read_jpeg.c:62: undefined reference to `jpeg_read_scanlines' /home/ustun/Downloads/jpeg_test/read_jpeg.c:74: undefined reference to `jpeg_finish_decompress' /home/ustun/Downloads/jpeg_test/read_jpeg.c:75: undefined reference to `jpeg_destroy_decompress' obj/read_jpeg.o: In function `read_JPEG_file_props': /home/ustun/Downloads/jpeg_test/read_jpeg.c:93: undefined reference to `jpeg_std_error' /home/ustun/Downloads/jpeg_test/read_jpeg.c:100: undefined reference to `jpeg_CreateDecompress' /home/ustun/Downloads/jpeg_test/read_jpeg.c:101: undefined reference to `jpeg_stdio_src' /home/ustun/Downloads/jpeg_test/read_jpeg.c:102: undefined reference to `jpeg_read_header' /home/ustun/Downloads/jpeg_test/read_jpeg.c:103: undefined reference to `jpeg_start_decompress' /home/ustun/Downloads/jpeg_test/read_jpeg.c:113: undefined reference to `jpeg_read_scanlines' /home/ustun/Downloads/jpeg_test/read_jpeg.c:116: undefined reference to `jpeg_finish_decompress' /home/ustun/Downloads/jpeg_test/read_jpeg.c:117: undefined reference to `jpeg_destroy_decompress' collect2: ld returned 1 exit status make: *** [test_jpeg] Error 1 

You can download a simple project using the Makefile here .

+4
source share
3 answers

You need to specify the full path to libjpeg.a if you have libjpeg.a in the .libs folder relative to where you are compiling:

 gcc mycode.c -o executable_name .libs/libjpeg.a 

If your special libjpeg.a is located elsewhere, provide the full path to it.

If this fails, you must tell us what is happening. (details are important, so copy the exact errors and the exact command line that run).

+5
source

You need to use -static:

 gcc -static -o exec_name mycode.c -ljpeg 

No need to copy the archive (.a). You could find out by reading man ld.

+1
source

This can help you if you have the same problem as mine. In my system I have

libjpeg.so.62 → libjpeg.so.62.0.0 libjpeg.so.62.0.0

After adding a symlink:

sudo ln -s libjpeg.so.62 libjpeg.so

My problem is resolved.

0
source

All Articles