ホームページ  >  記事  >  運用・保守  >  Linuxでmakeコマンドを使う方法

Linuxでmakeコマンドを使う方法

WBOY
WBOY転載
2023-05-15 12:37:124489ブラウズ

make の仕組み
その背後にある仕組みを知らない人のために説明すると、make コマンドはコマンド ライン引数と同じようにターゲットを受け入れます。これらのターゲットは通常、「makefiles」という名前の特別なファイルに保存され、ファイルにはターゲットに対応する操作も含まれています。詳細については、Makefile の仕組みに関するこの一連の記事を参照してください。

make コマンドが初めて実行されると、makefile がスキャンされてターゲットとその依存関係が検索されます。これらの依存関係自体がターゲットである場合は、これらの依存関係のメイクファイルのスキャンを続けて依存関係を確立し、コンパイルします。主要な依存関係がコンパイルされると、次にメイン ターゲットがコンパイルされます (これは make コマンドを通じて渡されます)。

ここで、あるソース ファイルを変更した後、再度 make コマンドを実行すると、そのソース ファイルに関連するターゲット ファイルのみがコンパイルされるため、最終的な実行可能ファイルをコンパイルすると大幅なコストが節約されます。時間。

make コマンドの例
この記事で使用したテスト環境は次のとおりです:

os —— ubunut 13.04
shell —— bash 4.2.45
application —— gnu make 3.81

プロジェクトの内容は次のとおりです:

$ ls 
anothertest.c makefile test.c test.h

以下は makefile の内容です:

all: test 

test: test.o anothertest.o 
  gcc -wall test.o anothertest.o -o test

test.o: test.c 
  gcc -c -wall test.c 

anothertest.o: anothertest.c 
  gcc -c -wall anothertest.c 

clean: 
  rm -rf *.o test

次に、Linux での make コマンド アプリケーションの例をいくつか見てみましょう:

1. 簡単な例

##プロジェクト全体をコンパイルするには、単に make を使用するか、make コマンドの後にターゲット all を指定して実行するだけです。

$ make 
gcc -c -wall test.c 
gcc -c -wall anothertest.c 
gcc -wall test.o anothertest.o -o test

make コマンドによって初めて作成された依存関係と実際のターゲットを確認できます。

ディレクトリの内容を再度確認すると、その中にさらに .o ファイルと実行可能ファイルがいくつかあります:

$ ls 
anothertest.c anothertest.o makefile test test.c test.h test.o

ここで、test.c ファイルにいくつかの変更を加えたと仮定して、次を使用します。 make でプロジェクトを再度コンパイルします。

$ make 
gcc -c -wall test.c 
gcc -wall test.o anothertest.o -o test

test.o のみが再コンパイルされ、他の test.o は再コンパイルされないことがわかります。

すべてのターゲット ファイルと実行可能ファイル テストをクリーンアップします。ターゲット クリーンを使用できます:

$ make clean
rm -rf *.o test

$ ls
anothertest.c makefile test.c test.h

すべての .o ファイルと実行可能ファイル テストが削除されたことがわかります。

2. -b オプションを渡すと、すべてのターゲットが常にリビルドされるようになります

もうお気づきかもしれませんが、make コマンドは、以前に作成されたターゲットをコンパイルしません。最後のビルド以降にコンパイルされました。変更されたファイルはありませんが、make のデフォルトの動作をオーバーライドしたい場合は、-b オプションを使用できます。

以下は例です:

$ make
make: nothing to be done for `all'.

$ make -b
gcc -c -wall test.c
gcc -c -wall anothertest.c
gcc -wall test.o anothertest.o -o test

make コマンドはファイルをコンパイルしませんが、make -b はすべてのターゲット ファイルと最終的な実行可能ファイルを強制的にコンパイルすることがわかります。

3. デバッグ情報を出力するには、-d オプションを使用します。

make の実行時に実際に何を行うかを知りたい場合は、-d オプションを使用します。

これは例です:

$ make -d | more
gnu make 3.81
copyright (c) 2006 free software foundation, inc.
this is free software; see the source for copying conditions.
there is no warranty; not even for merchantability or fitness for a
particular purpose.

this program built for x86_64-pc-linux-gnu
reading makefiles…
reading makefile `makefile'…
updating makefiles….
considering target file `makefile'.
looking for an implicit rule for `makefile'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.o'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.c'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.cc'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.c'.
trying pattern rule with stem `makefile'.
trying implicit prerequisite `makefile.cpp'.
trying pattern rule with stem `makefile'.
--more--

これは非常に長い出力です。出力をページごとに表示するために more コマンドを使用していることもわかりました。

4. -c オプションを使用してディレクトリを変更します

make コマンドに別のディレクトリ パスを指定すると、ディレクトリを検索する前に切り替えられます。メイクファイル。

これは、現在のディレクトリにいると仮定した場合のディレクトリです:

$ ls 
file file2 frnd frnd1.cpp log1.txt log3.txt log5.txt
file1 file name with spaces frnd1 frnd.cpp log2.txt log4.txt

ただし、実行する make コマンドの makefile ファイルは ../make-dir/ ディレクトリに保存されます。

$ make -c ../make-dir/ 
make: entering directory `/home/himanshu/practice/make-dir' 
make: nothing to be done for `all'. 
make: leaving directory `/home/himanshu/practice/make-dir

make コマンドが最初に特定のディレクトリに切り替えて、そこで実行し、その後元に戻っていることがわかります。

5. 他のファイルをメイクファイルとして扱うには、-f オプションを使用します。

メイクファイル ファイルの名前 (my_makefile または他の名前など) を変更したい場合は、 make でそれを makefile として扱うには、-f オプションを使用します。

make -f my_makefile

この方法では、make コマンドは makefile の代わりに my_makefile をスキャンすることを選択します。

以上がLinuxでmakeコマンドを使う方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はyisu.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。