2016-12-08 5 views
1

나는 리눅스 메이크 파일을 사용하는 방법을 배우려하고있다. 링크하기 전에 메이크 파일 정리 개체를 방지하는 방법 (-j 옵션 사용)

I가 다음 두 파일 :

makefile.config

$(info Starting building process) 

# include configurations 

include makefile.conf 

$(info - Makefile.conf loaded) 

# find project files 

H_FILES := $(shell find -L ./ -name '*.h' -exec dirname {} \; | sed 's/ /\\ /g' | uniq) 

C_FILES := $(shell find ./ -name '*.c' -type f | sed 's/ /\\ /g' | uniq) 

CXX_FILES := $(shell find ./ -name '*.cpp' -type f | sed 's/ /\\ /g' | uniq) 

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

O_FILES += $(CXX_FILES:.cpp=.o) 

H_FILES := $(notdir $(H_FILES)) 

C_FILES := $(notdir $(C_FILES)) 

CXX_FILES := $(notdir $(CXX_FILES)) 

INCLUDES := $(H_FILES:%=-I%) 

$(info - Project Files Loaded) 


ifeq ($(DEBUG),yes) 

    $(info - Debug flag added [makefile.conf DEBUG = yes]) 

    CFLAGS := -g3 $(CFLAGS) 

endif 


ifeq ($(IS_LIBRARY),yes) 

    $(info - Set Parameters for Shared Library build process) 

    ALL_PARAMETERS = lib$(PROJECT_NAME).so.$(PROJECT_VERSION) clean 

    ALL_TYPE = lib$(PROJECT_NAME).so.$(PROJECT_VERSION): $(O_FILES) 

    LIBFLAGS = -shared -Wl,-soname,lib$(PROJECT_NAME).so 

    CFLAGS := -fPIC $(CFLAGS) 

    CXXFLAGS := -fPIC $(CXXFLAGS) 

else 

    $(info - Set Parameters for Application build process) 

    ALL_PARAMETERS = $(PROJECT_NAME) clean 

    ALL_TYPE = $(PROJECT_NAME): $(O_FILES) 

    LIBFLAGS = 

endif 

# Build Process 

all: $(ALL_PARAMETERS) 

$(ALL_TYPE) 
    @echo - [OUTPUT][CXX] [email protected] @[$(BIN_DIRECTORY)] 
    @$(CXX) $(CFLAGS) $(INCLUDES) $(LDFLAGS) $(LIBFLAGS) -o $(BIN_DIRECTORY)/[email protected] $^ $(LDLIBS) 

%.o: %.c 
    @echo - [CC] [email protected] 
    @$(CC) $(CFLAGS) -c $(INCLUDES) -o [email protected] $< $(LFLAGS) 

%.o: %.cpp 
    @echo - [CXX] [email protected] 
    @$(CXX) $(CXXFLAGS) -c $(INCLUDES) -o [email protected] $< $(LFLAGS) 

# Clear Objects 

clean: 
    $(info - Remove all .o [object] files) 
    @find . -name \*.o -type f -delete 

# Clear Objects & Executables 

cleanall: 
    $(info - Remove all .o [object] files) 
    @find . -name \*.o -type f -delete 
    $(info - Remove all files in $(BIN_DIRECTORY)) 
    @find $(BIN_DIRECTORY) -name \*.* -type f -delete 

# Install Project 

