'Makefile linking a file to a file that depends on another file

I have a file test1.c which depends on test2.c, which in turn depends on libcurl(curl.h) In my makefile, when I do gcc test1.o test2.o, while linking test1 to test2, it gives error - undefined reference to all the curl methods used in test2.c.

How do I make test1.c inherit all the dependencies of test2.c without explicitly having to do gcc test1.o test2.o -lcurl



Solution 1:[1]

There are many little shortcuts that Makefile provides that make it easier to get the job done if that is what you are asking.

# $* = "test"
# $^ = all dependencies

test: test1.o test2.o
    gcc -o $* $^

test1.o: test1.c
    # GCC command
test2.o: test2.c
    # GCC command

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Primitive