Jerfov2
Jerfov2

Reputation: 5524

Make remove directory from every path in array

So far, I have been compiling my c and c++ (and a little glsl) project with a shell script that recompiles the whole thing even when I wanted to make a small, simple change. It started taking almost 5 seconds. I couldn't stand for that, so I turned to Makefiles. I think my Makefile is good so far except for one problem: When I convert my CSOURCES to COBJECTS, I don't remove the directory that the source file is in, so I get these errors:

clang: error: no such file or directory: 'lib/key_store.o'
clang: error: no such file or directory: 'lib/logger.o'
clang: error: no such file or directory: 'lib/myglutils.o'

Here's the makefile:

CC=gcc
CPPC=g++
CCFLAGS=-std=c11 -Wall -pedantic
CPPFLAGS=-std=c++11 -Wall -pedantic
INC=-Iinclude/
LDFLAGS=lib/libGLEW.a lib/libglfw3.a -framework OpenGL -framework CoreVideo -framework Cocoa -framework IOKit
CSOURCES=$(wildcard lib/*.c)
COBJECTS=$(CSOURCES:.c=.o)
CPPSOURCES=$(wildcard *.cpp)
CPPOBJECTS=$(CPPSOURCES:.cpp=.o)
TARGET=Ultra-Fighters

all: $(TARGET)

$(TARGET): $(COBJECTS) $(CPPOBJECTS)
    $(CPPC) -o $@ $^ $(LDFLAGS)

%.o: %.cpp %.hpp
    $(CPPC) $(CPPFLAGS) $(INC) -c $<

%.o: %.cpp
    $(CPPC) $(CPPFLAGS) $(INC) -c $<

%.o: %.c %.h
    $(CC) $(CFLAGS) $(INC) -c $<

%.o: %.c
    $(CC) $(CCFLAGS) $(INC) -c $<

clean:
    rm -f *.o

My question is: How do I remove the directory part of the path from CSOURCES when I assign to COBJECTS?

Upvotes: 1

Views: 4711

Answers (1)

Brendan
Brendan

Reputation: 2401

A slightly hacky, direct solution to your problem is notdir.

Upvotes: 2

Related Questions