Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile - remove ../ from path

Tags:

makefile

I have object files coming in with paths that might look like this:

 '../../src/foo/bar.c'

I'd like them to be output to

 'build/src/foo/bar.o'

Currently using:

 COBJS      :=  $(notdir $(CFILES))
 COBJS      :=  $(patsubst %,$(BUILD)%.o,$(COBJS))

I can achieve

 'build/bar.o'

This is problematic if any two library/project contains the same class name.

So the question is, how can one remove multiple '../' from a path in Make. I've attempted the obvious and naive approaches with no results.

Update, the following will match exactly ../../ and replace it with the rest. This is perfect except that it is specific to ../../. Just need to make it match any number of ../../

 COBJS      :=  $(CFILES:../../%=%)

Update,

SOLVED, just three reputation shy of posting my own answer.

 COBJS      :=  $(subst ../,,$(CFILES))
like image 712
Halsafar Avatar asked Mar 16 '12 04:03

Halsafar


People also ask

How do I get the full path of a makefile?

This 2 lines in Makefile get the full path and dir of the Makefile itself: mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) mkfile_dir := $(dir $(mkfile_path)) The piece of code gets you Makefile, finds its absolute path and the directory. It does not rely on your work directory at all.

What is the use of Makefiles in Python?

The main use of MAKEFILES is in communication between recursive invocations of the make. If you have put the header files in different directories and you are running make in a different directory, then it is required to provide the path of header files. This can be done using -I option in makefile.

What are Makefiles in C++?

Makefiles are the solution to simplify this task. Makefiles are special format files that help build and manage the projects automatically. For example, let’s assume we have the following source files. The following is the code for main.cpp source file − The code given below is for hello.cpp source file −

How to run a makefile from command prompt?

If you have prepared the Makefile with name "Makefile", then simply write make at command prompt and it will run the Makefile file. But if you have given any other name to the Makefile, then use the following command − This is an example of the Makefile for compiling the hello program.


1 Answers

As posted in my original question and I forgot to eventually answer.

The solution for this and likely many other Make string replacement is as follows:

COBJS      :=  $(subst ../,,$(CFILES))

'subst' takes 3 parameters. $toMatch, $replaceWith, $string.

In this case $(CFILES) is the list of all .c files to compile. I replace '../' with nothing.

like image 89
Halsafar Avatar answered Sep 28 '22 10:09

Halsafar