Scorb
Scorb

Reputation: 1840

makefile "no such file or directory"

I am trying to compile a C++ program using make. After reading quite a few tutorials, I have come upon the following make file.

It reads source files from src directory, puts objects in the build directory, and puts the exe in the bin directory.

When I try running the following makefile, I get the subsequent error.

CC := g++
CFLAGS := -g -O2
BIN_DIR := /bin
BUILD_DIR := /build
SRC_DIR := /src
TARGET := wavfiletool.exe
SOURCES  := $(wildcard $(SRC_DIR)/*.c)
OBJECTS  := $(SOURCES:$(SRCDIR)/%.cpp=$(BUILD_DIR)/%.o)

$(BIN_DIR)/$(TARGET): $(OBJECTS)
    $(CC) $@ $(CFLAGS) $(OBJECTS)

$(OBJECTS): $(BUILD_DIR)/%.o : $(SRC_DIR)/%.c
    @$(CC) $(CFLAGS) -c $< -o $@

++ /bin/wavfiletool.exe -g -O2 g++: error: /bin/wavfiletool.exe: No such file or directory g++: fatal error: no input files compilation terminated. make: *** [/bin/wavfiletool.exe] Error 1 [Finished in 0.1s with exit code 2]

UPDATE:

I have edited the makefile. One typo in the previous version was that I have .c when I am using .cpp and c++. I am now getting a different error.

UPDATED MAKEFILE

CC := g++
CFLAGS := -g -O2
BIN_DIR := bin
BUILD_DIR := build
SRC_DIR := src
TARGET := wavfiletool.exe
SOURCES  := $(wildcard $(SRC_DIR)/*.cpp)
OBJECTS  := $(SOURCES:$(SRCDIR)/%.cpp=$(BUILD_DIR)/%.o)

$(BIN_DIR)/$(TARGET): $(OBJECTS)
    $(CC) $(OBJECTS) $(CFLAGS)  -o $@ 

$(OBJECTS): $(BUILD_DIR)/%.o : $(SRC_DIR)/%.cpp
    @$(CC) $(CFLAGS) -c $< -o $@

new error

makefile:13: target `src/WavFile.cpp' doesn't match the target pattern
makefile:13: target `src/WavFileTool.cpp' doesn't match the target pattern
g++ src/WavFile.cpp src/WavFileTool.cpp -g -O2  -o bin/wavfiletool.exe 

Upvotes: 2

Views: 92355

Answers (1)

Michael Albers
Michael Albers

Reputation: 3779

Unless all of your directories (i.e., BIN_DIR, SRC_DIR) are in the root directory (/) then that is why you're getting the error. You either want to remove the initial slash or you can use an environment variable prefix like SRC_DIR = $(MY_PROJECT_BASE_DIRECTORY)/src.

Upvotes: 5

Related Questions