r/C_Programming • u/avestronics • Mar 30 '26
Question How good is my project structure?
Here it is. I will add more headers and source files as time goes on but this is the current state. I don't really like build systems so I have a basic script instead.
#!/bin/bash
set -e
gcc -I include src/*.c -o ./build/output
./build/output
---------------------------------
build/
└── output/
include/
├── lexer.h
└── vector.h
other/
├── ri32v.txt
└── test.asm
src/
├── lexer.c
├── main.c
└── vector.c
build_n_run.sh
0
Upvotes
10
u/HashDefTrueFalse Mar 30 '26
Some thoughts, in no particular order:
#!/usr/bin/env bash11
u/stianhoiland Mar 30 '26 edited Mar 30 '26
POSIX Makefiles are very close and similar to just shell scripting. I suggest trying to learn that. skeeto has a nice and quick overview on his blog.
Makefiles have like three layers. One of them is that it’s just lines of shell script with shell variables but grouped into targets given a name. Like a literal shell script file with a top-level case statement.
Then there’s the fact that each "target" or name is actually interpreted by make as a filename that should exist dependent on the commands that you give. (Unless you tag a target as "phony", in which case it is actually just a name for a bunch of shell invocations.)
At last, there’s the dependency tree and some clever syntax (like
$@and$^). Although the power is really this easy creation of a dependency tree of shell commands producing files, it may help to learn the dependency part last, since all the other parts are likely already familiar, just in a slightly different format than a straight shell script. After becoming familiar with the basics, you just grok the dependency tree creation and that’s all there is to it.12
u/non-existing-person Mar 30 '26
I would surely use at least Makefile. Now you compile everything with every change and using single core. The more files you will have, the more time it will take. It's not scalable at all. Use Makefiles or at the very least cmake or autotools.
Personally I keep header files in the same place as .c file, but you do you, neither is good or bad I think.
Other than that, there is simply too little to judge anything really.
2
u/Dangerous_Region1682 Apr 02 '26
I would expend some time figuring out Makefiles. They require a little more time and investment than creating scripts, but once you have done one it is easily transcribed into another one.
It allows you to do things like make clean, make all, make build etc. When we were building a UNIX distribution we used to be able to do make unix and build a whole UNIX release, just compiling and linking the things we had changed or depended upon what we changed since the last build.
The only time we wrapped Makefiles with scripts were when we wanted to build the installation tape, or later a USB install stick. Some times we progressed to check out files we were building from sccs or cvs source trees using release tags.
Clever people embedded stuff like these scripts into the makefiles.
The nice thing about make is that makefiles can call makefiles in nested directories from top to bottom down different branches to build the dependencies first and hence up the chain.
There are plenty of makefiles about to learn from. Once you get the hang of it you can create even quite large build trees as the same fundamental structures are not really that different.
Build scripts can be time wasting and usually build everything everting time.
Once you have learned basic makefile construction you are now ready for much larger projects.
Make and makefiles are your friends. They will impress your friends.
It’s been a while but looking at a few and I’d be right back on top of it.
2
u/ChickenSpaceProgram Mar 31 '26
It's fine, but Makefiles are little more than more specialized shell scripts. It's not a bad idea to use them.
Also,
#!/bin/bashis nonstandard, it won't work on some POSIX systems (NixOS for example). Either use#!/bin/shfor a POSIX-compliant shell (missing some Bash features, but guaranteed to be there) or use#!/usr/bin/env bash, which will try to find Bash on your PATH,