Unable to build zpipe.c (Zlib sample program) - c

I'm trying to build zpipe.c. I've installed zlib 1.2.5 via configure; make; make install.
I've moved my zpipe.c file into the actual zlib-1.2.5 directory where it includes the zlib.h header file.
This is what I get when I attempt to build:
[sk#lldma zlib-1.2.5]$ gcc zpipe.c
/tmp/ccZ2OBz0.o: In function `def':
zpipe.c:(.text+0x3c): undefined reference to `deflateInit_'
zpipe.c:(.text+0x8f): undefined reference to `deflateEnd'
zpipe.c:(.text+0xe2): undefined reference to `deflate'
zpipe.c:(.text+0x163): undefined reference to `deflateEnd'
zpipe.c:(.text+0x1df): undefined reference to `deflateEnd'
/tmp/ccZ2OBz0.o: In function `inf':
zpipe.c:(.text+0x22d): undefined reference to `inflateInit_'
zpipe.c:(.text+0x280): undefined reference to `inflateEnd'
zpipe.c:(.text+0x2c1): undefined reference to `inflate'
zpipe.c:(.text+0x312): undefined reference to `inflateEnd'
zpipe.c:(.text+0x36e): undefined reference to `inflateEnd'
zpipe.c:(.text+0x398): undefined reference to `inflateEnd'
The README doens't say anything specific about building on Linux and I see that deflate.h and the other .h files are all located in the directory...
The source to zpipe.c is here: http://www.zlib.net/zpipe.c
Any ideas? TIA
[sk#lldma zlib-1.2.5]$ ls
adler32.c example64 infback.c Makefile.in watcom
adler32.lo example64.o infback.lo make_vms.com win32
adler32.o example.c infback.o minigzip zconf.h
amiga example.o inffast.c minigzip64 zconf.h.cmakein
ChangeLog examples inffast.h minigzip64.o zconf.h.in
CMakeLists.txt examplesh inffast.lo minigzip.c zlib2ansi
compress.c FAQ inffast.o minigzip.o zlib.3
compress.lo gzclose.c inffixed.h minigzipsh zlib.3.pdf
compress.o gzclose.lo inflate.c msdos zlib.h
configure gzclose.o inflate.h nintendods zlib.map
contrib gzguts.h inflate.lo old zlib.pc
crc32.c gzlib.c inflate.o qnx zlib.pc.in
crc32.h gzlib.lo inftrees.c README zpipe.c
crc32.lo gzlib.o inftrees.h treebuild.xml zutil.c
crc32.o gzread.c inftrees.lo trees.c zutil.h
deflate.c gzread.lo inftrees.o trees.h zutil.lo
deflate.h gzread.o libz.a trees.lo zutil.o
deflate.lo gzwrite.c libz.so trees.o
deflate.o gzwrite.lo libz.so.1 uncompr.c
doc gzwrite.o libz.so.1.2.5 uncompr.lo
example INDEX Makefile uncompr.o

The problem is not in the headers, it compiles fine. The problem is that the linker can't find the library functions. Try compiling with this:
gcc zpipe.c -lz
This will compile with libz.a or libz.so (depending on whether you are static or dynamically compiling), and if correct these object files should contain the missing functions.

Related

How do I compile with zlib on windows using gcc?

My aim is to compile zlib alongside my application files, I use gcc from tdm-gcc and have cloned the repository https://github.com/madler/zlib
in /example there is a zpipe.c file which utilizes the library but I can't seem to get it compiling on windows.
I've added -lz to the end of my compile command and I receive /x86_64-w64-mingw32/bin/ld.exe: cannot find -lz error.
If I try to compile with just -Izlib I end up with these errors:
$ gcc -Izlib zpipe.c -o zpipe
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x6a): undefined reference to `deflateInit_'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0xca): undefined reference to `deflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x12e): undefined reference to `deflate'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x1be): undefined reference to `deflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x23c): undefined reference to `deflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x2c0): undefined reference to `inflateInit_'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x320): undefined reference to `inflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x36f): undefined reference to `inflate'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x3d1): undefined reference to `inflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x440): undefined reference to `inflateEnd'
C:/TDM-GCC-64/bin/../lib/gcc/x86_64-w64-mingw32/10.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\[redacted]\AppData\Local\Temp\ccjXicsi.o:zpipe.c:(.text+0x474): undefined reference to `inflateEnd'
collect2.exe: error: ld returned 1 exit status
Any help is greatly appreciated! I've searched stack overflow and tried all the suggestions and also googled and I'm still having issues.
The errors seem trivial and I've fixed such errors in the past, but this one I'm stuck on and I'm not sure what else to try.
1) Creating the static library.
Inside the zlib folder where the .c and .h files are located running gcc -c *.c will generate .o files that can be used to build the library.
after you have the .o files, running ar -rc libz.a *.o will generate a libz.a file, this will allow you to link via -lz
2) Compiling zpipe.c into zpipe.exe
put zpipe.c into the folder where libz.a is located, this is for simplicity when compiling.
running gcc -L. zpipe.c -o zpipe.exe -lz will create zpipe.exe.
3) Quick demo
Create a file hello.txt with Hello World! inside.
Run ./zpipe.exe < hello.txt > output.txt this will inflate the data in hello.txt and put it in output.txt
Run ./zpipe.exe -d < output.txt > original.txt to decompress the file and you will see Hello World! inside original.txt
Definitions
-L. tells gcc to use the current folder for the header files.
-lz tells gcc to compile with a static library who's name starts with lib and ends in .a for eg. libexample.a would be -lexample
When using MSYS2 shell it's quite simple, just run the following commands (replace /usr/local with the desired location):
INSTALLPREFIX=/usr/local
wget -c http://www.zlib.net/zlib-1.2.12.tar.gz
tar xfz zlib-1.2.12.tar.gz
cd zlib-1.2.12
make -f win32/Makefile.gcc install \
SHARED_MODE=1 \
INCLUDE_PATH=$INSTALLPREFIX/include \
LIBRARY_PATH=$INSTALLPREFIX/lib \
BINARY_PATH=$INSTALLPREFIX/bin

