Makefile compile error not applying -std=c99 - c

I have been given supposedly working code that I just need to modify, however I can't actually compile the program with the supplied makefile given below.
INCLUDE_PATH = -I/usr/X11/include -I$(BLDR_FREEGLUT_INCLUDE_PATH)
LIBRARY_PATH = -L/usr/X11/lib -L$(BLDR_FREEGLUT_LIB_PATH)
CCFLAGS = -std=c99
LDFLAGS = -lGL -lGLU -lglut -lX11 -lpthread
GCC = gcc
APP = nbody
all: $(APP)
#echo Make done
clean:
#rm -f *.o $(APP)
nbody: nbody.c
$(GCC) nbody.c -o nbody $(INCLUDE_PATH) $(CCFLAGS) $(LIBRARY_PATH) $(LDFLAGS)
The first output line of the make command gives below.
gcc nbody.c -o nbody -I/usr/X11/include -I -std=c99 -L/usr/X11/lib -L -lGL -lGLU -lglut -lX11 -lpthread
However below that are errors saying that certain things are only "allowed in C99 mode", which I don't understand since the flag for c99 seems to be specified correctly. To troubleshoot this I have tried changing the location of the $(CCFLAGS) value without any success.
How can I modify this makefile so the program complies?

-I -std=c99 says compiler to add directory named -std=c99 to include search paths. This is not what you wanted. Judging by presented fragment, $(BLDR_FREEGLUT_INCLUDE_PATH) is empty - while it shouldn't be. Fix that variable's value and things will go well.

Related

usr/x86_64-unknown-linux-gnu/bin/ld: cannot find -lbsd

A little background for the problem I am having:
I just migrated from Ubuntu Focal Fossa to Mint Cinnamon keeping my home in a partition
libbsd is installed and can be used to compile some test code (I did a "Hello world" test using strlcpy in the main to verify that libbsd was usable)
Basically in order to use this project I have to use a library provided by my school (which compiled without problems in my last system) which is has a testing script that is going to run a test with its own Makefile:
INC=%%%%
INCLIB=$(INC)/../lib
UNAME := $(shell uname)
CFLAGS= -I$(INC) -O3 -I.. -g
NAME= mlx-test
SRC = main.c
OBJ = $(SRC:%.c=%.o)
LFLAGS = -L.. -lmlx -L$(INCLIB) -lXext -lX11 -lm
ifeq ($(UNAME), Darwin)
# mac
CC = clang
else ifeq ($(UNAME), FreeBSD)
# FreeBSD
CC = clang
else
#Linux and others...
CC = gcc
LFLAGS += -lbsd
endif
all: $(NAME)
$(NAME): $(OBJ)
$(CC) -o $(NAME) $(OBJ) $(LFLAGS)
clean:
rm -f $(NAME) $(OBJ) *~ core *.core
re: clean all
mlx would be the name of the library. For who is not familiar with this syntax (I was not) INC=%%%% is going to expand to /usr/include.
The command to compile the test binary expands to:
$ gcc -o mlx-test main.o -L.. -lmlx -L/usr/include/../lib -lXext -lX11 -lm -lbsd
which generates the error
/usr/lib/gcc/x86_64-unknown-linux-gnu/11.3.0/../../../../x86_64-unknown-linux-gnu/bin/ld: cannot find -lbsd
(that path expands to usr/x86_64-unknown-linux-gnu/bin/ld)
Now, this kinda looks like a gcc configuration, in fact I saw something similar in the output of the command gcc --version -v, but only with x86_64-linux-gnu so without the "unkown" part. Also, one of the weird things is that the path that appears in the error actually does not exist in my system.
I already tried reinstalling gcc but with no different result.
I hope the description was clear enough, please let me know if you may need any other detail that I did not think of.

MAKEFILE using Library : Linker command failed

