我学习c,当我用 make 编译我的程序时,它会给我一个关于隐式声明的警告,但仍然像它应该的那样运行
这些是我尝试编译时收到的警告,
gcc -o bin/final src/file.c src/main.c src/parse.c
src/main.c: In function \u2018main\u2019:
src/main.c:16:14: warning: implicit declaration of function \u2018open_file_rw\u2019 [-Wimplicit-function-declaration]
16 | fd = open_file_rw(argv[1]);
| ^~~~~~~~~~~~
src/main.c:20:12: warning: implicit declaration of function \u2018parse_file_header\u2019 [-Wimplicit-function-declaration]
20 | if(parse_file_header(fd, &numEmployees))
| ^~~~~~~~~~~~~~~~~
我的 Makefile 有一个主文件,然后我有并包含所有标头和 src 以及所有 c 文件
这是我的主要内容,
#include <stdio.h>
#include "../include/file.h"
#include "../include/parse.h"
int main(int argc, char** argv)
{
int fd, numEmployees = 0;
if(argc != 2)
{
printf("Usage: %s <filename>\n", argv[0]);
return 0;
}
fd = open_file_rw(argv[1]);
if(fd == -1)
return -1;
if(parse_file_header(fd, &numEmployees))
return -1;
printf("Number of employees stored: %d\n", numEmployees);
return 0;
}
这是文件.h
#ifdef FILE_H
#define FILE_H
int open_file_rw(char* filename);
#endif /* FILE_H */
这是文件.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "../include/file.h"
int open_file_rw(char* filename)
{
int fd = open(filename, O_RDWR);
if(fd == -1)
{
perror("open");
return fd;
}
return fd;
}
这里是parse.h,
#ifdef PARSE_H
#define PARSE_H
struct dbheader
{
unsigned short version;
unsigned short count;
};
int parse_file_header(int fd, int* numEmployeesOut);
#endif /* PARSE_H */
这是 parse.c
#include <stdio.h>
#include <unistd.h>
#include "../include/parse.h"
struct dbheader_t
{
unsigned short version;
unsigned short count;
};
int parse_file_header(int fd, int* numEmployeesOut)
{
if(fd == -1)
{
printf("Bad file descriptior provided\b");
return -1;
}
struct dbheader_t header = {0};
if(read(fd, &header, sizeof(header)) != sizeof(header))
{
printf("Failed to read file header\n");
return -1;
}
*numEmployeesOut = header.count;
return 0;
}
最后这是我的 make 文件,
TARGET := bin/final
SRC := $(wildcard src/*.c)
OBJ := $(patsubst src/%*.c, obj/*.c, $(SRC))
INCLUDE := ../include
default: $(TARGET)
clean:
rm -f obj/*.o
rm -f bin/*
$(TARGET): $(OBJ)
gcc -o $@ $?
obj/%.o : src/%.c
gcc -c $< -o $@ -I$(INLCUDE)
我知道这是一篇很长的文章,我已经看了几个小时了,无法弄清楚出了什么问题,任何帮助都是值得赞赏的。谢谢。
我尝试使用 make 构建我的程序,但不断出现错误。
您的头文件已损坏。您使用
#ifdef FILE_H
,它应该是 #ifndef FILE_H
。你永远不会包括他们。