GCC Makefile Error: -std=c99 and -lm - makefile

I've been trying for quite long to adapt my makefile to work with -std=c99 and -lm. The code above work fine without -std=c99, but doesn't compile when I have this flag. I tried this question, as it's the same problem I'm facing, but I can't make it work.
CC = gcc -std=c99
PARAMETERS = -g -Wall
LIB = -lm
jacobi: jacobi.o
$(CC) $(PARAMETERS) jacobi.o -o jacobi $(LIB)
jacobi.o: jacobi.c
$(CC) $(PARAMETERS) -c jacobi.c -o jacobi.o $(LIB)
clean:
rm -f *.o jacobi *~
Could someone kindly help me? Thanks!

Related

Makefile - include files not found when building with multiple source files

I have a make file:
# Makefile
CC = gcc
SRC = pixel.c
TARGET = pixel.exe
OBJECTS = bmp.o pixel.o
#CFLAGS_W = -Wall -Wl,-subsystem,windows
CFLAGS_W = -Wall
LFLAGS_W = -lSDL2
INCS_W = -I.\SDL2\include
LIBS_W = -L.\SDL2\lib\x64
all: $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -o $(TARGET) $(OBJECTS)
it will build bmp.c, but when it tries to build pixel.c I get the error:
pixel.c:6:10: fatal error: SDL.h: No such file or directory
however, pixel.c has been building fine all the time I was just building it on its own (before I added bmp.c), using the command:
all:
$(CC) $(SRC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -o $(TARGET)
I really struggle with makefiles because I just don't use them enough, but this doesn't really feel like a "make" problem.
I'm using mingw32-make and gcc in a windows sandbox.
EDIT:
I have added
.c.o:
$(CC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -c $< -o $#
to my makefile, which gets me a step further (I think), but now complains that all of the SDL functions are undefined
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel>mingw32-make
gcc -I./SDL2/include -L./SDL2/lib/x64 -Wall -lSDL2 -g -c bmp.c -o bmp.o
gcc -I./SDL2/include -L./SDL2/lib/x64 -Wall -lSDL2 -g -c pixel.c -o pixel.o
gcc -I./SDL2/include -L./SDL2/lib/x64 -Wall -lSDL2 -g -o pixel.exe bmp.o pixel.o
pixel.o: In function `main':
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:119: undefined reference to `SDL_Init'
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:121: undefined reference to `SDL_GetError'
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:126: undefined reference to `SDL_CreateWindow'
C:\Users\WDAGUtilityAccount\Desktop\Projects\Pixel/pixel.c:131: undefined reference to `SDL_GetError'
...
I can get the whole project to compile if I just do this on its own:
SRC = bmp.c pixel.c
...
test:
$(CC) $(SRC) $(INCS_W) $(LIBS_W) $(CFLAGS_W) $(LFLAGS_W) -g -o $(TARGET)
which is fine, it'll do, but it rebuilds everything every time, and I'd like to know why it isn't working anyway :)

compiler error gcc-8: error: unrecognized command line option '-no-pie'

I am trying to compile using this makefile but got this error. I can compile using school's linux computer with gcc 6.3. I tried using my MacOS mojave using a few different version of gcc from homebrew (gcc-8, gcc-4.9, gcc-6) but I get this error consistently.
CC = gcc-8
OPT = -O3 -g
LIBS = -lz -lcvp -lz
FLAGS = -std=c++11 -L. $(LIBS) $(OPT) -no-pie
OBJ = myprogram.o
DEPS = cvp.h myprogram.h
all: cvp
cvp: $(OBJ)
$(CC) $(FLAGS) -o $# $^
%.o: %.cc $(DEPS)
$(CC) $(FLAGS) -c -o $# $<
.PHONY: clean
clean:
rm -f *.o cvp
Going off information found here: https://github.com/xd009642/tarpaulin/issues/7#issuecomment-317180523
The problem may very well be related to how gcc is built: "Builds of gcc that don't have the --enable-default-pie flag set at compile time because they are too old or have the --disable-default-pie flag just don't have the -no-pie linker flag"
You might need to update or recompile gcc for that environment to be able to use that flag if important to you.

Why does make delete my temporary files?

I have a simple Makefile,
.PHONY: clean
PROGRAMS=$(patsubst main%.cpp,example%,$(wildcard main*.cpp))
all: ${PROGRAMS}
GCCVERSION=$(shell gcc -dumpversion)
GLCFLAGS=$(shell pkg-config --cflags gl)
CPPFLAGS=-Wall -O2 ${GLCFLAGS}
ifeq "${GCCVERSION}" "4.5.2"
CXXFLAGS=-std=c++0x
else
CXXFLAGS=-std=c++11
endif
GLLIBS=$(shell pkg-config --libs gl)
LIBS=${GLLIBS} -lglut
example%: main%.o shaders.o fileutils.o
${CXX} $^ ${LIBS} -o $#
clean:
rm -f *.o ${PROGRAMS}
But when I executed it, it delete the *.o files as last command. I don't know why:
$ make
g++ -std=c++11 -Wall -O2 -I/usr/include/libdrm -c -o main01.o main01.cpp
g++ -std=c++11 -Wall -O2 -I/usr/include/libdrm -c -o shaders.o shaders.cpp
g++ -std=c++11 -Wall -O2 -I/usr/include/libdrm -c -o fileutils.o fileutils.cpp
g++ main01.o shaders.o fileutils.o -lGL -lglut -o example01
rm main01.o fileutils.o shaders.o
Is there anything wrong with my Makefile?
Intermediate files are deleted by design: see Chained Rules in GNU make manual.
Use .SECONDARY or .PRECIOUS targets to keep your precioussss temp files.
Just to clarify the previous response, you need to add a special rule like
.PRECIOUS: myfile.o

How to use gcc to compile multiple files

I have several files needed to be compiled.
here is the command. the sample_client.c dependent on the lsp.o. Now I changed the lsp.c and lsp.h. How can I compile to get this change effective to lsp.o?
the main function is in the sample_client.c, lsp.c does not have a main function.
gcc -g -I/usr/include -g sample_client.c lsp.o lspmessage.pb-c.o -o sample_client -L/usr/lib -lprotobuf-c
Here is my makefile,
CC = gcc
TARGET = sample_client sample_server
CFLAGS += -g -I/usr/include
LDFLAGS += -g -lprotobuf-c -L/usr/lib
all: $(TARGET)
$(TARGET): lsp.o lspmessage.pb-c.o
%.o: %.c
$(CC) -c $(CFLAGS) $< -o $#
clean:
rm -f *.o
rm -f $(TARGET)
However, the lprotobuf-c can not be correctly linked.
run make -f Makefile
I can get the following,
lspmessage.pb-c.o: In function `lspmessage__get_packed_size':
...: undefined reference to `protobuf_c_message_get_packed_size'
lspmessage.pb-c.o: In function `lspmessage__pack':
...: undefined reference to `protobuf_c_message_pack'
I know that I can run this command,
gcc -g -I/usr/include -g sample_client.c lsp.o lspmessage.pb-c.o -o sample_client -L/usr/lib -lprotobuf-c
But what if I change the lsp.c and lsp.h ?
It looks to me like your LDFLAGS isn't correct. Try the following:
LDFLAGS += -L/usr/lib -lprotobuf-c
It looks like you had the directory -L and the library out of order.
Also, I removed the additional call to -g for you.

Compiling error

I downloaded someone's source code for a program and i needed to make some changes.
Now i want to compile it but it doesn't seem to work.
PROGS = isotociso
COMMON = tools.o bn.o ec.o wiidisc.o rijndael.o
DEFINES = -DLARGE_FILES -D_FILE_OFFSET_BITS=64
LIBS = C:/Dev-Cpp/lib/libwsock32.a C:/Dev-Cpp/lib/libcrypto.a C:/Dev-Cpp/lib/libcomdlg32.a
CC = gcc
#CFLAGS = -Wall -W -Os -Ilibwbfs -I.
CFLAGS = -Wall -m32 -W -ggdb -Ilibwbfs -I.
LDFLAGS = -m32 -static
VPATH+=libwbfs
OBJS = $(patsubst %,%.o,$(PROGS)) $(COMMON)
all: $(PROGS)
$(PROGS): %: %.o $(COMMON) Makefile
$(CC) $(CFLAGS) $(LDFLAGS) $< $(COMMON) $(LIBS) -o $#
$(OBJS): %.o: %.c tools.h Makefile
$(CC) $(CFLAGS) $(DEFINES) -c $< -o $#
clean:
-rm -f $(OBJS) $(PROGS)
Output
C:\Users\Panda\Desktop\uloader_v4.1\src\isotociso\src>make
gcc -Wall -m32 -W -ggdb -Ilibwbfs -I. -DLARGE_FILES -D_FILE_OFFSET_BITS=64 -c i
sotociso.c -o isotociso.o
process_begin: CreateProcess((null), gcc -Wall -m32 -W -ggdb -Ilibwbfs -I. -DLAR
GE_FILES -D_FILE_OFFSET_BITS=64 -c isotociso.c -o isotociso.o, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [isotociso.o] Error 2
What would be the problem?
Looks to me as if gcc is not in your PATH.
It also looks like you need MinGW to get the libraries.
I am no expert in C(++) development under Windows, but my interpretation would be that it can't find the compiler itself. What development environment are you using?
It looks like it can't find a file. Are you sure you have all the required source files?

Resources