我想用gcc编译以下代码:
/**********************************************************************\
* Copyright (C) Michael Kerrisk, 2010. *
* *
* This program is free software. You may use, modify, and redistribute *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 or (at *
* your option) any later version. This program is distributed without *
* any warranty. See the file COPYING for details. *
\**********************************************************************/
/* copy.c
Copy the file named argv[1] to a new file named in argv[2].
*/
#include <sys/stat.h>
#include <fcntl.h>
#include "tlpi_hdr.h"
#ifndef BUF_SIZE /* Allow "cc -D" to override definition */
#define BUF_SIZE 1024
#endif
int
main(int argc, char *argv[])
{
int inputFd, outputFd, openFlags;
mode_t filePerms;
ssize_t numRead;
char buf[BUF_SIZE];
if (argc != 3 || strcmp(argv[1], "--help") == 0)
usageErr("%s old-file new-file\n", argv[0]);
/* Open input and output files */
inputFd = open(argv[1], O_RDONLY);
if (inputFd == -1)
errExit("opening file %s", argv[1]);
openFlags = O_CREAT | O_WRONLY | O_TRUNC;
filePerms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
S_IROTH | S_IWOTH; /* rw-rw-rw- */
outputFd = open(argv[2], openFlags, filePerms);
if (outputFd == -1)
errExit("opening file %s", argv[2]);
/* Transfer data until we encounter end of input or an error */
while ((numRead = read(inputFd, buf, BUF_SIZE)) > 0)
if (write(outputFd, buf, numRead) != numRead)
fatal("couldn't write whole buffer");
if (numRead == -1)
errExit("read");
if (close(inputFd) == -1)
errExit("close input");
if (close(outputFd) == -1)
errExit("close output");
exit(EXIT_SUCCESS);
}
gcc copy.c -o copy
错误:
copy.c:15:10: fatal error: 'tlpi_hdr.h' file not found
#include "tlpi_hdr.h"
^~~~~~~~~~~~
1 error generated.
代码来自https://github.com/bradfa/tlpi-dist/blob/78f35620dfe47ec7abc9dbcd9b56d51de4c19f7e/filebuff/copy.c
为了编译此.c文件,编译器必须知道头文件在哪里。它将在各种不同的默认位置中查找。为了帮助编译器找到它,请在包含文件的文件夹中使用-I
选项。
它与您从中提取单个文件的项目相同。在项目目录结构中,copy.c位于tlpi-dist / filebuff