Hello I can't make my Makefile working with
$(CC) $(CFLAGS) $(INC) $(OBJS) $(MLX_LNK) -o $(NAME).
got a
clang -O3 -Wall -Wextra -Werror -I -I cub3d.h src/cub3d.o src/checks/argvcheck.o src/checks/parse_map.o src/libft/basics.o src/libft/basics_bis.o src/libft/get_next_line.o src/utils/errors.o -L minilibx_opengl -lmlx -framework OpenGL -framework AppKit -o cub3D
clang: error: cannot specify -o when generating multiple output files
make: *** [cub3D] Error 1
The command on terminal I do is "make test1"
I also tried with $(CC) $(CFLAGS) -I $(HEADER) $(OBJS) $(MLX_LNK) -o $(NAME).
but got
Compiling...
clang -O3 -Wall -Wextra -Werror -I cub3d.h src/cub3d.o src/checks/argvcheck.o src/checks/parse_map.o src/libft/basics.o src/libft/basics_bis.o src/libft/get_next_line.o src/utils/errors.o -L minilibx_opengl -lmlx -framework OpenGL -framework AppKit -o cub3D
Undefined symbols for architecture x86_64:
"_init_cube", referenced from:
_init_game in cub3d.o
"_write_errors", referenced from:
_verify_line in argvcheck.o
_ft_parse_cub in argvcheck.o
_my_get_next_line in get_next_line.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Many thanks for your help
Makefile here :
NAME = cub3D
HEADER = cub3d.h
CC = clang
CFLAGS = -O3 -Wall -Wextra -Werror
INC = -I $(MLX-DIR) -I $(HEADER)
MLX_DIR = minilibx_opengl
MLX_LNK = -L $(MLX_DIR) -lmlx -framework OpenGL -framework AppKit
SRCS = src/main.c \
src/checks/argvcheck.c \
src/libft/basics.c \
src/libft/get_next_line.c \
src/utils/errors.c \
OBJS = $(SRCS.c=.o)
all: $(NAME)
mlx: $(MLX-DIR)
#echo "\033[34m-= Making libX.a... =-"
#make -C $(MLX_DIR)
$(NAME): ${OBJS} mlx
$(CC) $(CFLAGS) $(INC) $(OBJS) $(MLX_LNK) -o $(NAME)
test1: $(NAME)
$(NAME) ; ./a.out maps/test1.cub
clean:
#echo "\033[0;31mCleaning..."
rm -f $(OBJS)
# + $(B_OBJ)
# + rm -f bitmap.bmp
#echo "\033[0m"
fclean: clean
#echo "\033[34m-= Cleaning mlx... =-"
#make clean -C $(MLX_DIR)
#echo "\033[0;31mRemoving executable..."
rm -f $(NAME)
#echo "\033[0m"
re: fclean all
.PHONY: all clean fclean re
````
Well, first of all this is wrong:
OBJS = $(SRCS.c=.o)
You're missing a : here, it should be $(SRCS:.c=.o) As a result, OBJS will be empty.
Next, this is not causing you problems at the moment but is not right: you should always use $(MAKE) never a raw command like make when invoking a sub-make.
Finally, the way you've written your question by embedding results into the middle of the makefile makes it very hard to read. Please put the makefile first, then separate sections for different attempts at recipes. And you need to include the command line that make printed out (cut and paste the exact line please!) for us to see what the command being run it (with all variables expanded). Typically it becomes VERY obvious what the problem is if you look at that.
For example in this case you'd see that there are actually no object files in the link line, so it should be clear that the $(OBJS) variable is not being set properly.
EDIT
OK, thanks for showing the command line. Now, you should look at it carefully and you will see your problem :). Look at this here:
clang -O3 -Wall -Wextra -Werror -I -I cub3d.h src/cub3d.o ...
Does that look right to you? Look specifically at -I -I cub3d.h... does that seem right?
What happens is that the compiler expects a pathname to come after the -I and there isn't one, so it treats the second -I as the pathname. Then the file cub3d.h is treated as a source file, and you can't link a source file with object files.
So why does this look like this? Look at your makefile:
INC = -I $(MLX-DIR) -I $(HEADER)
so the missing thing is where $(MLX-DIR) goes. What is that variable? Well you have this:
MLX_DIR = minilibx_opengl
but this is not the same thing because it uses an underscore whereas the reference uses a dash. So, make them the same.
Then you'll see that it's not valid to put a file as an argument to -I. That takes a directory to search for header files. If you want to include the header you have to add #include "cub3d.h" in your source code, not add it to the compile line.

Makefile: "undefined reference to cos"

