I want to write a lot of tiny example programmes for one same library, each needs gcc $(OtherOpt) -o xxx -lthelibname xxx.c.
How to write a Makefile without dozens of tagret lines ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Pattern rules are your friend for these situations. As long as your targets all match a predictable pattern — and they do in this case, as they are all of the form “create
foofromfoo.c” — you can write a single pattern rule that will be used for all of the targets:Now you can either run simply
maketo build all your apps, ormake appnameto build a specific app. Here I’ve created a single pattern rule that will be used anytime you want to createsomethingfromsomething.c. I used the$@automatic variable, which will expand to the name of the output, and the$<variable, which will expand to the name of the first prerequisite, so that the command-line is correct regardless of the specific app being built. Technically you don’t need theallline, but I figured you probably didn’t want to always have to type in the name(s) of the apps you want to build.Also, technically you can probably get away without having any of this makefile, because GNU make already has a built-in pattern rule for the
%: %.crelationship! I mention this option only for completeness; personally, I prefer doing things the way I’ve shown here because it’s a little bit more explicit what’s going on.