{"id":28321005,"url":"https://github.com/umaumax/make-example","last_synced_at":"2025-07-26T21:11:52.890Z","repository":{"id":154771444,"uuid":"164892349","full_name":"umaumax/make-example","owner":"umaumax","description":null,"archived":false,"fork":false,"pushed_at":"2021-07-03T15:12:11.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-23T13:48:52.469Z","etag":null,"topics":["cpp","make","makefile"],"latest_commit_sha":null,"homepage":null,"language":"Makefile","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/umaumax.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}},"created_at":"2019-01-09T15:52:01.000Z","updated_at":"2021-07-03T15:12:13.000Z","dependencies_parsed_at":"2023-05-20T08:15:21.040Z","dependency_job_id":null,"html_url":"https://github.com/umaumax/make-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/umaumax/make-example","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umaumax%2Fmake-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umaumax%2Fmake-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umaumax%2Fmake-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umaumax%2Fmake-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/umaumax","download_url":"https://codeload.github.com/umaumax/make-example/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/umaumax%2Fmake-example/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267236922,"owners_count":24057658,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-26T02:00:08.937Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["cpp","make","makefile"],"created_at":"2025-05-25T12:12:52.436Z","updated_at":"2025-07-26T21:11:52.881Z","avatar_url":"https://github.com/umaumax.png","language":"Makefile","funding_links":[],"categories":[],"sub_categories":[],"readme":"# make example\n\n## how to run\n``` bash\nmake CXX=clang++\nmake clean\nmake\nmake test\n```\n\n----\n\n## memo\n* `:=`: 変数を再帰的に展開しないで代入(基本的にはこの代入を使うべき)\n  * `CC := $(CC)`: 環境変数で初期化(default:`${CXX:-cc}` at darwin)\n  * `CXX := $(CXX)`: 環境変数で初期化(default:`${CXX:-c++}` at darwin)\n* `?=`: デフォルト値を設定(環境変数/`make XXX=123`などの方が優先度が高い)\n  * `RANLIB ?= ranlib`\n* `+=`: 純粋なMakefile内の変数に対して追記する(`hoge := $(hoge) xxx`と同等)\n\n## デフォルトの`CXX`の値を設定しつつ，`$CXX`の値で上書き可能な設定にする方法は?\n1. makfileのみで対応する方法\n`Makefile`\n``` make\nifneq ($(shell echo $${CXX}),)\n\tCXX := $(CXX)\nelse\n\tCXX := g++\nendif\n\n# or simple way\nCC := $(if $(CC),$(CC),gcc)\nCXX := $(if $(CXX),$(CXX),g++)\nAR := $(if $(AR),$(AR),ar)\nSTRIP := $(if $(STRIP),$(STRIP),strip)\nRANLIB := $(if $(RANLIB),$(RANLIB),ranlib)\n```\n\n[Makefile set if variable is empty \\- Stack Overflow]( https://stackoverflow.com/questions/38801796/makefile-set-if-variable-is-empty )\n\n2. 運用でカバーする方法\n``` bash\n$ make CXX=g++\n```\n__makeコマンドのオプションで指定した値はソースコード内では全く変更ができないことに注意(環境変数指定の場合は変更可能)__\n(e.g. `+=`での追記などが有効でなくなる)\n\n`Makefile`\n``` make\nCXX := $(if $(CXX),$(CXX),g++)\n$(info [DEBUG] $$CXX is [${CXX}])\nCXX := 'XXX'\n$(info [DEBUG] $$CXX is [${CXX}])\n\nall:\n```\n\n``` bash\n$ make\n[DEBUG] $CXX is [c++]\n[DEBUG] $CXX is ['XXX']\nmake: Nothing to be done for `all'.\n$ make CXX=piyo\n[DEBUG] $CXX is [piyo]\n[DEBUG] $CXX is [piyo]\nmake: Nothing to be done for `all'.\n$ CXX=piyo make\n[DEBUG] $CXX is [piyo]\n[DEBUG] $CXX is ['XXX']\nmake: Nothing to be done for `all'.\n```\nちなみに，`'XXX'`とするとシングルクォートも値に含まれることに注意\n\nFYI: [make と環境変数 – talkwithdevices\\.com]( https://www.talkwithdevices.com/archives/49 )\n\n### Makefile:xxx: *** missing separator.  Stop.\nUSE __tab__ not space!\n\n### 変数の値を確認したい\n`make var`\n```\nvar:;: CC:'$(CC)' CXX:'$(CXX)' RANLIB:'$(RANLIB)'\n```\n\n## how to generate asm\n* NOTE: `.o`から実行ファイルを作成するときの`-S`は意味がないよう(これは`.o`が複数ある場合も)\n* NOTE: CCもCXXも両方CXX.shで処理してしまっている\n``` bash\nmake clean\necho '#!/usr/bin/env bash' \u003e make.asm.sh; chmod u+x make.asm.sh; \\\nCC=\"$PWD/CXX.sh $PWD '$CC'\" CXX=\"$PWD/CXX.sh $PWD '$CXX'\" make\nmake\nbash -ex ./make.asm.sh\n```\n\nMakefileによっては，下記のような出力にCXXが利用されているので，注意(-oではない)\n``` bash\nCXX.sh g++ -M main.cpp \\\n                | sed \"s;^.*\\.o[ :]*;obj/\u0026;\" \u003e obj/_depend_\n```\n\n## 終了コード\nmakeは各シェルコマンドの終了コードを調べ，エラーの場合は中断するが，コマンドの先頭に`-`を付加すれば終了コードを無視する\n``` make\nclean:\n        -rm -f *.o\n```\n\n## include flags\n``` make\nINCLUDE_FILES = \\\n\t./xxx \\\n\t./yyy \\\n\t./zzz\nINCLUDE_FLAG := $(addprefix -I,$(INCLUDE_FILES))\n# $(info [DEBUG] $$INCLUDE_FLAG is [${INCLUDE_FLAG}])\n```\n\n## lib flags\n``` make\nLIB_FILES = \\\n\txxx \\\n\tyyy \\\n\tzzz\nLIB_FLAG := $(addprefix -l,$(LIB_FILES))\n# $(info [DEBUG] $$LIB_FLAG is [${LIB_FLAG}])\n```\n\n## 文字列パターン\n`%`は複数利用できない\n\n[gnu make \\- Makefile: Filter out strings containing a character \\- Stack Overflow]( https://stackoverflow.com/questions/6145041/makefile-filter-out-strings-containing-a-character )\n\u003e As the documentation says, only the first % character is a wildcard -- subsequent % characters match literal % characters in whatever you are matching. So your command filters out names that end in g%\n\n## filter-out\n`%xxx%`のパターンでフィルタイングを行いたい\n\n* [make\\-filter\\-out \\- filter\\-out関数の使い方 \\- spikelet days]( https://taiyo.hatenadiary.org/entry/20080402/p1 )\n* [gnu make \\- Makefile: Filter out strings containing a character \\- Stack Overflow]( https://stackoverflow.com/questions/6145041/makefile-filter-out-strings-containing-a-character )\n\n``` make\nlist := __ hello makefile world __\nsubmatch-filter-out = $(foreach v,$(2),$(if $(findstring $(1),$(v)),,$(v)))\n# NOTE: :2nd argに指定する文字列の前にスペースを設けるとそのスペースを含めたパターンとなってしまうため注意\nfiltered_list:=$(call submatch-filter-out,e,$(list))\n$(info [DEBUG] $$filtered_list is [${filtered_list}])\n```\n\n## debug\n### CC, CXXの使い方の例\n``` bash\nCXX=\"echo $PWD | grep xxx; echo\" make\nCXX=\":\" make\nCXX=\"echo\" make\nCXX=\"pwd; clang++ -flto\" CC=\"pwd; clang -flto\" make |\u0026 tee build.log | ccze -A\n# NOTE: force -O0 hack one liner (you may use cxx_hook.sh)\n#  becase cmake parse command ' ' as ';'\n# NOTE: $@ will be replaced by make command\n# NOTE: CXX is run by /bin/sh -c\n# for darwin sh\nmake CXX='bash -xc \"clang++ -g -flto `echo \\\\\\x24\\\\\\x40` -O0\" -- '\n# for ubuntu sh: JEAK is base64 encode str of \"$@\"\nmake CXX='bash -xc \"clang++ -g -flto `echo JEAK | base64 -d` -O0\" -- '\n```\n\nNOTE: `clang++` parse last `-OX` option\n\n## checkmake warnings\n### Missing required phony target \"test\"\n`.PHONY: test`を作成することで解決\ne.g.\n``` make\n.PHONY: test\ntest:\n\techo \"[TEST] OK\"\n```\n\n## error messages\n### Makefile:xxx: *** missing separator.  Stop.\n返り値の処理をしていない場合\n``` make\n$(shell echo 1)\n```\n正しいパターン\n``` make\nret=$(shell echo 1)\n```\n\n## タスク定義名が、プロジェクトのファイル名やディレクトリ名と同じ場合\n\n\u003e makeの仕様でタスク定義名と同じファイルが存在している場合はタスクが実行されません。\n\u003e これを回避するためには、.PHONY: task をタスク定義に付けます。\n\n``` make\n.PHONY: task\ntask:\n\tcommand\n```\n\n## 自身のMakefileに定義してある別のタスク定義を実行したい場合\n\n``` make\n$(MAKE) task1\n```\n\n\u003e $(MAKE) ではなく、 make で書いてもこのケースでは動作しますが、\n\u003e make task2 に付けたオプションも引き継がせるためには$(MAKE)を使います。\n\n## タスクコマンド実行\n\u003e タスク実行中に環境変数を書き換えたい場合は、コマンド実行が1行終わるごとに環境変数がmakeコマンド実行時に戻ることに注意します。\n\u003e `;\\`で改行してコマンドを複数書きます。\n\n## foreach\n* [makefileのforeachのハマりどころ \\- podhmo's diary]( https://pod.hatenablog.com/entry/2018/05/10/194204 )\n\n----\n\n## FYI\n* [Makefileの書き方 \\- $ cat /var/log/shin]( http://shin.hateblo.jp/entry/2012/05/26/231036#fn1 )\n* [2016年だけどMakefileを使ってみる \\- Qiita]( https://qiita.com/petitviolet/items/a1da23221968ee86193b )\n* [cpp\\-intro/002\\-build\\.md at master · EzoeRyou/cpp\\-intro]( https://github.com/EzoeRyou/cpp-intro/blob/master/002-build.md#make-%E3%83%93%E3%83%AB%E3%83%89%E3%82%B7%E3%82%B9%E3%83%86%E3%83%A0 )\n* [Makefile の特殊変数・自動変数の一覧]( https://tex2e.github.io/blog/makefile/automatic-variables )\n* [Makeについて知っておくべき7つのこと \\| POSTD]( https://postd.cc/7-things-you-should-know-about-make/ )\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fumaumax%2Fmake-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fumaumax%2Fmake-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fumaumax%2Fmake-example/lists"}