0

I am currently working on a C project to implement a Linux userspace application. I wrote this Makefile, which allows me to compile :

PROJ_DIR := ../
SRC_DIR := $(PROJ_DIR)/src
BUILD_DIR := $(PROJ_DIR)/build

# define the executable file
TARGET := tcp_proxy

# include paths list
INC_DIRS := $(SRC_DIR)
INC_DIRS += $(SRC_DIR)/rpmsg
INC_DIRS += $(SRC_DIR)/rpmsg/rx
INC_DIRS += $(SRC_DIR)/rpmsg/tx
INC_DIRS += $(SRC_DIR)/tcp
INC_DIRS += $(SRC_DIR)/tcp/rx
INC_DIRS += $(SRC_DIR)/tcp/tx

INC_FLAGS := $(addprefix -I,$(INC_DIRS))

# source file list
SRCS := $(shell find $(SRC_DIR) -name *.c)

# object file list
OBJS := $(SRCS:.c=.o)

LDFLAGS := -pthread

all: tcp_proxy

tcp_proxy: $(OBJS)
    $(CC) $(CFLAGS) $(INC_FLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)

%.o: %.c
    $(CC) $(CFLAGS) $(INC_FLAGS) -c $< -o $@

.PHONY: clean

clean:
    $(RM) *.o *~ $(TARGET)

However, the object files are generated in the same place as the source files. So I would like to know how to duplicate the folders under "src" in a "build" directory and generate inside this directory the object files and the executable.

I would also like, when I do a make clean, to be able to delete all the object files and the executable.

My project :

enter image description here

1
  • There is no need to write a %.o: %.c rule. make has default rules for common things like this; use them. Commented Jan 13, 2022 at 17:08

1 Answer 1

1

Searching would definitely find you a lot of examples and information on this.

First tell make what objects you want to build; change your setting of OBJS to this:

# object file list
OBJS := $(SRCS:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)

Then tell make how to build it; change your pattern rule for building objects to this:

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
        @mkdir -p $(@D)
        $(CC) $(CFLAGS) $(INC_FLAGS) -c $< -o $@
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your help. However, it doesn't compile, I get "mkdir: missing operand"
It seems like you must have mistyped something, or else maybe your value of OBJ_DIR variable is something weird? However, that's a very strange error message. What version of Linux are you using? You can try removing the @ from @mkdir so you can see the command line to see what it says.
I am working on Ubuntu 20.04.3 LTS. When I remove the @ from @mkdir I only see the command mkdir -p. @D seems to be empty.
It's just not possible for the @D variable to be empty. Make sets it to a value before it expands your recipe. Please be sure you haven't typo'd something about the variable name, or added some kind of weird non-printing character, or something like that. What is the value you set your OBJ_DIR variable to? What is the value you set your SRC_DIR variable to? You don't show these in your makefile.
Also, you can try adding the --warn-undefined-variables option when invoking make, and make will tell you if you've used the wrong variable name.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.