install: 
    @cp -r $(BIN_DIRECTORY)/lib$(PROJECT_NAME).so.$(PROJECT_VERSION) $(INS_SO_DIRECTORY) 
    @cp -r $(LIB_DIRECTORY)/* $(INS_HEARERS_DIRECTORY) 
    @ln -s $(INS_SO_DIRECTORY)/lib$(PROJECT_NAME).so.$(PROJECT_VERSION) $(INS_SO_DIRECTORY)/lib$(PROJECT_NAME).so 
    @ldconfig 
    $(info - Installation completed) 

# Uninstall Project 

uninstall: 
    @rm $(INS_SO_DIRECTORY)/lib$(PROJECT_NAME).so 
    @rm $(INS_SO_DIRECTORY)/lib$(PROJECT_NAME).so.*.* 
    @rm $(INS_HEARERS_DIRECTORY)/$(PROJECT_NAME).h 
    @ldconfig 
    $(info - Uninstallation completed) 

모든 것이 완벽하게 작동

# Project Configurations 

    # Project Name 

    PROJECT_NAME    =   myapp 

    # Project Version 

    PROJECT_VERSION   =   0.1 

    # Program or Shared Library 

    IS_LIBRARY     =   no 

    # Source Files Directory 

    SRC_DIRECTORY    =   src 

    # Header Files Directory 

    INC_DIRECTORY    =   inc 

    # Library Header Files Directory 

    LIB_DIRECTORY    =   lib 

    # Build Output Directory 

    BIN_DIRECTORY    =   ../Executable 

    # Installation Directory Exec 

    INS_DIRECTORY    =   /usr/local/bin/ 

    # Installation Directory Headers 

    INS_HEARERS_DIRECTORY  =   /usr/local/include/ 

    # Installation Directory SO  

    INS_SO_DIRECTORY   =   /usr/local/lib/ 

    # C Flags 

    CFLAGS      =   -O3 -D_GNU_SOURCE -Wfatal-errors 

    # C++ Flags 

    CXXFLAGS     =   $(CFLAGS) 

    # Linker Flags 

    LDFLAGS     =   $(shell dpkg-buildflags --get LDFLAGS) 

    # Linker Libraries 

    LDLIBS      =   -fno-inline 

    # Debug Yes/No 

    DEBUG      =   yes 

    # C Compiler 

    CC       =   gcc 

    # C++ Compiler 

    CXX      =   g++ 

메이크 ... 음 적어도 내가 그것을 사용하고 무엇을위한 .. 그러나 makefile을 -j 옵션과 함께 실행하려고 할 때 (예 :

)
$ make -j4 

프로 시저를 연결하기 전에 개체 (.o)를 정리합니다.

[email protected]:Source Code$ make -j4 
Starting building process 
- Makefile.conf loaded 
- Project Files Loaded 
- Debug flag added [makefile.conf DEBUG = yes] 
- Set Parameters for Shared Library build process 
- [CXX] src/isca-streamer.o 
- [CXX] src/isca-streamer-pause.o 
- [CXX] src/isca-streamer-thread-image.o 
- [CXX] src/isca-streamer-initialize.o 
- [CXX] src/isca-streamer-start.o 
- [CXX] src/isca-streamer-stop.o 
- [CXX] src/isca-streamer-thread-socket.o 
- [CXX] src/isca-streamer-clear.o 
- Remove all .o [object] files 
- [CXX] src/isca-streamer-settings.o 
- [OUTPUT][CXX] libisca-streamer.so.0.1 @[../Executable] 
g++: error: src/isca-streamer.o: No such file or directory 
g++: error: src/isca-streamer-initialize.o: No such file or directory 
g++: error: src/isca-streamer-thread-image.o: No such file or directory 
g++: error: src/isca-streamer-pause.o: No such file or directory 
g++: error: src/isca-streamer-start.o: No such file or directory 
g++: error: src/isca-streamer-stop.o: No such file or directory 
Makefile:71: recipe for target 'libisca-streamer.so.0.1' failed 
make: *** [libisca-streamer.so.0.1] Error 1 

이 문제는 어떻게 해결할 수 있습니까? 이 링커와 병렬로 실행하고 파일을 삭제하면 훨씬 빠르게 할 수 있기 때문에, 당신은 -j를 사용하는 경우 당신은 당신의 all 대상에 대한 전제 조건으로 clean을 추가 할 수 없습니다

답변

2

(단계를 연결하기 전에 단계를 수행하기 위해 개체를 청소 방지) 그들을 연결하는 것보다.

ALL_PARAMETERS 변수에서 clean 타겟을 제거해야하므로 더 이상 링크 줄 중간에있는 객체는 삭제되지 않습니다.

모든 실행 후에 모든 개체를 삭제하려는 이유가 전혀 없습니다. 매번 처음부터 모든 것을 다시 작성하려면 make 또는 makefiles가 필요하지 않습니다. 단지 쉘 스크립트 만 작성하면됩니다. makefile을 사용하는 전체 요점은 변경되지 않은 프로젝트의 부분을 다시 빌드하지 않는 것입니다.

하지만 실제로 그렇게하고 싶다면 가장 간단한 방법은 재귀 적 make를 사용하는 것입니다. 예를 들면 : all의 모든 전제 조건이 완전히 구축 한 후

all: $(ALL_PARAMETERS) 
     $(MAKE) clean 

은 오직 당신이 -j 실행하는 경우에도, 청소하게됩니다.

+0

감사합니다! .. 당신 말이 맞습니다. 매번 실행 후 개체를 삭제해서는 안됩니다. – iikem