Undefined reference to _gzopen etc

2019-08-08 09:22发布

问题:

Hi I have a program in c built on a non Windows machine(ubuntu machine I think but could be mac os as well).

I did a quick look around and tried cygwin (installed cygwin, used the terminal to goto the directory containing the c program and called make.)

This yields:

$ make
g++ -lz -Wextra -O3 -o bin/lda src/lda_main.o src/data.o src/lda.o c/lda_init.o
   src/lda_io.o src/lda_sampler.o src/opts.o

src/data.o:data.cpp:(.text+0x5d): undefined reference to `_gzopen'
src/data.o:data.cpp:(.text+0x9e): undefined reference to `_gzgets'
src/data.o:data.cpp:(.text+0x138): undefined reference to `_gzgets'
src/data.o:data.cpp:(.text+0x1f0): undefined reference to `_gzgets'
src/data.o:data.cpp:(.text+0x257): undefined reference to `_gzclose'
src/data.o:data.cpp:(.text+0x3cb): undefined reference to `_gzopen'
src/data.o:data.cpp:(.text+0x498): undefined reference to `_gzgets'
src/data.o:data.cpp:(.text+0x4b4): undefined reference to `_gzclose'

/usr/lib/gcc/i686-pc-cygwin/4.5.3/../../../../i686-pc-cygwin/bin/ld: src/data.o:
   bad reloc address 0x1c in section `.eh_frame'
collect2: ld returned 1 exit status
Makefile:16: recipe for target `lda' failed
make: *** [lda] Error 1

I do have gcc g++ installed as well as zlib.

I then tried mingw32 using mingw32-make in the right directory. returns an error faster than eye can see: ld.exe: cannot find -lz

Any suggestions? Please keep in mind that I am an economist by training and we know how useful that is :-). Simple will go far with me.

回答1:

Change the makefile so that -lz comes after the list of object files.

With the command line:

g++ -lz -Wextra -O3 -o bin/lda src/lda_main.o src/data.o src/lda.o src/lda_init.o
   src/lda_io.o src/lda_sampler.o src/opts.o

The library will not be searched for external references that the various object files need. If you arrange it so the command looks like:

g++ -Wextra -O3 -o bin/lda src/lda_main.o src/data.o src/lda.o src/lda_init.o
   src/lda_io.o src/lda_sampler.o src/opts.o -lz

Then libz will be searched for any external references that the object files need.