- 1 year ago
C++ - Creating Makefile on Windows
To create a Makefile on Windows for a C++ project, you can use tools like GNU Make or CMake. Here's a basic example of creating a Makefile using GNU Make:
Create a new text file in your project directory and name it
Makefile
(no file extension).Open the
Makefile
in a text editor and define the build rules and dependencies. Here's an example Makefile:
makefileCC = g++
CFLAGS = -Wall -Wextra
LDFLAGS =
SOURCES = main.cpp foo.cpp bar.cpp
OBJECTS = $(SOURCES:.cpp=.o)
EXECUTABLE = myprogram
.PHONY: all clean
all: $(EXECUTABLE)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) -o $@ $^
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
clean:
rm -f $(OBJECTS) $(EXECUTABLE)
In this example, we have defined variables for the compiler (CC
), compiler flags (CFLAGS
), linker flags (LDFLAGS
), source files (SOURCES
), object files (OBJECTS
), and the executable name (EXECUTABLE
).
The all
rule is the default target, which depends on the $(EXECUTABLE)
target.
The $(EXECUTABLE)
target depends on the object files ($(OBJECTS)
), and the commands under this target link the object files into the final executable.
The %.o
target is a pattern rule that compiles each source file into an object file.
The clean
target is a phony target that removes the object files and the executable.
Save the Makefile and close the text editor.
Open a command prompt or terminal and navigate to your project directory.
Run the
make
command to build your project using the Makefile.
Make sure you have GNU Make installed on your system and its path added to the environment variable PATH
for the make
command to be recognized.
This is a basic example to get started with creating a Makefile on Windows using GNU Make. You can customize it according to your specific project structure and requirements.