APUEのopen,read,write,close関数を適用し,基本的なmakefileを作成する.
条件:ファイルAの内容をファイルBに書き込む.中には、ファイルA,Bが存在するか否かを判断する必要がある.Bが存在する場合は以前の内容を削除し、Aに内容を書き込む.
dd.c
makefile
makefileの作成ルールを添付します.
target ... : prerequisites ...
command
...
...
targetはターゲットファイルであり、Object Fileであっても実行ファイルであってもよい.ラベル(Label)でも構いません.
prerequisitesとは、そのtargetを生成するために必要なファイルまたはターゲットです.
commandとは、makeが実行する必要があるコマンドです.(任意のShellコマンド)
これは、targetという1つ以上のターゲットファイルがprerequisites内のファイルに依存し、生成ルールがcommandに定義されるファイルの依存関係です.はっきり言って、prerequisitesにtargetファイルよりも1つ以上のファイルが新しい場合、commandで定義されたコマンドが実行されます.
私が初歩的に理解した決定関係は:
.h or .cファイル→(-c)→.o→(-o)→実行可能なターゲットファイル.gcc: -o output_filename、出力ファイルの名前をoutput_と決定filename、同時にこの名前はソースファイルと同名にできません.このオプションが指定されていない場合、gccは予め設定された実行可能ファイルa.outを与える.-cコンパイルコードのみobjectターゲットファイルを生成するがリンクは行わない.
dd.c
/*********************************************************************************
* Copyright: (C) 2014 songyong<handy_skyoutlook.com>
* All rights reserved.
*
* Filename: dd.c
* Description: This file
*
* Version: 1.0.0(2014 12 19 )
* Author: sky <[email protected]>
* ChangeLog: 1, Release initial version on "2014 12 19 20 38 08 "
*
********************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#define BUF_SIZE 128
/********************************************************************************
* Description:
* Input Args:
* Output Args:
* Return Value:
********************************************************************************/
int main (int argc, char **argv)
{
char *src_file;
char *dst_file;
int fd_src;
int fd_dst;
char buf[BUF_SIZE];
int len;
if(argc != 3)
{
printf("usage: %s [src_file] [dst_file]
", argv[0]);// .
return -1;
}
src_file = argv[1];
dst_file = argv[2];
fd_src = open(src_file,O_RDONLY);
if(fd_src < 0)
{
printf("Open file '%s' failure: %s
",src_file,strerror(errno));
return 0;
}
fd_dst = open(dst_file, O_RDWR|O_CREAT|O_TRUNC,0755);
if(fd_dst < 0)
{
printf("Open file '%s' failure: %s
",dst_file,strerror(errno));
return 0;
}
while ((len = read(fd_src,buf,sizeof(buf))) > 0)
{
write(fd_dst,buf,len);
}
close(fd_src);
close(fd_dst);
return 0;
} /* ----- End of main() ----- */
makefile
bins = dd
objs = dd.o
srcs = dd.c
$(bins) : $(objs)
gcc -o dd dd.o
$(objs) : $(srcs)
gcc -c dd.c
clean:
rm -f $(bins) (objs)
makefileの作成ルールを添付します.
target ... : prerequisites ...
command
...
...
targetはターゲットファイルであり、Object Fileであっても実行ファイルであってもよい.ラベル(Label)でも構いません.
prerequisitesとは、そのtargetを生成するために必要なファイルまたはターゲットです.
commandとは、makeが実行する必要があるコマンドです.(任意のShellコマンド)
これは、targetという1つ以上のターゲットファイルがprerequisites内のファイルに依存し、生成ルールがcommandに定義されるファイルの依存関係です.はっきり言って、prerequisitesにtargetファイルよりも1つ以上のファイルが新しい場合、commandで定義されたコマンドが実行されます.
私が初歩的に理解した決定関係は:
.h or .cファイル→(-c)→.o→(-o)→実行可能なターゲットファイル.gcc: -o output_filename、出力ファイルの名前をoutput_と決定filename、同時にこの名前はソースファイルと同名にできません.このオプションが指定されていない場合、gccは予め設定された実行可能ファイルa.outを与える.-cコンパイルコードのみobjectターゲットファイルを生成するがリンクは行わない.