I learning c, when I compile my program with make it give me a warning for implicit declaration but still runs like it should
These are the warnings I get when I try to compile,
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))
| ^~~~~~~~~~~~~~~~~
I have a main file with my Makefile, then I have include with all my headers and src with all my c files
Here is my main.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;
}
Here is file.h
#ifdef FILE_H
#define FILE_H
int open_file_rw(char* filename);
#endif /* FILE_H */
Here is file.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;
}
Here is 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 */
and here is 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;
}
and finally here is my Makefile,
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)
I know this is a long post, Ive been looking at this for hours and cant figure out whats going wrong, any help is appreciated. Thanks.
I tried to build my program using make, but keep getting errors.
Your header files are broken. You use #ifdef FILE_H and it should be #ifndef FILE_H. Therefore, the header files are empty, when the compiler sees them.
If you suspect that there is something wrong with one of your header files, there are two standard strategies that can help you find the issue.
First option: Add #error directives to see whether they are processed, and how. Something like this:
#error file.h gets included!
#ifdef FILE_H /* Bug! Wrong! */
# define FILE_H 1
#error file.h was processed!
#endif
That will print an error to the console and show you that the file was correctly included.
For more complicated issues, it helps to invoke the preprocessor only, like this:
$ gcc -c -E program.c
That will print the output of the preprocessor, including filename and line number information to the console. And you would have seen, that the header file is completely empty for the compiler.
To answer your other, implicit question, why the code works although the header file is not processed: It is not necessary to declare functions in C. If you implicitely declare function, the compiler will assume a signature of int FUNCTIONNAME(), i.e. a function that takes no arguments and returns an integer.
It is just not wise to use that "feature" because the compiler is not able to check whether the usage of the function is correct and that can lead to problems. And besides, if the function is really missing, the warning from the compiler is usually easier to understand than the warning from the linker. Try that out by something like prindf("typo");.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With