How to Make a SIMPLE C Makefile
This guide provides a simple approach to creating a Makefile for a C project with minimal dependencies.
Problem:
You need to create a Makefile for a project with a single file, a3driver.cpp, which imports a class from "/user/cse232/Examples/example32.sequence.cpp". The goal is to generate an executable named a3a.exe.
Solution:
1. Create a Makefile:
Create a file named Makefile in the project directory.
2. Define Variables:
Start by defining variables that will be used throughout the Makefile:
CC = g++ CFLAGS = -g LDFLAGS = -g LDLIBS = -lstdc++ -lm
3. Specify Targets:
Next, define targets for the project:
all: a3a.exe a3a.exe: a3driver.o example32.sequence.o a3driver.o: a3driver.cpp example32.sequence.o: example32.sequence.cpp
4. Define Dependencies:
Define the dependencies for each target:
5. Define Rules:
Specify the commands that will be executed for each target and its dependencies:
a3driver.o: $(CC) $(CFLAGS) -c a3driver.cpp example32.sequence.o: $(CC) $(CFLAGS) -c example32.sequence.cpp a3a.exe: a3driver.o example32.sequence.o $(CC) $(LDFLAGS) -o a3a.exe a3driver.o example32.sequence.o $(LDLIBS)
6. Define Clean Target (Optional):
You may also define a clean target to remove all build artifacts:
clean: rm -f a3driver.o example32.sequence.o a3a.exe
7. Run the Makefile:
To build the project, run make from the command line. This will create the a3a.exe executable.
Note:
Since Unix-based systems do not use file extensions for executables, the a3a.exe target does not have an extension in the Makefile.
The above is the detailed content of How to Create a Simple C Makefile for a Single-File Project with External Dependencies?. For more information, please follow other related articles on the PHP Chinese website!