Home > Software engineering >  A little trouble with g compiling makefile
A little trouble with g compiling makefile

Time:02-20

I've got the following makefile:

test_containers: containers.o
    g   out/containers.o test/test_containers.cpp -o out/test_containers.exe

containers.o: queue.o stack.o container.o
    g   -c out/queue.o out/stack.o out/container.o -o out/containers.o

queue.o: container.o
    g   -c src/Queue.cpp out/container.o -o out/queue.o

stack.o: container.o
    g   -c src/Stack.cpp out/container.o -o out/stack.o

container.o:
    g   -c src/Container.cpp -o out/container.o

The problem is, when I do make containers.o, out/containers.o does not appear.

Compile log:

D:\DevProjects\2nd-semester\cpp\S1_dev>make containers.o
g   -c src/Container.cpp -o out/container.o

g   -c src/Queue.cpp out/container.o -o out/queue.o
g  : warning: out/container.o: linker input file unused because linking not done

g   -c src/Stack.cpp out/container.o -o out/stack.o
g  : warning: out/container.o: linker input file unused because linking not done

g   -c out/queue.o out/stack.o out/container.o -o out/containers.o
g  : warning: out/queue.o: linker input file unused because linking not done
g  : warning: out/stack.o: linker input file unused because linking not done
g  : warning: out/container.o: linker input file unused because linking not done

Is there something that I do not understand right? How do I get a containers.o file?

(ran on Windows 10 machine btw)

upd: information supply because people say there's very little

My file tree:

│   makefile
│
├───.vscode
│       settings.json
│
├───out
│       container.o
│       queue.o
│       stack.o
│
├───src
│       Container.cpp
│       Container.h
│       Queue.cpp
│       Queue.h
│       Stack.cpp
│       Stack.h
│
└───test
        catch.hpp
        test_containers.cpp

I don't really know what I can say more other than that .cpp files include matching .h files, and both Queue.h and Stack.h include Container.h and test_containers.cpp file includes catch.

CodePudding user response:

Your recipes are cluttered with files that don't belong there. Given the very limited amount of information you've provided, the most direct solution is to just slam out the recipes directly.

test_containers: out/test_containers.exe

out/test_containers.exe: out/test_containers.o out/containers.a
    g   $^ -o $@ 

out/test_containers.o: test/test_containers.cpp
    g   -c $< -o $@

out/containers.a: out/queue.o out/stack.o out/container.o
    ar rcs $@ $^

out/queue.o: src/Queue.cpp
    g   -c $< -o $@

out/stack.o: src/Stack.cpp
    g   -c $< -o $@

out/container.o: src/Container.cpp
    g   -c $< -o $@

clean:
    rm -f out/*.o

Some notes about some of the stuff you see up there, that make the rules simpler to write:

  • $@ is a macro denoting the target of the rule
  • $^ is a macro that expands to all dependencies of the current target, with duplicate names removed.
  • $< is a macro that expands to the first dependency.

There is a LOT more that can/should be done (dependency generation, implicit rule usage, phony target, etc.), but that's the basics of it.

  • Related