当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Make recompiles non modified files

问题描述:

I have a makefile for my program but I got everything recompiled every time I run it, even if I modify nothing.

Every time I run make it recompiles simHwIntf.cpp showHelp.cpp and sendFromFile.cpp

This is my make file:

IDIR = inc

LDIR = -L/usr/lib/x86_64-linux-gnu/

SDIR = src

ODIR = obj

BINDIR = bin

LDLIBS = -luhd

OBJ = $(patsubst %,$(ODIR)/%,$(O_FILES))

CC = g++

CFLAGS = -Wall -std=c++11 -I $(IDIR) #-Werror

BINARIES= main

C_FILES = simHwIntf.cpp showHelp.cpp sendFromFile.cpp

H_FILES = simHwIntf.h

O_FILES = $(C_FILES:.cpp=.o)

all: $(BINARIES)

@echo "Make file executed"

$(BINARIES): $(O_FILES)

$(CC) $(CFLAGS) -o $(BINDIR)/[email protected] $(OBJ) $(LDIR) $(LDLIBS)

fileCreator: fileCreator.o

$(CC) $(CFLAGS) -o $(BINDIR)/[email protected] $(ODIR)/fileCreator.o

fileHandler: fileHandler.o

$(CC) $(CFLAGS) -o $(BINDIR)/[email protected] $(ODIR)/fileHandler.o

backYard: backYard.o

$(CC) $(CFLAGS) -o $(BINDIR)/[email protected] $(ODIR)/backYard.o

%.o: $(SDIR)/%.cpp $(IDIR)/$(H_FILES)

$(CC) $(CFLAGS) -c -o $(ODIR)/[email protected] $<

clean:

-rm -rf $(ODIR)/*.o *~

distclean: clean

-rm -rf $(BINDIR)/*

Each time the output in the shell is:

g++ -Wall -std=c++11 -I inc -c -o obj/simHwIntf.o src/simHwIntf.cpp

g++ -Wall -std=c++11 -I inc -c -o obj/showHelp.o src/showHelp.cpp

g++ -Wall -std=c++11 -I inc -c -o obj/sendFromFile.o src/sendFromFile.cpp

g++ -Wall -std=c++11 -I inc -o bin/main obj/simHwIntf.o obj/showHelp.o obj/sendFromFile.o -L/usr/lib/x86_64-linux-gnu/ -luhd

Make file executed

I've already search and read this: (How do I make Makefile to recompile only changed files?) but didn't help much.

Anybody that could give me a hand with this ?

I have a doubt with the directories, maybe one or several directories are re-created each time I run make and this causes everything inside to look like new to the compiler.

Thanks

网友答案:

You can see what triggered the build by echoing the dependencies that changed. Add this to your %.o target :

@echo [triggered by changes in $?]

You should also use the VPATH special variable instead of specifying the sources path in your %.o target. See GNU make VPATH documentation

网友答案:

Please try replacing

%.o: $(SDIR)/%.cpp $(IDIR)/$(H_FILES)
     $(CC) $(CFLAGS) -c -o $(ODIR)/[email protected] $<

with

$(ODIR)/%.o: $(SDIR)/%.cpp $(IDIR)/$(H_FILES)
     $(CC) $(CFLAGS) -c -o $(ODIR)/[email protected] $<
网友答案:

Directories matter when you define targets.

If a define a rule

myexec: objdir/myexec.o
    $(CC) $(CFLAGS) -o bindir/myexec objdir/myexec.o $(LDFLAGS)

Make believes that that this would create the file myexec in the working directory. When you rerun make the target myexec wasn't found, so it will be created again. Add the paths in the targets and it should work.

Try replacing

BINARIES= main

with

BINARIES= $(BINDIR)/main

and the rule

$(CC) $(CFLAGS) -o $(BINDIR)/[email protected] $(OBJ) $(LDIR) $(LDLIBS)

with

$(CC) $(CFLAGS) -o [email protected] $^ $(LDIR) $(LDLIBS)

And change the other rules similarly.

Note, in general it is a bad idea to use [email protected] in combination with a path when creating the target in some rule (as in $(BINDIR)/[email protected]), because this will never create the actual target file. A bare [email protected] should be sufficient.

分享给朋友:
您可能感兴趣的文章:
随机阅读: