Yes, this question has been asked many times, and I’ve been looking and reading forums, and SO posts, but the answers are all unrelated (or so they seem) to this one. So, I have this main file :
— sgbd_server.c —
#include "sgbd_server.h"
/**
* Open server pipe and return handle. -1 = error
*/
int open_server_pipe() {
return pipe_open(FIFO_NAME, O_RDONLY, S_CON_COLOR);
}
/**
* Close server pipe
*/
void close_server_pipe(int fd) {
pipe_close(fd, FIFO_NAME, S_CON_COLOR);
}
int main(int argc, char *argv[]) {
int pipe_fd;
pipe_fd = open_server_pipe();
if (pipe_fd == -1) {
perror("Cannot open pipe");
}
close_server_pipe(pipe_fd);
exit(EXIT_SUCCESS);
}
Then the header files :
— sgbd_server.h —
#include "common.h"
#define FIFO_NAME "./sgbd_server_pipe"
#define BUFFER_SIZE PIPE_BUF
#define S_CON_COLOR 1 /* C_COLOR_RED */
— common.h —
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "console.h"
#define CLIENT_FIFO_PREFIX = "./sgbd_client_"
int pipe_open(char *f, int mode, int color);
void pipe_close(int pipe_fd, char *f, int color);
The two functions pipe_open and pipe_close are defined in pipe.c and are basically returning 0 and void. This last file is compiled separately in the Make file.
I’m not a guru at making Make files, but for the sake of this question, here it is :
SERVER = sgbd_server
CLIENT = sgbd_client
CC = gcc
C_FLAGS = -Wall -I.
LINKER = gcc
L_FLAGS = -Wall -l pthread -Wall -I.
RM = rm -f
client: sgbd_client.o pipe.o console.o
@echo -n "Building client... "
@$(LINKER) $(L_FLAGS) -o $(CLIENT) sgbd_client.o pipe.o console.o
@echo "Complete!\n"
server: sgbd_server.o pipe.o console.o
@echo -n "Building server... "
@$(LINKER) $(L_FLAGS) -o $(SERVER) sgbd_server.o pipe.o console.o
@echo "Complete!\n"
sgbd_client.o: sgbd_client.c
@echo -n "Refreshing client sources... "
@$(CC) $(C_FLAGS) -c sgbd_client.c
@echo "Done!"
sgbd_server.o: sgbd_server.c common.h
@echo -n "Refreshing server sources..."
@$(CC) $(C_FLAGS) -c sgbd_server.c common.h
@echo "Done!"
pipe.o: pipe.c
@echo -n "Refreshing pipe sources..."
@$(CC) $(C_FLAGS) -c pipe.c
@echo "Done!"
console.o: console.c
@echo -n "Refreshing console sources..."
@$(CC) $(C_FLAGS) -c console.c
@echo "Done!"
clean:
@echo -n "Cleaning up executables and object files... "
@$(RM) $(SERVER) $(CLIENT) *.o
@echo "Ok\n"
** NOTE ** : the file console.c and implements some functions to control I/O on the console, nothing fancy. As you see, it is also compiled separately.
Now, when I type make client, all is well and birds are signing, etc. etc. But when I type make server, it spits out
sgbd_server.c: In function ‘open_server_pipe’:
sgbd_server.c:7: warning: implicit declaration of function ‘pipe_open’
sgbd_server.c: In function ‘close_server_pipe’:
sgbd_server.c:14: warning: implicit declaration of function ‘pipe_close’
I’m running GCC on a Linux amd64 if it makes any difference (I doubt it).
Now, why would it warn me about that? The two functions are declared in common.h, which is included in sgbd_server.h… What am I missing here?
Thank you for your time!
** UPDATE **
Thank you everyone for your suggestion. I did try to find if there would be a file common.h somewhere else in my include path that would be included somehow… While I failed to find any that would have slipped in the compilation process instead of the local common.h (sig) I found some .ghc files sitting in my source folder. Since they are not cleaned by make clean, I deleted manually those files. Guess what? No warning. What are those files and why are they created?
For a start, I don’t think it’s a good idea to be giving
common.hto the compiler in the makefile:This would be better as just:
Header files are usually incorporated with just an
#include. You appear to be telling the compiler to try and compilecommon.has a standalone C file. That’s one difference between the client and server compile commands and you should fix it.The only other thing I can suggest is that you may not be getting the header files you think you’re getting. Start by putting the line:
at the top of your
common.hand ensure the build fails there.If not, then that file is coming from somewhere else. You may also want to do the same thing with your
sgbd_server.hfile as well.Based on your edit:
These are, assuming
ghcwas a typo and you meantgch, pre-compiled headers generated bygcc, at least in part to speed up the compilation process. Rather than having to process a header file many times during a build (once per source file that includes it), pre-compiling it once and using the pre-compiled version is a lot more efficient.I think this was most likely cause by the fact that you included
common.hon your compiler command line when you did the server. By default, header files given directly togccwill be turned into pre-compiled header files and used in preference after that point. To test this, I created aqq.hfile and executedgcc qq.hand out poppedqq.h.gch.It’s quite likely, given that deleting them solved your problem, that these files were somehow causing your issues (be that the presence of pre-compiled headers older than the real headers or something else entirely). There’s a good chance that your compile line:
would first compile the server program, including the old precompiled header, then make a new precompiled header out of the newer header file.
That’s probably why a change to
common.hhad no (immediate) effect. You would have tomake ; touch common.h ; maketo ensure the newer pre-compiled header file was used in the server program.Whether you want to track it back to the root cause and get a proper explanation is a matter of taste – there’s a school of thought that you should sometimes just record how you fixed it and not worry too much, lest you become entangled in the very nature of reality itself.
Not me of course, I’m the personality type that will attempt to track my problems back to the individual sub-atomic particle that caused it, but sometimes pragmatism requires me to let it go 🙂