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
Related
The title might be confusing, but that's what happened...
Here's my Makefile.
all:mystdio libmystdio.a main.o myscanf.o myprintf.o
mystdio: main.o libmystdio.a
clang++ -o mystdio main.o -L. -lmystdio -Wall -g -std=c++17
libmystdio.a: myscanf.o myprintf.o
ar cr libmystdio.a myprintf.o myscanf.o
main.o: main.cpp mystdio.hpp
clang++ -c main.cpp -Wall -g -std=c++17
myscanf.o: myscanf.cpp mystdio.hpp utilities.hpp
clang++ -c myscanf.cpp -Wall -g -std=c++17
myprintf.o: myprintf.cpp mystdio.hpp utilities.hpp
clang++ -c myprintf.cpp -Wall -g -std=c++17
clean:
rm -rf mystdio *.o *.a *.a
When I made changes in myscanf.cpp or myprintf.cpp and then make file, the output showed that it had been compiled and mystdio is updated. But actually nothing happened.
For example, let's assume I add printf(":)\n"); at the beginning of a function in myscanf.cpp. Then I call the function in main.cpp. After making file, output in the terminal tells me that myscanf.o and related files were recompiled. But when the program runs, no :) is outputed.
However, when I add printf(":)\n"); in the main function and make file, it was outputed. This did confuse me. I wonder how can I solve it.
command used:
make && ./mystdio
When I changed something in myprintf.cpp and make again, here's output from the terminal.
clang++ -c myprintf.cpp -Wall -g -std=c++17
ar cr libmystdio.a myprintf.o myscanf.o
clang++ -o mystdio main.o -L. -lmystdio -Wall -g -std=c++17
By the way, if I use make clean before make && ./mystdio, it'll work correctly, but of course that's not what "make" is designed for...
Thanks for your help in advance!
I have some code that I use a (old) Mersenne Twister header file with to get a good pseudo-random number generator.
The problem is that that code uses the register keyword which (as far as I understand) was deprecated in c++11 and throws an error with c++17. I believe I am using the latter in the version of clang I'm using on Mac OSX (10.14.6; Mojave).
The Makefile I am using is as thus:
BUILDDIR=$(TOPDIR)/bin
INCDIROUT=$(BUILDDIR)/include
LIBDIROUT=$(BUILDDIR)/lib
INCDIR=inc/
SRCDIR=src/
INCFLAG= -IMersenne -Iinc
LIBFLAG= -L. -L$(LIBDIROUT)
#need to use an older version of gcc b/c of Mersenne Twister using the deprecated `register` keyword
#https://github.com/danini/graph-cut-ransac/issues/23
CXX=clang++
#CXXFLAGS=-g -std=c++11 -Wall -pedantic
CXXFLAGS=-fpermissive -std=c++98
#trick for getting the git version in the code
GIT_VERSION = $(shell sh -c 'git describe --abbrev=4 --always')
CFLAGS += -D__GIT_VERSION=\"$(GIT_VERSION)\"
RUN_SCRIPT := $(shell mkdir -p 'bin/lib')
all: $(BUILDDIR)/realizeCascades $(LIBDIROUT)/rootUtil.o $(LIBDIROUT)/edepmath.o $(LIBDIROUT)/cascadeProd.o $(LIBDIROUT)/isotope_info.o $(LIBDIROUT)/weisskopf.o $(LIBDIROUT)/lindhard.o $(LIBDIROUT)/libncap.so
$(LIBDIROUT)/isotope_info.o: $(SRCDIR)/isotope_info.c $(INCDIR)/isotope_info.h
$(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/isotope_info.c `root-config --cflags --glibs` $(LIBFLAG)
mv isotope_info.o $(LIBDIROUT)/
$(LIBDIROUT)/rootUtil.o: $(SRCDIR)/rootUtil.c $(INCDIR)/rootUtil.h
$(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/rootUtil.c `root-config --cflags --glibs` $(LIBFLAG)
mv rootUtil.o $(LIBDIROUT)/
$(LIBDIROUT)/edepmath.o: $(SRCDIR)/edepmath.c $(INCDIR)/edepmath.h
$(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/edepmath.c `root-config --cflags --glibs` $(LIBFLAG)
mv edepmath.o $(LIBDIROUT)/
$(LIBDIROUT)/weisskopf.o: $(SRCDIR)/weisskopf.c $(INCDIR)/weisskopf.h
$(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/weisskopf.c `root-config --cflags --glibs` $(LIBFLAG)
mv weisskopf.o $(LIBDIROUT)/
$(LIBDIROUT)/lindhard.o: $(SRCDIR)/lindhard.c $(INCDIR)/lindhard.h
$(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/lindhard.c `root-config --cflags --glibs` $(LIBFLAG)
mv lindhard.o $(LIBDIROUT)/
$(LIBDIROUT)/cascadeProd.o: $(SRCDIR)/cascadeProd.c $(INCDIR)/cascadeProd.h
$(CXX) -std=c++98 -fPIC -c $(CFLAGS) $(INCFLAG) $(SRCDIR)/cascadeProd.c `root-config --cflags --glibs` $(LIBFLAG)
mv cascadeProd.o $(LIBDIROUT)/
$(LIBDIROUT)/libncap.so: $(LIBDIROUT)/isotope_info.o $(LIBDIROUT)/weisskopf.o $(LIBDIROUT)/lindhard.o $(LIBDIROUT)/cascadeProd.o $(LIBDIROUT)/edepmath.o $(LIBDIROUT)/rootUtil.o
$(CXX) -std=c++98 -fPIC -shared $(LIBDIROUT)/lindhard.o $(LIBDIROUT)/weisskopf.o $(LIBDIROUT)/isotope_info.o $(LIBDIROUT)/cascadeProd.o $(LIBDIROUT)/edepmath.o $(LIBDIROUT)/rootUtil.o `root-config --cflags --glibs` -o $(LIBDIROUT)/libncap.so
$(BUILDDIR)/realizeCascades: $(LIBDIROUT)/libncap.so $(BUILDDIR)/realizeCascades.cpp
$(CXX) -std=c++98 -fPIC -Wl,-rpath=$(LIBDIROUT) $(CFLAGS) $(INCFLAG) $(LIBFLAG) $(BUILDDIR)/realizeCascades.cpp `root-config --cflags --glibs` -lncap -o $(BUILDDIR)/realizeCascades
clean:
rm -f $(LIBDIROUT)/*.o
rm -f $(LIBDIROUT)/*.so
rm -f $(BUILDDIR)/realizeCascades
rm -f *.o
rm -f *.so
rm -rf $(LIBDIROUT)
despite using the -std=c++11 or -std=c++98 flags once the makefile begins to compile lindhard.c as thus:
clang++ -std=c++98 -fPIC -c -D__GIT_VERSION=\"v1.0.6-73-g01bc\"
-IMersenne -Iinc src//lindhard.c `root-config --cflags --glibs` -L. -L/Users/villaa/nrCascadeSim/bin/lib
An error is thrown for each of the many times the register keyword is used:
Mersenne/MersenneTwister.hh:187:2: error: ISO C++17 does not allow 'register' storage class specifier [-Wregister]
register uint32 s1;
^~~~~~~~~
I am struggling to find out the reason for this. Is the -std flag not being applied to the pre-processing of include files? Am I going about this the wrong way?
#MadScientist and #idz were correct above. The key was that the root libraries when using root-config --cflags --glibs added the following:
-stdlib=libc++ -pthread -std=c++17 -m64 -I/usr/local/Cellar/root/6.24.04/include/root -L/usr/local/Cellar/root/6.24.04/lib/root -lGui -lCore -lImt -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lROOTVecOps -lTree -lTreePlayer -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lMultiProc -lROOTDataFrame -stdlib=libc++ -lpthread -lm -ldl
This came after my flag -std=c++98 and therefore overrode it.
The solution here was to move the -std=c++98 to after the root-config line.
I suppose it was technically possible that some of the ROOT code that I was using required c++17 and then I'm not sure if this could have been resolved.
But as it turns out I was using a portion of the ROOT code base that was older and likely (though I'm still not sure) didn't have any upgrades requiring c++17.
If it did I suppose my only choice would have been to update my incompatible library, Mersenne, to be compatible with c++17
PS: I know that Mersenne Twister now is implemented in C++ standard code, but for reasons I had preferred to use my old implementation.
The below makefile does not have target for making OBJECTS but still the make is forming the rule and makes the OBJECTS and makes the final all.exe. I am not sure how does it work. Does make have mechanism to create rules if they are not available in the makefile for the dependencies?
SOURCE=$(wildcard Source/\*.cpp)
TEST=$(wildcard Source/Tests/\*.cpp)
OBJECTS=$(SOURCE:.cpp=.o) $(TEST:.cpp=.o)
CXXFLAGS= -O3 -fstack-usage -MMD
all:$(OBJECTS)
g++ $(CXXFLAGS) -o $# $(OBJECTS)
print::
#echo $(SOURCE)
#echo $(OBJECTS)
#echo $(TEST)
clean:
rm -rf Source/*.o Source/Tests/*.o *.exe
Console Output:
$ make
g++ -O3 -fstack-usage -MMD -c -o Source/main.o Source/main.cpp
g++ -O3 -fstack-usage -MMD -c -o Source/Tests/TestList.o Source/Tests/TestList.cpp
g++ -O3 -fstack-usage -MMD -c -o Source/Tests/TestLinkedlist.o Source/Tests/TestLinkedlist.cpp
g++ -O3 -fstack-usage -MMD -o all Source/main.o Source/Tests/TestList.o Source/Tests/TestLinkedlist.o
Folder Structure:
Yes, GNU make has a full suite of built-in rules.
See the manual: https://www.gnu.org/software/make/manual/html_node/Catalogue-of-Rules.html
I've been trying to compile some language (CISC) that my professor created which is pseudo assembly. He wanted us to make a makefile and compile the file with .asm extension using gcc.
This is the makefile:
.SUFFIXES: .asm
all:schemeCompiler
schemeCompiler:target.o
gcc -g -m32 -Wall -o schemeCompiler target.o
target.o: target.asm
gcc -m32 -g -Wall -ansi -c -o target.o target.asm
.PHONY: clean
clean:
rm -f *.o schemeCompiler
And this is the error I get:
gcc -m32 -g -Wall -ansi -c -o target.o target.asm
gcc: warning: target.asm: linker input file unused because linking not done
gcc -g -m32 -Wall -o schemeCompiler target.o
gcc: error: target.o: No such file or directory
gcc: fatal error: no input files
compilation terminated.
I've put the makefile with the asm file in the same directory
Thanks !
***We've figured out the problem , we weren't running the make command right. Also the makefile wasn't right:
.SUFFIXES: .asm
all:schemeCompiler
schemeCompiler:$#
gcc -g -m32 -Wall -o schemeCompiler $#
%: %.asm
gcc -x c -o $# $<
.PHONY: clean
clean:
rm -f *.o schemeCompiler
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.