undefined reference to gzdopen, gzclose, gzread - zlib

I have compiled and installed the 2.2.7.2 version of libxml. While compiling, I have this error: Makefile:755: recipe for target 'install-data-local' failed. But the lib files are correctly generated (libxml2.a and libxml2.so).
I'd like to use libxml2 in a C project so I edited my makefiles in order to integrate lib files (libxml2.a and libxml2.so).
The problem is that when I compile my project I get the followings errors:
/libxml2.a(xmlIO.o): In function xmlGzfileRead': undefined reference togzdopen'
/libxml2.a(xmlIO.o): In function xmlGzfileRead': undefined reference togzclose'
/libxml2.a(xmlIO.o): In function xmlGzfileRead': undefined reference togzread'
...etc
It seems that I have to install the zlib library in order to resolve this linker errors. I installed the zlib library and edited the LD_LIBRARY_PATH in order to add the path where are the zlib libraries.
I recompiled my project, but I still always having the same linker errors.
Would you please help me to resolve those linker errors.
Regards.

See the libxml2 FAQ:
Troubles compiling or linking programs using libxml2
Usually the problem comes from the fact that the compiler doesn't get the right compilation or linking flags. There is a small shell script xml2-config which is installed as part of libxml2 usual install process which provides those flags. Use
xml2-config --cflags
to get the compilation flags and
xml2-config --libs
to get the linker flags. Usually this is done directly from the Makefile as:
CFLAGS=`xml2-config --cflags`
LIBS=`xml2-config --libs`
On my current system, the output from xml2-config --libs is
-lxml2 -lz -lpthread -licucore -lm

Related

How to fix "cannot find -lz"

I am working on code have Zlib.h header, This header is found in my code folder, I compile this code by using
gcc -o x xx.c -lz
but I get on this
/usr/bin/ld: cannot find -lz
collect2: error: ld returned 1 exit status
This happen just with Linux that I installed in a VBox.
How to fix that.
Try installing 'zlib1g-dev'. On Ubuntu this following command will install the library.
sudo apt install zlib1g-dev
When you type gcc foo.c, you ask gcc to compile and link the given file.
1. Compilation
Compilation consist of transforming the source file into an object file.
This step need the included files, like zlib.h to be found by gcc.
This step seems to be correct on system.
NB: You can ask gcc to only do this step typing gcc -c foo.c, or better gcc -Wall -c foo.c
2. Link
Once the object files have be created, then need to be linked to create an executable file.
It's that step that failed for you: your linked can't find the already compiled functions needed by your code.
When linking with option -lz, you tell your linker "search for libz.so file to find the missing functions"
On current linux distribution, you can install package like libz-dev to install the .so file in well known places. (/lib, /usr/lib, /usr/local/lib...)
If you don't have the libz.so file installed on the library search path, you can specify where is the library to your linker.
For instance, if libz.so is if /bar/baz directory, you can type gcc foo.c /bar/baz/libz.so. The same for libz.a.
In any case, you'll need the libz.so file or at least the libz.a file
See also What's the difference between .so, .la and .a library files?

What are the proper flags to compile c code using zlib

I'm trying to execute the zpipe.c file from https://zlib.net/zlib_how.html
gcc zpipe.c -o executable -lz
But I'm getting the error
zpipe.c:18:18: fatal error: zlib.h No such file or directory
I installed zlib from source following this tutorial https://geeksww.com/tutorials/libraries/zlib/installation/installing_zlib_on_ubuntu_linux.php
you can use pkg-config.
pkg-config --cflags --libs zlib
Try adding -I/usr/local/include/. It could happen that you have to specify even the library's location with -L/usr/local/lib/.

Linker error with FFmpeg on Windows

