Linux學習筆記 子目錄的支援

2021-08-25 19:25:39 字數 1231 閱讀 4987

在實際的工程專案中會使用子目錄:

通常,乙個專案下會有多個子目錄,乙個子目錄通常在邏輯上是乙個模組(不是強制要求)

makefile相當於專案級的檔案

如:

mytestpro/

makefile

src/

main.cpp

object/

object.cpp

object.h

在makefile中,要自動羅列出子目錄下的cpp檔案,使用foreach

foreach相當於for迴圈;

dir相當於乙個迭代器,像int i=0,i++這些;

第三個引數就是乙個迴圈體;

檔案結構如上面所屬,原始碼如下:

object.h

struct mypro

};void printhello();

object.cpp

#include "object.h"

#include void printhello()

main.cpp

#include "../object/object.h"

#include int main()

makefile

exe=helloworld

subdir= src object

cxx_sources=$(foreach dir,$(subdir), $(wildcard $(dir)/*.cpp))

cxx_object=$(patsubst %.cpp, %.o, $(cxx_sources))

dep_files=$(patsubst %.o, %d, $(cxx_object))

$(exe): $(cxx_object)

g++ $(cxx_object) -o $(exe)

%.o: %.cpp

g++ -c -mmd $< -o $@

-include $(dep_files)

clean:

rm -rf $(cxx_object) $(dep_files) $(exe)

執行截圖如下:

linux遍歷目錄及其子目錄

1 在linux下遍歷某一目錄下內容linux下歷遍目錄的方法一般是這樣的 2 開啟目錄 讀取 關閉目錄 4 include 5 dir opendir const char dirname 6 struct dirent readdir dir dirp 7 int closedir dir di...

Linux中的子目錄介紹

bin 普通使用者的命令 dev 系統中所有裝置檔案 home 普通使用者工作目錄 lost found 發生意外後丟失的檔案 misc 系統配置檔案 net 網路協議狀態資訊 proc 偽檔案系統 sbin 超級使用者命令 srv 網路服務啟動之後所需要取用的資料目錄 tmp 讓一般使用者或者正在...

linux 根目錄下的子目錄的意義

bin 可執行程式,通常抱括系統工具 boot 系統啟動時用於初始化linux的檔案,比如linux核心 dev 鏈結到硬體資源的檔案 etc 配置檔案,特別是系統工具和網路服務 home 所有普通使用者的home目錄 lib 系統工具和核心所使用的系統庫 root 超級使用者目錄 sbin 僅由r...