Reputation: 360
I'm working on an exercice at school (Mac OSX), and would like to continue at home (Ubuntu 16.04). To do this project I need to use a library : Minilibx (https://github.com/ttshivhula/minilibx) I met a problem when I try to gcc. Here is what gcc tells me : "mlx_init.c:(.text+0x2c) : référence indéfinie vers « XShmQueryVersion »" (undefined reference in english). There are a lot of these error, for each library (even math.h). At school there is no problem when compiling, but at home it doesn't work.
I compile with this line :
gcc -Wall -Werror -Wextra -std=gnu99 -I. -g -lm -lGLEW -lglfw -lGL ${OBJS} minilibx/libmlx_Linux.a -o ${NAME}
With :
OBJS = ${SRC:%.c=%.o}
SRC = cub3d.c ${addprefix ${GNL_PATH}, $(GNL_FILE)} ${addprefix ${LIB_PATH}, $(LIB_FILE)}
GNL and LIB are my two personnal librairies.
I tried to change my way of compiling but I cannot find what's wrong.
Upvotes: 2
Views: 7004
Reputation: 754090
Rule of Thumb: On a linking command line, libraries go after object files, not before them.
If you put the libraries before the object files, some systems will not record the symbols defined by the libraries because there were no outstanding undefined references to any of the functions in the libraries as the command line was processed left-to-right, so when the object files are scanned, the libraries are not rescanned.
Change this:
gcc -Wall -Werror -Wextra -std=gnu99 -I. -g -lm -lGLEW -lglfw -lGL ${OBJS} \
minilibx/libmlx_Linux.a -o ${NAME}
to:
gcc -Wall -Werror -Wextra -std=gnu99 -I. -g ${OBJS} -lm -lGLEW -lglfw -lGL \
minilibx/libmlx_Linux.a -o ${NAME}
Now you stand a chance of the functions being linked. On a Mac, using -lm
is unnecessary but harmless; the maths functions are in the main system library. Other systems, notably Linux, still have a separate maths library — mainly for hysterical raisins (…err, historical reasons…).
I'd not be surprised to find that you need to place the minilibx/libmlx_Linux.a
argument earlier in the command line too — immediately after the ${OBJS}
.
Upvotes: 3