STANAANDREY

Untitled

Aug 2nd, 2026
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. There isn't one "official" standard dictated by `gcc`, but the C community has coalesced around a clean, conventional directory structure that scales well from small tools to massive projects.
  2.  
  3. Here is the standard, production-ready project structure:
  4.  
  5. ```text
  6. my_c_project/
  7. ├── bin/ # Compiled binary executables (ignored by git)
  8. ├── build/ # Intermediate object files (.o, .d) (ignored by git)
  9. ├── include/ # Public header files (.h)
  10. │ └── my_project/ # Optional namespace folder for headers
  11. │ └── utils.h
  12. ├── src/ # Source code (.c files) and private internal headers
  13. │ ├── main.c
  14. │ └── utils.c
  15. ├── tests/ # Test scripts and source files
  16. │ └── test_main.c
  17. ├── .gitignore
  18. ├── LICENSE
  19. ├── Makefile # Build script at the root
  20. └── README.md
  21.  
  22. ```
  23.  
  24. ---
  25.  
  26. ## 1. Where should `main.c` go?
  27.  
  28. **Put `main.c` inside `src/`.** Keep your root directory clean for build configs, documentation, and configuration files (`Makefile`, `README.md`, `.gitignore`).
  29.  
  30. * **`src/`**: Houses all implementation files (`.c`), including `main.c`.
  31. * **`include/` vs `src/` for headers**:
  32. * Put **public headers** (interfaces meant to be shared or exported) in `include/`.
  33. * Put **private headers** (internal headers only used by code inside `src/`) right alongside the `.c` files inside `src/`.
  34.  
  35.  
  36.  
  37. ---
  38.  
  39. ## 2. Where should the `Makefile` go?
  40.  
  41. **Place the `Makefile` in the project root.**
  42.  
  43. Having the Makefile at the root lets you build the entire project by just typing `make` from the main directory. A clean Makefile will compile objects into a `build/` directory and place the final output in `bin/`.
  44.  
  45. Here is a robust, production-ready starter `Makefile` using `gcc` that handles auto-creating directories and tracking header dependencies:
  46.  
  47. ```makefile
  48. # Compiler and Flags
  49. CC := gcc
  50. CFLAGS := -Wall -Wextra -std=c11 -pedantic -Iinclude
  51. DEPFLAGS := -MMD -MP
  52.  
  53. # Directories
  54. SRC_DIR := src
  55. BUILD_DIR := build
  56. BIN_DIR := bin
  57.  
  58. # Target Binary Name
  59. TARGET := $(BIN_DIR)/my_program
  60.  
  61. # Discover all .c files in src/ and map them to .o files in build/
  62. SRCS := $(wildcard $(SRC_DIR)/*.c)
  63. OBJS := $(SRCS:$(SRC_DIR)/%.c=$(BUILD_DIR)/%.o)
  64. DEPS := $(OBJS:.o=.d)
  65.  
  66. # Default Rule
  67. all: $(TARGET)
  68.  
  69. # Link the executable
  70. $(TARGET): $(OBJS) | $(BIN_DIR)
  71. $(CC) $(OBJS) -o $@
  72.  
  73. # Compile source files to object files
  74. $(BUILD_DIR)/%.o: $(SRC_DIR)/%.c | $(BUILD_DIR)
  75. $(CC) $(CFLAGS) $(DEPFLAGS) -c $< -o $@
  76.  
  77. # Create directories if they don't exist
  78. $(BUILD_DIR) $(BIN_DIR):
  79. mkdir -p $@
  80.  
  81. # Clean build artifacts
  82. clean:
  83. rm -rf $(BUILD_DIR) $(BIN_DIR)
  84.  
  85. # Include automatically generated dependency files (.d)
  86. -include $(DEPS)
  87.  
  88. .PHONY: all clean
  89.  
  90. ```
  91.  
  92. ---
  93.  
  94. ## 3. Important Tips for `gcc`
  95.  
  96. 1. **`-Iinclude` Flag:** When invoking `gcc`, use the `-Iinclude` flag so your `#include` directives search the `include/` directory automatically. Inside your source files, write `#include "utils.h"` instead of relative paths like `#include "../include/utils.h"`.
  97. 2. **Auto-Dependencies (`-MMD -MP`):** The Makefile snippet above generates `.d` files automatically. This ensures that if you edit a `.h` file, `make` re-compiles only the `.c` files that depend on it.
  98. 3. **Ignore Build Artifacts:** Add `bin/` and `build/` to your `.gitignore` so generated binaries aren't pushed to source control.
Advertisement
Add Comment
Please, Sign In to add comment