簡體   English   中英

通用Makefile

[英]Generic Makefile

我正在尋找一個通用的makefile,它將在當前目錄和所有子目錄(例如源代碼,測試文件,gtest等)中構建所有C ++文件。

我花了幾個小時嘗試了幾次,最終確定了源子目錄的make文件解決方案。

我需要對其進行三處更改:

  1. Gtest的C ++文件使用* .cc,我還有其他人使用* .cpp
  2. 我需要能夠定義多個搜索路徑。
  3. 添加編譯器標志,例如-W all

我設法破壞了makefile,如下所示,這樣運行make就可以

make:***沒有規則可以使myProgram'成為目標%.cpp=%.o', needed by '。 停止。

我該如何做這三件事?

# Recursively get all *.cpp in this directory and any sub-directories
SRC = $(shell find . -name *.cc) $(shell find . -name *.cpp)

INCLUDE_PATHS = -I ../../../ -I gtest -I dummies

#This tells Make that somewhere below, you are going to convert all your source into 
#objects
# OBJ =  src/main.o src/folder1/func1.o src/folder1/func2.o src/folder2/func3.o

OBJ = $(SRC:%.cc=%.o %.cpp=%.o)

#Tells make your binary is called artifact_name_here and it should be in bin/
BIN = myProgram

# all is the target (you would run make all from the command line). 'all' is dependent
# on $(BIN)
all: $(BIN)

#$(BIN) is dependent on objects
$(BIN): $(OBJ)
    g++ 

#each object file is dependent on its source file, and whenever make needs to create
# an object file, to follow this rule:
%.o: %.cc
    g++ -c $(INCLUDE_PATHS) $< -o $@

[更新]謝謝您到目前為止的幫助。 為了解決一些意見,我無法控制混合的* .cc和* .cpp擴展名,我可以說目錄樹中永遠不會有我不希望包含的源文件。在構建中。

我仍無法使用SRC,因為找不到輸入文件。 我想我應該更多地考慮find命令,因為自從我使用Linux已經有一段時間了。

伊坦指出了您的問題。 但是您不必執行兩次替換,只需:

OBJ := $(addsuffix .o,$(basename $(SRCS)))

並且,在使用shell函數時,應該始終使用:=而不是=進行分配。

那是一個相當糟糕的makefile:它不會為您構建頭文件依賴項,因此您最終可能會損壞構建文件。

我毫不客氣地推薦這個

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM