patsubst in makefile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting patsubst in makefile
# 1  
Old 06-02-2011
patsubst in makefile

hi,
I have .cpp files in 2 subdirectories within my ../src directory,
so the file structure is:
../src/subdir1/file1.cpp
../src/subdir2/file2.cpp

I am trying to use patsubst as follows:
Code:
SRCDIR = $(shell echo ../src/*)
libname := $(notdir $(basename $(SRCDIR)))
OBJDIR = ../objs
OBJDIRS := $(addprefix $(OBJDIR)/,$(libname))
find_cppfiles = $(wildcard $(dir)/*.cpp)
cppsources :=$(foreach dir, $(SRCDIR),$(find_cppfiles)) 
cppobjects :=$(patsubst $(SRCDIR)/%.cpp,$(OBJDIRS)/%.o, $(cppsources))
echo $(cppsources) $(cppobjects)

but it doesn't work....the echo command returns:
../src/subdir1/file1.cpp
../src/subdir2/file2.cpp
../src/subdir1/file1.cpp
../src/subdir2/file2.cpp

Can you please let me know how I can fix this so that echo $(cppobjects) returns:
../objs/subdir1/file1.o
../objs/subdir2/file2.o

thanks!
# 2  
Old 06-06-2011
Code:
for file in ../src/*
do 
    echo $file|sed 's/src/objs/'
done

# 3  
Old 06-06-2011
thanks,
maybe this is a basic q? but do I assign this statement to a variable...
also, do I need to write a separate sed to change the file extension from .c to .o
# 4  
Old 06-06-2011
u can replace multiple occurance in the same sed itself for making change inside file.

sample one.

Code:
echo "first string,Second string"|sed 's/first/test1/g;s/second/test2/g' 
test1 string,test2 string

for renaming file you may have to write something after declaring variables for new file names or something like this.

Code:
mv filename.cpp `echo filename.cpp|sed 's/\.cpp/\.o/'`

Thanks
SHa
This User Gave Thanks to Shahul For This Post:
# 5  
Old 06-06-2011
thanks Shahul,

that worked Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Makefile for g++

Hi All, We have moved our OS from Sun Solaris to Linux and also some of the compilers. Our old makefile used to be as below: CC=cc FLAGS=-G -KPIC -DLG_SOLARIS_OS DEFINES=-DSunOS SYSLIBS=-lc .SUFFIXES : .c .c.o : ;$(CC) -c $(FLAGS) $(DEFINES) $*.c -o $*.o ... (3 Replies)
Discussion started by: shash
3 Replies

2. UNIX for Dummies Questions & Answers

MakeFile

Hey everybody, This may be stup*d question for you, but i am new in unix and i wonder how can i make the rules for translating and linking my .c "primjer1.c", "primjer2.c" and "primjer3.c" in makefile. Thank you. (7 Replies)
Discussion started by: jskako
7 Replies

3. Shell Programming and Scripting

Makefile

Dear all, I have a quite simple question about how to manipulate "makefile.am". I intend to: 1. "CFLAGS" and "CXXFLAGS" have no value at all. I know that these values get "-g -O2" by default. On the other hand, when I try to set them as "CFLAGS = " in "makefile.am", I get warning messages... (4 Replies)
Discussion started by: Dandan
4 Replies

4. UNIX for Dummies Questions & Answers

Help with MakeFile

I'm really confused how to use a makefile. Are you supposed to be make a file from emacs called MakeFile and put code in there to compile? I am trying to create a makefile to compile two .cpp files in my current directory to produce two .o files and then link them... What I did was make a... (1 Reply)
Discussion started by: jzhang172
1 Replies

5. Homework & Coursework Questions

Makefile Help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have been trying to make the program swap but i have been getting errors with the makefile such as driver.o:... (1 Reply)
Discussion started by: mgyeah
1 Replies

6. Homework & Coursework Questions

Help with Simple Multi-Level Makefile (Extremely New at Makefile)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Basically, the prompt is make a makefile with various sub makefiles in their respective subdirectories. All code... (1 Reply)
Discussion started by: Tatl
1 Replies

7. UNIX for Advanced & Expert Users

Makefile executing another Makefile first?

I have 2 libraries in 2 different directories that I build with Makefiles. library B depends on library A. If I modify a .cpp file in library A and run lib B's Makefile can I have B's makefile to automatically rebuild library A? I am now rebuilding A, followed by B... but I'd like B to... (0 Replies)
Discussion started by: wwuster
0 Replies

8. UNIX for Advanced & Expert Users

Makefile problem - How to run module load in a Makefile

Hi, I'm trying to run the module load command in a Makefile and i'm getting the following error: make: module: command not found Why is this? Is there any way to run this command in a Makefile? NOTE: command - module load msjava/sunjdk/1.5.0 works fine outside of the Makefile (2 Replies)
Discussion started by: hernandinho
2 Replies

9. High Performance Computing

help with makefile

I am new to creating makefiles. I have several fortran programs in a folder called as "test" and also have several subroutines in another folder (which is inside this test folder) called as libry My makefile is in the folder "test" I want to create a makefile which can access the files in... (2 Replies)
Discussion started by: explorer
2 Replies

10. Programming

about the makefile

can anyone well explain how to create a makefile? especially those commands in the makefile? BTW, what is CFLAG? (2 Replies)
Discussion started by: ligerdave
2 Replies
Login or Register to Ask a Question