Linking the static version of a library instead of the dynamic one - gcc

I am trying to use libjpeg in my application. Making the project yields a libjpeg.a in .libs folder. What I would like to do is to use this file in the linkage stage. I have tried the following: I copied the libjpeg.a into the folder where my C code resides. Trying to link with
gcc libjpeg.a mycode.c -o executable_name
fails. If I do gcc -ljpeg mycode.c, the compilation is successful when I change my header to point to instead of "libjpeg.h", but this obviously links to the system-wide dynamic version of the library.
Trying to link with relative or absolute path also fails:
gcc ./libjpeg.a mycode.c -o executable_name
I have tried the static option as well:
gcc -static libjpeg.a mycode.c -o executable_name
The linker error is the following:
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 with a Makefile here.

You'd have to give the full path to libjpeg.a, If you have libjpeg.a in a .libs folder relative to where you compile:
gcc mycode.c -o executable_name .libs/libjpeg.a
If your special libjpeg.a is elsewhere, give a full path to it.
If this fails, you have to tell us what happens. (the details are important, so please copy paste the exact errors and the exact command line that is run).

You need to use -static:
gcc -static -o exec_name mycode.c -ljpeg
No need to copy the archive (.a). You could have found out by reading man ld.

This may 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 I added symbolic link:
sudo ln -s libjpeg.so.62 libjpeg.so
My problem got solved.

Related

How to link Libbluetooth for GCC?

I am trying to use hci.h, hci_lib.h etc... in my c files. The final command to link the object files into exec look like this:
gcc -g3 -o execfile ./dev1.o ./dev2.o ./main.o ./nets.o `pkg-config --cflags --libs glib-2.0 gio-unix-2.0`
However I am getting linking error like this:
undefined reference to `hci_open_dev'
undefined reference to `hci_le_set_scan_parameters'
undefined reference to `hci_le_set_scan_enable'
undefined reference to `hci_le_set_scan_enable'
undefined reference to `hci_close_dev'
I know that this is because I am not specifying -lbluetooth to gcc.
Is there a way to specify the libbluetooth-dev to pkg-config like I do for glib & gio-unix above?

Linker error undefined reference to `timer_getoverrun' (-lrt linker option doesn't work)

I am trying to compile example code from timer_create() Linux>man_page. using following command line:
gcc example.c -o example -lrt
Nevertheless, I have linker error:
undefined reference to `timer_getoverrun'
Why -lrt couldn't work.
Sorry - timer_getoverrun() isn't implemented under Cygwin.

Error in linking while creating a shared library of AES C file [duplicate]

This question already has answers here:
Linking OpenSSL libraries to a program
(4 answers)
Closed 5 years ago.
Encountering error while creating a shared library of AES. The following commands are used :
gcc -Wall Test1.c x64/libSESDAPI.a -fPIC -lssl -lcrypto
gcc -shared -o libfile.so a.out -nostartfiles
And I am getting the following errors:
/usr/bin/ld: error in a.out(.eh_frame); no .eh_frame_hdr table will be created.
/usr/bin/ld: libfile.so: No symbol version section for versioned symbol `AES_cbc_encrypt##OPENSSL_1.0.0'
/usr/bin/ld: final link failed: Nonrepresentable section on output
collect2: error: ld returned 1 exit status
I am new to shared libraries so please help.
Edit: I have edited and added the lcrypto and lssl
Edit2: By adding a -c in the first command, the above errors are now resolved. But now accessing the .so file with python gives a new error
OSError: ./libfile.so: undefined symbol: SDSCListDevs
Please tell why this error is coming.
You should be creating your shared library from an object file, not from an executable program.
You also need to link with the SSL libraries.
Commands to use:
# Compile the source file, generate object file
gcc -Wall Test1.c -c -fPIC
# Link object file with libraries to create the shared object
gcc -shared -fPIC -o libfile.so Test1.o x64/libSESDAPI.a -lssl -lcrypto

getting ld to ignore undefined references when making a DLL

I'm trying to get gcc's ld to ignore unresolved references when putting together a shared library from a bunch of object files compiled with -fpic flag.
I tried a bunch of options so far such as (replaced the long file names of the many object files with a few small ones for brevity):
ld --allow-shlib-undefined --unresolved-symbols=ignore-all -shared 1.o 2.o -o lib0.so
ld -G 1.o 2.o -o lib0.so
(I've red somewhere that -G will allow unresolved references, but had no luck with it.)
Running it though gcc (with -Wl,--unresolved-symbols=ignore-all) results in fewer unresolved references as it links by default with
-lstdc++ -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt -lpthread -ladvapi32 -lshell32 -luser32 -lkernel32 -liconv -lmingw32 -lgcc_s -lgcc -lmoldname -lmingwex -lmsvcrt
but it still complains about not having -lopengl32 and -lgdi32.
Compiling the lib without -fpic, stashing all the object files in a .a with ar and static linking to it while having the exe program link to -lopengl32 and -lgdi32 results in working completely fine.
The actual error messages (replaced the long file and function names for brevity):
[file].o: In function `[function]': [file].cpp:19: undefined reference to `memcpy'
[file].o: In function `[function]': [file].cpp:26: undefined reference to `memcpy'
[file].o:[file].cpp:(.xdata+0x4c): undefined reference to `__gxx_personality_seh0'
[file].o:[file].cpp:(.xdata+0x74): undefined reference to `__gxx_personality_seh0'
[file].o:[file].cpp:(.rdata$[file]]+0x20):undefined reference to `__cxa_pure_virtual'
[file].o:[file].cpp:(.rdata$[file]]+0x28): undefined reference to `__cxa_pure_virtual'
How can I get ld to ignore undefined references (at least from libopengl32 and libgdi32 if not libstdc++ etc too) and let the program thats going to use it link to them?
Actually ld is not part of gcc. It is part of binutils.
Try to link with gcc, or in case of C++ with g++ instead of ld.
Also, on Windows you can't build shared libraries (.dll) or executables (.exe) with unresolved symbols. Part of "DLL hell" is that you must make sure all symbols resolve.
Normally it's a matter of finding the library providing those symbols and adding the required -l flags.
However errors like undefined reference to 'memcpy' indicate it's not finding stuff in the standard library. So either you're using ld that did not come with MinGW or there is a flag like -nostdlib present telling the compiler not to look for the standard library.

changing gcc parameters in netbeans 7.0.1

I have a single c file that can be compiled by the following command without any error:
gcc -o largeread main.c -L. -lftd2xx -Wl,-rpath /usr/local/lib
But when I try to build this single file in one netbeans 7.0.1 application project I get the following error:
/home/mohsen/NetBeansProjects/largeread/main.c:45: undefined reference to `FT_Open'
/home/mohsen/NetBeansProjects/largeread/main.c:57: undefined reference to `FT_ResetDevice'
/home/mohsen/NetBeansProjects/largeread/main.c:58: undefined reference to `FT_SetBaudRate'
It means that some libraries are not added to my project. I have added -L. -lftd2xx -Wl,-rpath /usr/local/lib to:
File->Project Properties->Categories->Build->C Compiler->Command Line->Additional Options
but I still have the same problem. Does anyone have any idea?
thanks.

Resources