{"id":25208230,"url":"https://github.com/1010code/makefile-tutorial","last_synced_at":"2026-04-16T01:33:34.033Z","repository":{"id":118676458,"uuid":"529784271","full_name":"1010code/makefile-tutorial","owner":"1010code","description":"Using make and writing Makefile","archived":false,"fork":false,"pushed_at":"2023-04-07T16:25:34.000Z","size":70,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-05T13:42:57.294Z","etag":null,"topics":["c","cpp","cpp11","makefile"],"latest_commit_sha":null,"homepage":"https://andy6804tw.github.io/2022/08/27/makefile/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/1010code.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2022-08-28T06:42:20.000Z","updated_at":"2022-09-02T13:02:29.000Z","dependencies_parsed_at":null,"dependency_job_id":"28358510-5f7e-45fa-bd52-a84f2d322759","html_url":"https://github.com/1010code/makefile-tutorial","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/1010code/makefile-tutorial","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fmakefile-tutorial","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fmakefile-tutorial/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fmakefile-tutorial/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fmakefile-tutorial/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/1010code","download_url":"https://codeload.github.com/1010code/makefile-tutorial/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/1010code%2Fmakefile-tutorial/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31867711,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-15T15:24:51.572Z","status":"ssl_error","status_checked_at":"2026-04-15T15:24:39.138Z","response_time":63,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","cpp","cpp11","makefile"],"created_at":"2025-02-10T12:19:08.352Z","updated_at":"2026-04-16T01:33:34.026Z","avatar_url":"https://github.com/1010code.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 在 C++ 中使用 Makefile\n\n一般編譯需要將而外引用的函式庫檔案位置附上：\n\n```sh\ng++ main.cpp function1.cpp function2.cpp -o main\n```\n\n若當有超過幾十隻附加檔案時，逐一的在指令上添加檔案路徑是非常辛苦的一件事。因此才需要透過 Makefile 幫我們管理這些檔案，讓編譯器在編譯的時候知道要處理哪些檔案。並且只編譯更動過的檔案，這意味著若先前已編譯過檔案若之後沒做更動時不會再花時間編譯一次，這動作大幅降低整體編譯的時間。\n\nMakefile 會先執行 all 區段，後面的 hello 是代表相依要被執行的區段。因此會掉到 hello，裏面也有相依的內容 `main.o function1.o function2.o`，接著第一步會先編譯 main.o 他的相依程式為 main.cpp 並使用 gcc 指令編譯 `.o` 檔。\n\n```makefile\nall: hello\n\nhello: main.o function1.o function2.o\n\t\tg++ main.o function1.o function2.o -o main\n\nmain.o: main.cpp\n\t\tg++ -c main.cpp\n\nfunction1.o: function1.cpp\n\t\tg++ -c function1.cpp\n\nfunction2.o: function2.cpp\n\t\tg++ -c function2.cpp\n\nclean:\n\t\trm -rf *o main\n```\n\n![](./screenshot/img01.png)\n\nMakefile 進階版寫法，提供變數可以省去重複性高的內容並且好管理指令 `-c` 代表編譯但不進行鏈結的意思 `-Wall` 代表編譯時顯示所有的警告訊息。\n\n```makefile\nCC=g++\nCFLAGS=-c -Wall\n\nall: hello\n\nhello: main.o function1.o function2.o\n\t\t$(CC) main.o function1.o function2.o -o main\n\nmain.o: main.cpp\n\t\t$(CC) $(CFLAGS) main.cpp\n\nfunction1.o: function1.cpp\n\t\t$(CC) $(CFLAGS) function1.cpp\n\nfunction2.o: function2.cpp\n\t\t$(CC) $(CFLAGS) function2.cpp\n\nclean:\n\t\trm -rf *o main\n```\n\n\n## Reference\n\n- [Using make and writing Makefile ( in C++ or C )](https://www.youtube.com/watch?v=aw9wHbFTnAQ)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1010code%2Fmakefile-tutorial","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F1010code%2Fmakefile-tutorial","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F1010code%2Fmakefile-tutorial/lists"}