LoginSignup
0
1

More than 3 years have passed since last update.

Makefileの練習

Last updated at Posted at 2020-05-21

目的

今更感があるのですが、Makefileの練習です

make

環境

$ sw_vers 
ProductName:    Mac OS X
ProductVersion: 10.13.6
BuildVersion:   17G4015

サンプルコードを用意

hello.c
#include <stdio.h>

int main() {
  puts("hello, world!");
  return 0;
}

一旦、ビルドして実行

$ gcc -o hello hello.c
$ ./hello 
hello, world!

デバッグ用に必要に応じて実行パスを追加

(例)カレントディレクトリの実行パスを追加する
$ CURRENT_DIR=`pwd`
$ export PATH=${PATH}:${CURRENT_DIR}
$ hello 
hello, world!

Makefile作成

Makefile
CC = gcc
CFLAGS = -g -Wall -Wextra
CODE = hello.c
PROGRAM = hello

hello:
        $(CC) $(CFLAGS) $(CODE) -o $(PROGRAM)

clean:
        $(RM) hello

makeして実行する

$ make
gcc -g -Wall -Wextra hello.c -o hello
$ ./hello 
hello, world!

.mkファイルを添えてMakefile作成

Makefile
include test.mk

CC = gcc
CFLAGS := -g $(CXXFLAGS)
CODE = hello.c
PROGRAM = hello

hello:
        $(CC) $(CFLAGS) $(CODE) -o $(PROGRAM)

clean:
        $(RM) hello
test.mk
CXXFLAGS = -Wall -Wextra

makeして実行する

$ make 
gcc -g -Wall -Wextra hello.c -o hello
$ ./hello
hello, world!

cmakeでMakefileを作成する

CMakeLists.txt を作成

CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(test_project)
add_executable(executable_file hello.c)

cmakeでMakefileを作成

$ cmake .
-- The C compiler identification is AppleClang 10.0.0.10001044
-- The CXX compiler identification is AppleClang 10.0.0.10001044
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc - works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: mkfile

makeして実行する

$ make
Scanning dependencies of target executable_file
[ 50%] Building C object CMakeFiles/executable_file.dir/hello.c.o
[100%] Linking C executable executable_file
[100%] Built target executable_file
$ ./hello
hello, world!

hello, world! と出力されたのでOK

make実行時に*** missing separator. Stop.が出る

Makefileのスペースをタブに変更すれば解決する。
例えば、以下のclean: の下の部分がスペースの場合にエラーが出るのでタブに変換する。

clean:
    $(RM) hello

参考

Makefileでmake時に 「*** missing separator. Stop.」 と出たときの対処法
2016年だけどMakefileを使ってみる
Makefileの解説
トリビアなmakefile入門
configure, make, make install とは何か
Makefileでmake時に 「*** missing separator. Stop.」 と出たときの対処法
3. Makefileの記述
メークファイルをインクルードする
make の種類あれこれ

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1