2017-08-11 18 views
1

在任何人,標誌着這是一個重複的,讓我說,我已經看過了這些問題無濟於事:如何編譯SRC的.cpp /和在建/輸出的.o

Makefiles: Get .cpp from one directory and put the compiled .o in another directory
Can I compile all .cpp files in src/ to .o's in obj/, then link to binary in ./?
Makefile, find sources in src directory tree and compile to .o in build file
C++ Makefile. .o in different subdirectories
Makefile: Compiling from directory to another directory

無論這些解決方案我嘗試,我不能讓這個規則來建立正確:

$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp 
    $(CC) $(CPPFLAGS) -o [email protected] $< 

以下是完整的生成文件:

CPPFLAGS = -c -Wall -g \ 
     -I/usr/include/glib-2.0 \ 
     -I/usr/lib/x86_64-linux-gnu/glib-2.0/include \ 
     -I/user/include/gdk-pixbux-2.0 \ 
     -Iinclude 
LFLAGS = -Wall -g 
LIB = -lnotify 
SRC_DIR = src 
INCL_DIR = include 
OBJ_DIR = build 
TARGET_DIR = bin 
SRC := $(subst $(SRC_DIR)/,,$(wildcard src/*.cpp)) 
INCL := $(subst $(INCL_DIR)/,,$(wildcard include/*.hpp)) 
OBJ := $(SRC:.cpp=.o) 
TARGET = reminders 
CC = g++ 

.PHONY: clean 

$(TARGET_DIR)/$(TARGET): $(OBJ_DIR)/$(OBJ) 
     $(CC) $(LFLAGS) -o [email protected] $^ $(LIB) 
     @echo Build successful 

$(OBJ_DIR)/$(OBJ): $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp 
     $(CC) $(CPPFLAGS) -o [email protected] $< 

clean: 
     rm -rf *.o build/* bin/* *~ 

在我的項目文件夾中我有一個src,bin和建立文件夾 在我的項目文件夾的main.cpp,reminder.hpp和控制器。 CPP。 這是我所得到的,當我運行make:

Makefile:24: target 'main.o' doesn't match the target pattern 
g++ -c -Wall -g -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/user/include/gdk-pixbux-2.0 -Iinclude -o mai 
g++: fatal error: no input files 
compilation terminated. 
Makefile:25: recipe for target 'main.o' failed 
make: *** [main.o] Error 1 
shell returned 2 

回答

3

我勸你學習測試的習慣...好... 一切

假設你的源目錄包含

blue.cpp main.cpp red.cpp 

現在,在你的makefile,試試這個:

$(info $(OBJ_DIR)/$(OBJ)) 

這將產生:

obj/blue.o main.o red.o 

確認!現在你知道你的規則爲什麼失敗了。你不能以這種方式添加前綴。模式爲$(OBJ_DIR)/%.o,但目標main.o與模式不匹配。

試試這個:

SRC := $(wildcard $(SRC_DIR)/*.cpp) # src/blue.cpp ... 
OBJ := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRC)) # obj/blue.o ... 

$(TARGET_DIR)/$(TARGET): $(OBJ) 
    $(CC) $(LFLAGS) -o [email protected] $^ $(LIB) 
    @echo Build successful 

$(OBJ): $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp 
    $(CC) $(CPPFLAGS) -o [email protected] $< 
相關問題