Skip to content

Commit

Permalink
Add makefile-example
Browse files Browse the repository at this point in the history
  • Loading branch information
diffstorm committed May 28, 2024
1 parent 51413c8 commit c00cc54
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 0 deletions.
46 changes: 46 additions & 0 deletions Makefile
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
60 changes: 60 additions & 0 deletions README.md
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 added bin/project
Binary file not shown.
3 changes: 3 additions & 0 deletions include/helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void print_helper();
14 changes: 14 additions & 0 deletions include/utils/utils.h
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
6 changes: 6 additions & 0 deletions src/helper.cpp
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;
}
11 changes: 11 additions & 0 deletions src/main.cpp
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;
}
6 changes: 6 additions & 0 deletions src/utils/utils.c
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");
}

0 comments on commit c00cc54

Please sign in to comment.