-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Functions | ||
define find_files | ||
$(shell find $(1) -type f -name '$(2)') | ||
endef | ||
|
||
# Variables | ||
DIR_INC = include | ||
DIR_SRC = src | ||
DIR_BUILD = build | ||
INC_C = $(call find_files,$(DIR_INC),*.h) | ||
INC_CPP = $(call find_files,$(DIR_INC),*.hpp) | ||
SRC_C = $(call find_files,$(DIR_SRC),*.c) | ||
SRC_CPP = $(call find_files,$(DIR_SRC),*.cpp) | ||
OBJ_C = $(SRC_C:$(DIR_SRC)/%.c=$(DIR_BUILD)/%.o) | ||
OBJ_CPP = $(SRC_CPP:$(DIR_SRC)/%.cpp=$(DIR_BUILD)/%.o) | ||
TARGET = bin/project | ||
|
||
# Flags | ||
CC = gcc | ||
CXX = g++ | ||
CFLAGS = -I$(DIR_INC) -O1 -Wall | ||
CXXFLAGS = -I$(DIR_INC) -O1 -Wall | ||
LDFLAGS = -lm | ||
|
||
# Rules | ||
all: $(TARGET) | ||
|
||
$(TARGET): $(OBJ_C) $(OBJ_CPP) | ||
$(CXX) $(CXXFLAGS) -o $(TARGET) $(OBJ_C) $(OBJ_CPP) $(LDFLAGS) | ||
|
||
$(DIR_BUILD)/%.o: $(DIR_SRC)/%.c | $(DIR_BUILD)/%.d | ||
$(CC) $(CFLAGS) -c $< -o $@ | ||
|
||
$(DIR_BUILD)/%.o: $(DIR_SRC)/%.cpp | $(DIR_BUILD)/%.d | ||
$(CXX) $(CXXFLAGS) -c $< -o $@ | ||
|
||
$(DIR_BUILD)/%.d: | ||
mkdir -p $(dir $@) | ||
|
||
run: all | ||
@./$(TARGET) | ||
|
||
clean: | ||
$(RM) -r $(DIR_BUILD) $(TARGET) | ||
|
||
.PHONY: all clean run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,62 @@ | ||
# makefile-example | ||
|
||
[![Build Status](https://github.com/diffstorm/makefile-example/actions/workflows/c-cpp.yml/badge.svg)](https://github.com/diffstorm/makefile-example/actions) | ||
[![License](https://img.shields.io/github/license/diffstorm/makefile-example)](https://github.com/diffstorm/makefile-example/blob/main/LICENSE) | ||
[![Language](https://img.shields.io/github/languages/top/diffstorm/makefile-example)](https://github.com/diffstorm/makefile-example) | ||
|
||
Reusable example Makefile for C/C++ projects | ||
|
||
This is an example project that demonstrates a simple C++ project structure with some modules written in C using a Makefile. | ||
|
||
## Directory Structure | ||
|
||
- `src`: Contains the `.cpp` and `.c` source files. | ||
- `include`: Contains the `.hpp` and `.h` header files. | ||
- `bin`: The output directory for the compiled binary. | ||
- `build`: The directory for object files. | ||
|
||
``` | ||
makefile-example/ | ||
+-- src/ | ||
¦ +-- main.cpp | ||
¦ +-- utils/ | ||
¦ ¦ +-- utils.c | ||
¦ +-- helper.cpp | ||
+-- include/ | ||
¦ +-- utils/ | ||
¦ ¦ +-- utils.h | ||
¦ +-- helper.hpp | ||
+-- bin/ | ||
+-- build/ | ||
+-- Makefile | ||
``` | ||
|
||
## Usage | ||
|
||
1. Ensure you have `gcc` and `g++` installed. | ||
2. Run `make` to build the project. | ||
3. The executable will be created in the `bin/` directory. | ||
4. Run `make run` to run the executable. | ||
5. Run `make clean` to remove the built files. | ||
|
||
```sh | ||
$ git clone https://github.com/diffstorm/makefile-example.git | ||
$ cd makefile-example | ||
|
||
$ make -j4 | ||
mkdir -p build/utils/ | ||
mkdir -p build/ | ||
mkdir -p build/ | ||
gcc -Iinclude -O1 -Wall -c src/utils/utils.c -o build/utils/utils.o | ||
g++ -Iinclude -O1 -Wall -c src/helper.cpp -o build/helper.o | ||
g++ -Iinclude -O1 -Wall -c src/main.cpp -o build/main.o | ||
g++ -Iinclude -O1 -Wall -o bin/project build/utils/utils.o build/helper.o build/main.o -lm | ||
|
||
$ make run | ||
Hello from C++ code! | ||
This is a helper function from C++ code. | ||
This is a utility function from C code. | ||
|
||
$ make clean | ||
rm -f -r build bin/project | ||
``` |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#pragma once | ||
|
||
void print_helper(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef UTILS_H | ||
#define UTILS_H | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
void print_utils(); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif // UTILS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include "helper.hpp" | ||
#include <iostream> | ||
|
||
void print_helper() { | ||
std::cout << "This is a helper function from C++ code." << std::endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#include "helper.hpp" | ||
#include "utils/utils.h" | ||
#include <iostream> | ||
|
||
int main() { | ||
std::cout << "Hello from C++ code!" << std::endl; | ||
print_helper(); | ||
print_utils(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#include "utils/utils.h" | ||
#include <stdio.h> | ||
|
||
void print_utils() { | ||
printf("This is a utility function from C code.\n"); | ||
} |