Home > Backend Development > C++ > How to Create a Simple C Makefile for a Single-File Project with External Dependencies?

How to Create a Simple C Makefile for a Single-File Project with External Dependencies?

Susan Sarandon
Release: 2024-12-10 05:44:16
Original
392 people have browsed it

How to Create a Simple C   Makefile for a Single-File Project with External Dependencies?

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
Copy after login
  • CC: The C compiler.
  • CFLAGS: Compilation flags for C source files.
  • LDFLAGS: Linking flags.
  • LDLIBS: Libraries required for linking.

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
Copy after login
  • all is the default target that will build the executable.
  • a3a.exe is the executable target.

4. Define Dependencies:

Define the dependencies for each target:

  • a3a.exe depends on a3driver.o and example32.sequence.o.
  • a3driver.o depends on a3driver.cpp.
  • example32.sequence.o depends on example32.sequence.cpp.

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)
Copy after login
  • These rules compile the source files and link them to create the executable.

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template