libcurl curl/curl.h No such file or Directory Win xp 32 MinGW

I have been searching for a couple of hours and found several threads with the same problem and tried all their suggestions.
My curl.h is in C:\MinGW\msys\1.0\curl-7.29.0\include\curl and I've included it at compile time but for some reason gcc claims it can't find it:
gcc -o curl.exe curl.c -IC:/MinGW/msys/1.0/local/ -IC:/MinGW/msys/1.0/curl-7.29.0/lib/.libs/ -IC:/MinGW/msys/1.0/curl-7.29.0/include/curl -IC:/MinGW/msys/1.0/OpenSSL-Win32/ -lcurl -lws2_32
and the result:
curl.c:4:32: fatal error: curl/curl.h: No such file or directory
UPDATE: (With the suggestion from H2CO3)
$ gcc -o curl.exe curl.c -IC:/MinGW/msys/1.0/local -IC:/MinGW/msys/1.0/curl-7.29.0/lib/.libs/ -IC:/MinGW/msys/1.0/curl-7.29.0/include/ -IC:/MinGW/msys/1.0/OpenSSL-Win32/include/ -lws2_32
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0xa8): undefined reference to `_imp__curl_global_init'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0xe6): undefined reference to `_imp__curl_formadd'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x124): undefined reference to `_imp__curl_formadd'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x162): undefined reference to `_imp__curl_formadd'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x169): undefined reference to `_imp__curl_easy_init'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x183): undefined reference to `_imp__curl_slist_append'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x1b8): undefined reference to `_imp__curl_easy_setopt'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x211): undefined reference to `_imp__curl_easy_setopt'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x237): undefined reference to `_imp__curl_easy_setopt'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x245): undefined reference to `_imp__curl_easy_perform'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x25e): undefined reference to `_imp__curl_easy_strerror'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x288): undefined reference to `_imp__curl_easy_cleanup'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x296): undefined reference to `_imp__curl_formfree'
C:\DOCUME~1\J10441\LOCALS~1\Temp\ccHP6WWt.o:curl.c:(.text+0x2a4): undefined reference to `_imp__curl_slist_free_all'
collect2: ld returned 1 exit status
If I try to compile with -lcurl it says ..... ld.exe: cannot find -lcurl is this because it can't find the dll?
You're including <curl/curl.h>, and not <curl.h>. So you have to tell the compiler to look for header files in the include directory, and not in include/curl:
gcc -IC:\MinGW\msys\1.0\curl-7.29.0\include etc. etc.
This might be beyond the scope of what you're doing, but, I found it best to install a version of cURL(libcurl) into the MinGW/MSYS environment. After installed, any program can be compiled to use it with the typical <curl/curl.h> and -lcurl conventions since it's installed where GCC would expect it. I documented my process here.

