Hi,
i want to create a Makefile to simplify the compilation of my C++ programs.
It should recursively collect all source files in a parent src directory and automatically create dependency files.
I found an example here but if i try to use it,
I understand that the Makefile fails to append the sources to the
Is this because i use shells/bash as my shell? How can i make the Makefile work?
Makefile
src/main.cpp
Thanks.
i want to create a Makefile to simplify the compilation of my C++ programs.
It should recursively collect all source files in a parent src directory and automatically create dependency files.
I found an example here but if i try to use it,
clang++
complains:
Code:
[xxx@xxx ~/development]$ make
mkdir -p ./build
clang++ -Wfatal-errors -Wall -Wextra -Wpedantic -Wconversion -Wshadow -o ./build/mybin
clang++: fatal error: no input files
*** Error code 1
Stop.
make: stopped in /home/xxx/development
I understand that the Makefile fails to append the sources to the
clang++
command.Is this because i use shells/bash as my shell? How can i make the Makefile work?
Makefile
Code:
CXX = clang++
CXX_FLAGS = -Wfatal-errors -Wall -Wextra -Wpedantic -Wconversion -Wshadow
# Final binary
BIN = mybin
# Put all auto generated stuff to this build dir.
BUILD_DIR = ./build
# List of all .cpp source files.
CPP = $(wildcard src/*.cpp)
# All .o files go to build dir.
OBJ = $(CPP:%.cpp=$(BUILD_DIR)/%.o)
# Gcc/Clang will create these .d files containing dependencies.
DEP = $(OBJ:%.o=%.d)
# Default target named after the binary.
$(BIN) : $(BUILD_DIR)/$(BIN)
# Actual target of the binary - depends on all .o files.
$(BUILD_DIR)/$(BIN) : $(OBJ)
# Create build directories - same structure as sources.
mkdir -p $(@D)
# Just link all the object files.
$(CXX) $(CXX_FLAGS) $^ -o $@
# Include all .d files
-include $(DEP)
# Build target for every single object file.
# The potential dependency on header files is covered
# by calling `-include $(DEP)`.
$(BUILD_DIR)/%.o : %.cpp
mkdir -p $(@D)
# The -MMD flags additionaly creates a .d file with
# the same name as the .o file.
$(CXX) $(CXX_FLAGS) -MMD -c $< -o $@
.PHONY : clean
clean :
# This should remove all generated files.
-rm $(BUILD_DIR)/$(BIN) $(OBJ) $(DEP)
src/main.cpp
Code:
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "HElloe" << std::endl;
return 0;
}
Thanks.