I'm trying to link FFmpeg libraries to a C program on Windows, but I can't successfully link the libraries. I downloaded the Dev archive from Zeranoe and put it into my project folder. (EDIT: The 64-bits version). The compilation works well but when I'm trying to link libraries, I get some undefined reference errors. Here is the command I'm trying:
gcc -o .\metadata.exe .\metadata.o -L .\ffmpeg-20170628-c1d1274-win64-dev\lib\ -lavutil -lavformat -lavdevice -lavfilter -lavcodec -lswresample -lswscale -lm
It then outputs me:
.\metadata.o:metadata.c:(.text+0x44): undefined reference to 'av_register_all'
.\metadata.o:metadata.c:(.text+0x6c): undefined reference to 'avformat_open_input'
.\metadata.o:metadata.c:(.text+0xc8): undefined reference to 'av_dict_get'
.\metadata.o:metadata.c:(.text+0xdf): undefined reference to 'avformat_close_input'
Paths are good because if I change the -libs parameters or the -L parameter I get files not found errors. Finally, I took those libraries from the Makefile in the examples' folder.
What is the source of the error? Thanks in advance, have a nice day!
EDIT: Okay so I found what was the problem. As I'm using MinGW, it includes a 32-bits compiler. So as I downloaded the 64-bits, something was off. I tried downloading the 32-bits builds and it built and linked successfully with only -lavformat and -lavutil parameters. And to run the program I needed the DLL which can be found in the Shared build (and 32-bits of course) on Zeranoe's website. Thanks!

C Program Compilation issue involving Glib2 library?

I'm trying to compile the following project on a remote server.
I've git cloned the project on a folder called 'scode'.
The project requires glib2 and gsl libraries. Since I'm trying to compile on a remote server, I do not have sudo privileges. So I can't use a tool to install glib2 and gsl for me.
As a result, I've manually compiled both gsl and gslib2 under the folders 'scode/gsl' and 'scode/glib'.
I've had to modify the Makefile and add absolute paths to these directories as -I options.
Nonetheless, when I try to compile the final executable. I get the following error:
[dyuret#psglogin scode]$ make
gcc -O3 -D_GNU_SOURCE -Wall -std=c99 -I. -I /home-2/dyuret/scode/gsl
-I /home-2/dyuret/scode/glib/ pkg-config --cflags glib-2.0 scode.o svec.o pkg-config --libs glib-2.0 -lm -lgsl -lgslcblas -o scode
//home-2/dyuret/scode/glib/glib/libglib-2.0.la: file not recognized:
File format not recognized
collect2: error: ld returned 1 exit status make: * [scode] Error 1
I've researched the issue a bit. This link looks informative but I can't quite decipher what the author is saying, as I'm not that experienced with compilers, libtools and the compilation flow in general.
Any help would be much appreciated. I've already spent some time on this issue and I haven't been able to make much progress.
It sounds as if what you did in order to compile the libraries in non-default (i.e. non-system) locations was maybe wrong.
For packages using autoconf (i.e. that have a configure script in the source root) you're supposed to use the --prefix option to ./configure to set the target location where you want the package installed.
With packages building shared libraries, it's often essential to do the make install step, which it sounds as if you maybe didn't do.
Sorry for being vague, these things are a bit complicated.
Someone at my group helped me with the problem. Here're the steps he roughly carried out:
(1) Manually installed glib and additional libraries at $HOME directory - i.e. $HOME/lib, $HOME/include.
(1.1) I think he did this by './configure prefix=$HOME', 'make', 'make install'.
(2) Got rid of `pkg_config` usage, which was causing the problem I outlined originally. Here are his new CLFAGS and LIBS variables:
CFLAGS=-O3 -D_GNU_SOURCE -Wall -std=c99 -I. -I$$HOME/include -I$$HOME/include/glib-2.0 -I$$HOME/lib/glib-2.0/include
LIBS=-lglib-2.0 -lm -lgsl -lgslcblas -L$$HOME/lib -L/usr/local/cuda/lib64 -lcudart
After this, the code compiled without additional problems.

Reason and solution for error -"/usr/bin/ld: cannot find -levent "?

While compiling my program which is using libevent library I am using gcc option -levent. But I am getting this error -
/usr/bin/ld: cannot find -levent
I do not have libevent on my system so I am statically linking to it while compiling using
gcc -o Hello -static -I libevent-1.4.12-stable/ hello.c -levent
How can i resolve this?
Thanks in advance!
Where is the libevent.(a|so) file on your system?
If it isn't on your system's library path then you will have to add a -L option adding its location to the list of paths searched by the linker for libraries.
e.g.
gcc -L/folder/containing/event/lib -levent mysource.cc
You need to have the libevent on your system or need to specify its path explicitly (if its a third-party library you got with the headers).
I suspect its not in your default /lib paths.

Resources