Is it possible to use make to automatically link files well?

3 years ago by rastignac to c/c_lang

I'll explain myself. I'm doing a small project using a few dependencies, so I have to add them when I call gcc. Everything else is really easy. I just have c files and header files. I find it really cumbersome to have to tell make what headers go with what c files when they have the same name. I don't see why we don't have a build system where you simply have to give a project folder with the name of source file with the main() function, give the name of the output executable, the external dependecies to be called with gcc, and that's it. Everything else can be automatically detected and linked apropriately, even with multiple folders inside the project folder. Does something like that exist that's simple to use, or is this doable in make?

suprjami 2 points 3 years ago

What you wish for is how I use make. Off the top of my head, something like this:


EXEC = programname
SOURCES = $(wildcard *.c)
OBJECTS = $(SOURCES:.c=.o)

all: $(EXEC)

$(EXEC): $(OBJECTS)

%.o: %.c %.h

.PHONY: all clean

clean:
        rm -f $(EXEC) $(OBJECTS)

Then just run make and it compiles and links all .c files into the executable. Each .c file needs a .h with the same name. Remove the %.h if you don't like that requirement.

From memory you might need a .c and .h file with the same name as the executable.

path: 0 3381502, hotness: undefined, score: 2, children: 0
lysdexic 1 point 3 years ago

I don’t see why we don’t have a build system where you simply have to give a project folder with the name of source file with the main() function, give the name of the output executable, the external dependecies to be called with gcc, and that’s it.

But we do. Check out CMake.

https://cmake.org/examples/

Originally CMake was a higher level abstraction over make, and generated makefiles based from the high-level project description. Nowadays it supports other build automation tools, such as Ninja, Visual Studio, Xcode, etc.

path: 0 3363600, hotness: undefined, score: 1, children: 3
UFODivebomb 1 point 3 years ago

Ah cmake. Boring but definitely works reliably for C/C++

path: 0 3363600 3364854, hotness: undefined, score: 1, children: 2
lysdexic 1 point 3 years ago

What do you mean by "boring" ?

path: 0 3363600 3364854 3388915, hotness: undefined, score: 1, children: 1
UFODivebomb 1 point 3 years ago

It's boring like a good tool should be: does what it says. No fuss. No cloud of hype.

path: 0 3363600 3364854 3388915 3402466, hotness: undefined, score: 1, children: 0
0x0 1 point 3 years ago

make has special variables you can use, some people will suggest cmake, others will mention pkgconfig.

path: 0 3362224, hotness: undefined, score: 1, children: 0
smpl 1 point 3 years ago
path: 0 3373212, hotness: undefined, score: 1, children: 0
c_lang
c_lang

@programming.dev

login for more options
1359
104
40

Welcome to the C community!

C is quirky, flawed, and an enormous success.
... When I read commentary about suggestions for where C should go, I often think back and give thanks that it wasn't developed under the advice of a worldwide crowd.
... The only way to learn a new programming language is by writing programs in it.

© Dennis Ritchie

🌐 https://en.cppreference.com/w/c

go to feed...