Ask Your Question
1

How can a Makefile target be written to accommodate multiple libraries that have distinct dependencies?

asked 2023-01-18 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-01-07 13:00:00 +0000

lalupa gravatar image

One way to accommodate multiple libraries with distinct dependencies in a Makefile target is to create separate sub-targets for each library and its specific dependencies. Then, use these sub-targets to build the libraries and link them together into the final executable.

For example, consider two libraries, libfoo and libbar, where libfoo depends on libbaz and libbar depends on libqux. Here's an example Makefile target:

all: myprogram

libfoo:
    cd libfoo && $(MAKE)

libbar:
    cd libbar && $(MAKE)

myprogram: libfoo libbar
    gcc -o myprogram main.c -Llibfoo -Llibbar -lfoo -lbar -lbaz -lqux

This Makefile has three targets: libfoo, libbar, and myprogram. The libfoo and libbar targets build each library separately, using their own dependencies. The myprogram target depends on libfoo and libbar, and links all the libraries together with the main code.

This way, each library can be built with its specific dependencies and the final program can use them all together.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-01-18 11:00:00 +0000

Seen: 12 times

Last updated: Jan 07 '22