[英]MakeFile:Force all files under directory to include header
我當前的目錄如下所示:
root
|___src
| |
| |____Makefile
| |____a.cpp
| |____b.cpp
| |____c.h
| |____tests
| |
|______________|____test.cpp
有沒有辦法從 makefile 強制測試目錄下的所有文件包含 ch
你還沒有回答我的任何一個問題,所以我不得不做一些猜測。
這可能是您正在尋找的:
%.o: %.cpp
@$(CXX) -c $< -o $@
tests/%.o: tests/%.cpp
@$(CXX) -include c.h -c $< -o $@
編輯:
如果你想在源文件的頂部插入#include "ch"
,你可以使用這個 sed 命令:
sed -i "" '1{h;s/.*/#include "c.h"/;G;}' filename
如果您想確保不會多次這樣做,您可以這樣做:
sed -i "" '1{h;s/.*/#include "c.h"/;G;s/\(.*\)\n\1$/\1/;}' filename
有幾種不同的方法可以使用 makefile 執行此操作。 這是一種粗略但有效的方法:
tests/%.o: tests/%.cpp
@sed -i "" '1{h;s/.*/#include "c.h"/;G;s/\(.*\)\n\1$/\1/;}' $<
@$(CXX) -c $< -o $@
但是如果你只在每個源文件上運行 sed 一次,Make 會工作得更好。 這可以做到,但需要更仔細地考慮優先級。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.