I have just started learning about makefile files. I created a program that consists of two functions and wanted to use a makefile to put it all together. This is my file:
#Makefile
all: main
main: main.o find_root.o
clang -o main main.o find_root.o
main.o: main.c
clang -c -Wall --pedantic -std=c11 main.c -lm
find_root.o: find_root.c
clang -c -Wall --pedantic -std=c11 find_root.c -lm
clean: rm -f main *.o*
However, when I run this, I get an error - "undefined reference to cos". I am using the cosine functions in my program, but I have already linked the library to the compilation of those two files. I thought about adding "-lm" to the first clang option as well. This led to no errors, but it made a warning instead - saying that "-lm linker is unused". What should I change in my file?
The "-lm" is a linker option but you have only included it in your compilation rule (main.o: main.c). You need to include it in your linker rule (main: main.o find_root.o).
As it stand the -lm option is ignored during compilation and missing during linking.
The linker flags aren't used when compiling, but when linking, so the command for the main rule should have -lm, rather than the command for the *.o files.
Better would be just to set the appropriate variables, and let Make use its built-in rules:
#Makefile
LDLIBS += -lm
CFLAGS += -Wall --pedantic -std=c11
C = clang
all: main
main: main.o find_root.o
$(LINK.c) $^ $(LDLIBS) -o $#
clean:
$(RM) main *.o *~

Unable to link math lib using a makefile [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Linker errors when compiling against glib…?
Okay, I know this may be a duplicate, but I can't find any other answer to my problem.
I am trying to install Pintos and when I run 'make' in the src/utils directory, I get the error that there is an undefined reference to 'floor'. I checked the makefile and here's what I got:
all: setitimer-helper squish-pty squish-unix
# 2207718881418
CC = gcc
CFLAGS = -Wall -W
LDFLAGS = -lm
setitimer-helper: setitimer-helper.o
squish-pty: squish-pty.o
squish-unix: squish-unix.o
clean:
rm -f *.o setitimer-helper squish-pty squish-unix
I tried adding LIBS = -lm but that didn't help.
Output of make:
gcc -lm setitimer-helper.o -o setitimer-helper
setitimer-helper.o: In function `main':
setitimer-helper.c:(.text+0xbb): undefined reference to `floor'
collect2: ld returned 1 exit status
make: *** [setitimer-helper] Error 1
Any solutions to this dilemma?
Your original makefile defines a bunch of variables
CC = gcc
# etc
and lists some dependencies
setitimer-helper: setitimer-helper.o
# etc
but doesn't have any recipes giving the exact commands to be used to remake the targets, except for the clean rule. This means that built-in implicit rules will be used; e.g., to link setitimer-helper the following built-in rule will be used:
$(CC) $(LDFLAGS) $^ $(LDLIBS) -o $#
For setitemer-helper, the automatic variables are filled in using the relevant dependency:
$(CC) $(LDFLAGS) setitimer-helper.o $(LDLIBS) -o setitimer-helper
and from this you can see how the remaining variables -- $(CC), $(LDFLAGS), and $(LDLIBS) -- were filled in to give the output of make that you saw.
As various people have noted, you need to make sure that -lm goes at the end of the link command so that it can be used to satisfy references to library functions like floor(). At the moment, your makefile sets $(LDFLAGS) to -lm, but that variable is used at the start of the link command.
The conventional variables are set up in this built-in rule so that LDFLAGS can be used for options (a.k.a. "flags") that (historically) need to be at the start of the link command, and LDLIBS can be used for libraries that need to be specified after the *.o object files.
So to fix this in terms of the makefile you are using, you need to remove -lm from the LDFLAGS variable that is defined, and instead add another variable definition for LDLIBS:
LDLIBS = -lm
(I'm oversummarising slightly: the built-in rule also contains $(TARGET_ARCH) and $(LOADLIBES), but those aren't of interest here.)
It's compiled in wrong order, the way to proceed is:
CC = gcc
CFLAGS = -Wall -W
LDFLAGS = -lm
myprog: myprog.o more_code.o
${CC} ${CFLAGS} myprog.o more_code.o ${LDFLAGS} -o myprog
myprog.o: myprog.c
${CC} ${CFLAGS} -c myprog.c
more_code.o: more_code.c
${CC} ${CFLAGS} -c more_code.c
clean:
\rm myprog.o more_code.o myprog
More info: http://www.physics.utah.edu/~p5720/rsrc/make.html
Can you show me in terms of the original makefile?
I can try :)
CC = gcc
CFLAGS = -Wall -W
LDFLAGS = -lm
OBJECTS = setitimer-helper.o squish-pty.o squish-unix.o
all: setitimer-helper
setitimer-helper: $(OBJECTS)
${CC} ${CFLAGS} $(OBJECTS) ${LDFLAGS} -o setitimer-helper
setitimer-helper.o: setitimer-helper.c
${CC} ${CFLAGS} -c setitimer-helper.c
squish-pty.o: squish-pty.c
${CC} ${CFLAGS} -c squish-pty.c
squish-unix.o: squish-unix.c
${CC} ${CFLAGS} -c squish-unix.c
And since you are new to Makefile, it's a good idea to add -Wextra -pedantic to CFLAGS

usage: cc [ options] files. Use 'cc -flags' for details

I have a makefile. When I run it on Unix I get this error:
cc -I/opt/oracle/product/9.2.0/rdbms/demo -I/opt/oracle/product/9.2.0/rdbms/public \
-I/opt/oracle/product/9.2.0/plsql/public \
-I/opt/oracle/product/9.2.0/network/public -I../common -I../include -I. \
-L/opt/oracle/product/9.2.0/lib -L/opt/oracle/product/9.2.0/rdbms/lib -L../../lib \
-g -errwarn=%all -Xt -lclntsh -ldl -Bstatic -lclient9 -lvsn9 -lcommon9 -lgeneric9 \
-lmm -lcore9 -lnls9 -lwssmbx -ldes -lnsl -lsocket -lgen -lm -o bessToWss
usage: cc [ options] files. Use 'cc -flags' for details
*** Error code 1
What does "usage: cc [ options] files. Use 'cc -flags' for details" mean?
I'm not sure why I get the error because I do use cc -flags:
$(TARGET_DIR)/bessToWss: $(INTFOBJS)
cc $(CFLAGS) $(INTFOBJS) $(OCISHAREDLIBS) -o $#
EDIT: Adding my entire makefile
ORACLE_HOME=/opt/oracle/product/9.2.0
COMMON_SRC=../common
BNS_INCLUDE=../include
LIBHOME=$(ORACLE_HOME)/lib/
RDBMSLIB=$(ORACLE_HOME)/rdbms/lib/
WSSLIBS =-lwssmbx -ldes
LLIBRDBMS_CLT =-lclient9 -lvsn9 -lcommon9 -lgeneric9 -lmm
LLIBCLNTSH =-lclntsh -ldl
CORELIBS =-lcore9 -lnls9
LDLIBS =-lnsl -lsocket -lgen -lm
EXSYSLIBS =-R $(ORACLE_HOME)/lib
STATICTTLIBS =$(LLIBRDBMS_CLT) $(CORELIBS) $(WSSLIBS) $(LDLIBS)
OCISHAREDLIBS =$(LLIBCLNTSH) -Bstatic $(STATICTTLIBS)
LDFLAGS =-L$(ORACLE_HOME)/lib -L$(ORACLE_HOME)/rdbms/lib -L../../lib
INCLUDE =-I$(ORACLE_HOME)/rdbms/demo -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/plsql/public -I$(ORACLE_HOME)/network/public -I$(COMMON_SRC) -I$(BNS_INCLUDE) -I.
CFLAGS =$(INCLUDE) $(LDFLAGS) -g -errwarn=%all -Xt
BESSOBJS=bessToWss.o
COMMONLIST=$(COMMON_SRC)/oracle.c \
$(COMMON_SRC)/logger.c
INTFOBJS=$(BESSOBJS) $(COMMONLIST)
$(TARGET_DIR)/bessToWss: $(INTFOBJS)
cc $(CFLAGS) $(INTFOBJS) $(OCISHAREDLIBS) -o $#
clean:
$(RM) *.o
It means you have an invalid compiler option on the command line. It might be that you are using the 'wrong' compiler (maybe GCC instead of Sun's compiler, for example). The probable problem options I see are:
-Xt
-errwarn=%all
and maybe (but probably not)
-Bstatic
The others would not lead to usage errors like that.
(NB: It would help to identify the machine and the compiler you are using, and the compiler Oracle expects you to use.)
It looks like you don't have any object files to actually link together. The $(INTFOBJS) variable is likely empty. Along with potentially having the wrong flags, this would also cause it to fail.

Resources