Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatic generate target From Source List Make File

Tags:

makefile

I have this directory structure.

app/
   src
   include
   lib/
       src
   maincode/
           main.cc
           Makefile

I want to generate automatic target from the source list in makefile. So I don't have to write rule for each file.

Example

source=\
        ../src/a.cpp
        ../src/ab.cpp
        ../lib/src/b.cpp

I want to write rule like

%.o:%.cpp

so that I don't have to repeat rule for each file. How I can achieve this ?

like image 485
Vivek Goel Avatar asked Aug 06 '26 23:08

Vivek Goel


1 Answers

Edit: the find command should be inside a shell variable

If you are using Linux, I think you can use:

SOURCES=$(shell find . -name *.cpp)
OBJECTS=$(SOURCES:%.cpp=%.o)

%.o: %.cpp
    <command to compile>
like image 141
Sagar Avatar answered Aug 09 '26 02:08

Sagar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!