Cannot find libcrypto library error

When i am trying to compile a C code which uses openssl 'crypto' library functions with comand line -lcrypto with gcc 4.4.3 it gives an error
`#ubu:$ gcc -ggdb aes_m.c -Werror -Wall -I /usr/local/ssl/include/ -lcrypto -o aes
/usr/bin/ld: cannot find -lcrypto
collect2: ld returned 1 exit status`
what can be the reason for this??
I have already gone through this discussion ld cannot find an existing library but that does not help.
locate command results in
$ locate libcrypto
/home/abhi/Downloads/openssl-1.0.1b/libcrypto.a
/home/abhi/Downloads/openssl-1.0.1b/libcrypto.pc
/lib/libcrypto.so.0.9.8
/lib/i486/libcrypto.so.0.9.8
/lib/i586/libcrypto.so.0.9.8
/lib/i686/cmov/libcrypto.so.0.9.8
/usr/lib/libcrypto.so.0.9.8
/usr/lib/vmware-tools/lib32/libcrypto.so.0.9.8
/usr/lib/vmware-tools/lib32/libcrypto.so.0.9.8/libcrypto.so.0.9.8
/usr/lib/vmware-tools/lib64/libcrypto.so.0.9.8
/usr/lib/vmware-tools/lib64/libcrypto.so.0.9.8/libcrypto.so.0.9.8
/usr/local/ssl/lib/libcrypto.a
/usr/local/ssl/lib/pkgconfig/libcrypto.pc
Can someone please help on this or point out any mistake i am doing
# Daniel Roethlisberger tried using the -L flag but that resulted in these errors
gcc -ggdb aes_m.c -Werror -Wall -I /usr/local/ssl/include/ -L /usr/local/ssl/lib -lcrypto -o aes
/usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':
dso_dlfcn.c:(.text+0x2d): undefined reference to `dlopen'
dso_dlfcn.c:(.text+0x43): undefined reference to `dlsym'
dso_dlfcn.c:(.text+0x4d): undefined reference to `dlclose'
/usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_pathbyaddr':
dso_dlfcn.c:(.text+0x8f): undefined reference to `dladdr'
dso_dlfcn.c:(.text+0xe9): undefined reference to `dlerror'
/usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_func':
dso_dlfcn.c:(.text+0x4b1): undefined reference to `dlsym'
dso_dlfcn.c:(.text+0x590): undefined reference to `dlerror'
/usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_bind_var':
dso_dlfcn.c:(.text+0x611): undefined reference to `dlsym'
dso_dlfcn.c:(.text+0x6f0): undefined reference to `dlerror'
/usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_unload':
dso_dlfcn.c:(.text+0x755): undefined reference to `dlclose'
/usr/local/ssl/lib/libcrypto.a(dso_dlfcn.o): In function `dlfcn_load':
dso_dlfcn.c:(.text+0x837): undefined reference to `dlopen'
dso_dlfcn.c:(.text+0x8ae): undefined reference to `dlclose'
dso_dlfcn.c:(.text+0x8f5): undefined reference to `dlerror'
collect2: ld returned 1 exit status
Many thanks
Add -L /usr/local/ssl/lib/ into the GCC command line, before the -lcrypto. Since you are building against the OpenSSL headers under /usr/local/ssl, you also need to link against the actual library under the same prefix (even though you only appear to have a static one installed there, that may or may not be your intention; you may need to properly reinstall your OpenSSL built from source).
(edit) To fix the dlopen() and friends not being found by the linker, add -ldl into the GCC command line. -ldl tells the linker to also link against libdl.so, which is the shared library containing dlopen(), dlsym(), dlclose() etc.; these functions are used by OpenSSL internally and thus, -ldl is an indirect dependency when using -lcrypto (on Linux). Because you are linking to a static version of libcrypto, you need to explicitly link against all indirect dependencies.
If you are not familiar with linking to the proper libraries, I'd suggest you use OpenSSL as installed from your Operating System package manager; it might save you some trouble.
This might be relevant for people who tried to build their own openssl from source and then use it to compile other programs (in my case Git)
During configuration of openssl, add 'shared' option:
./config shared
This will create the required shared library libcrypto.so. You'll find more in the INSTALL file.
Also, if you run into this error during 'make'
"....can not be used when making a shared object
recompile with -fPIC
./config shared -fPIC

