Hello everyone,
I am beginning to learn how to create Makefiles for C++ projects and am wondering if there is a way to automate the process of adding new .cpp or .h files as they are created. In other words, say I initially create my Makefile knowing of a few files ahead of time and I write in the necessary arguments to compile them but then later on, I create a new .cpp file in my project, and rather than going into the Makefile each time to manually add arguments to compile it, is there a way to have this added automatically or more efficiently? Hopefully this makes sense.
Thank you!
C++ and Makefiles
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Topics in this forum are automatically closed 6 months after creation.
C++ and Makefiles
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Re: C++ and Makefiles
Ok, I may have found the answer I am looking for but if anyone thinks this may not be it or there is a better way, please let me know.
https://www.classes.cs.uchicago.edu/arc ... /make.html
(Scroll towards the bottom to the section titled, "A Rule For Everyone - Using "File Type" Rules")
*The example I found is for the C programming language
I'm assuming 'make' will look at every .cpp and .h file in the directory (old and new) to see whether they have been changed and if so, execute the compilation on just those files that have been edited.
https://www.classes.cs.uchicago.edu/arc ... /make.html
(Scroll towards the bottom to the section titled, "A Rule For Everyone - Using "File Type" Rules")
*The example I found is for the C programming language
Code: Select all
# use "gcc" to compile source files.
CC = gcc
# the linker is also "gcc". It might be something else with other compilers.
LD = gcc
# Compiler flags go here.
CFLAGS = -g -Wall
# Linker flags go here. Currently there aren't any, but if we'll switch to
# code optimization, we might add "-s" here to strip debug info and symbols.
LDFLAGS =
# use this command to erase files.
RM = /bin/rm -f
# list of generated object files.
OBJS = main.o file1.o file2.o
# program executable file name.
PROG = prog
# linking rule remains the same as before.
$(PROG): $(OBJS)
$(LD) $(LDFLAGS) $(OBJS) -o $(PROG)
# now comes a meta-rule for compiling any "C" source file.
%.o: %.c
$(CC) $(CFLAGS) -c $<
Re: C++ and Makefiles
Have a look at something like this; https://elixir.bootlin.com/linux/latest/source/Makefile
Then, remember the KISS principle.
Focus on learning C++. If you're starting out, keep the name of the program and the cpp file the same. For instance, prog and prog.cpp, with that all you need to do is type make prog, no Makefile needed. Make knows how to build your program.
I agree with with you. There must be a better way. Unfortunately, there are many ways to write Makefiles depending on what you want to do. Make is a powerful tool. I actually just wrote a Makefile that scans directories for .gif files which makes sense for what I am doing. I can add or remove gif files, and my Makefile will still work. My "dependencies" are loose. Sometimes, it's better to have tight dependencies, but that means more work hard-coding those dependencies into the Makefile. I try not to get too complicated, if I can help it. Hopefully this makes sense.
Then, remember the KISS principle.
Focus on learning C++. If you're starting out, keep the name of the program and the cpp file the same. For instance, prog and prog.cpp, with that all you need to do is type make prog, no Makefile needed. Make knows how to build your program.
I agree with with you. There must be a better way. Unfortunately, there are many ways to write Makefiles depending on what you want to do. Make is a powerful tool. I actually just wrote a Makefile that scans directories for .gif files which makes sense for what I am doing. I can add or remove gif files, and my Makefile will still work. My "dependencies" are loose. Sometimes, it's better to have tight dependencies, but that means more work hard-coding those dependencies into the Makefile. I try not to get too complicated, if I can help it. Hopefully this makes sense.