Problem with library libpq in Debian

I'm installing postgresql 8.4 in Debian, put program testlibpq.c from http://www.postgresql.org/docs/9.0/interactive/libpq-example.html to directory which have file libpq-fe.h, but after compilation gcc write me
testlibpq.c:(.text+0x4a): undefined reference to `PQconnectdb'
testlibpq.c:(.text+0x5a): undefined reference to `PQstatus'
testlibpq.c:(.text+0x6f): undefined reference to `PQerrorMessage'
testlibpq.c:(.text+0xa9): undefined reference to `PQexec'
testlibpq.c:(.text+0xb9): undefined reference to `PQresultStatus'
testlibpq.c:(.text+0xcf): undefined reference to `PQerrorMessage'
testlibpq.c:(.text+0xf5): undefined reference to `PQclear'
testlibpq.c:(.text+0x10d): undefined reference to `PQclear'
testlibpq.c:(.text+0x121): undefined reference to `PQexec'
... e.t.c. What I must suppose to do to correct work?
Looks like you're not linking the PostgreSQL library. You should be compiling testlibpq.c with something like this:
gcc -o testlibpq testlibpq.c -lpq
The -lpq tells the linker to link against the PostgreSQL library and that's where PQconnectdb and friends come from.
You may need to tell the compiler where to find the libraries and headers as well, if so then something like this should sort that out:
gcc -o testlibpq -I$(pg_config --includedir) -L$(pg_config --libdir) -o testlibpq $(pg_config --libs)

undefined reference to #include <iconv.h>,`libiconv_open',`libiconv',`libiconv_close'

I had a problem with adding reference in my C application. I am unable to add linker reference to "libiconv" in my data compresser project.I have download "libiconv" library but confuse in adding reference which show error in compiling.I'm using Dev C++ for developing my compression apps.
here is the compile log:
Building Makefile: "C:\Dev-Cpp\Examples\eottest\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Examples\eottest\Makefile.win" all
gcc.exe eot.o libeot.o properties.o -o "Project1.exe" -L"C:/Dev-Cpp/lib"
libeot.o(.text+0x19):libeot.c: undefined reference to `libiconv_open'
libeot.o(.text+0x36):libeot.c: undefined reference to `libiconv_close'
libeot.o(.text+0x67):libeot.c: undefined reference to `libiconv'
collect2: ld returned 1 exit status`
You're only specifying an additional library path, not the actual library to link to. Try:
gcc.exe eot.o libeot.o properties.o -o "Project1.exe" -L"C:/Dev-Cpp/lib" -liconv

Resources