开发者

C warning: implicit declaration of function

开发者 https://www.devze.com 2023-01-25 10:46 出处:网络
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 :

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 functi开发者_StackOverflow中文版on ‘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.h to the compiler in the makefile:

@$(CC) $(C_FLAGS) -c sgbd_server.c common.h

This would be better as just:

@$(CC) $(C_FLAGS) -c sgbd_server.c

Header files are usually incorporated with just an #include. You appear to be telling the compiler to try and compile common.h as 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:

#error Urk! in common.h

at the top of your common.h and 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.h file as well.


Based on your edit:

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?

These are, assuming ghc was a typo and you meant gch, pre-compiled headers generated by gcc, 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.h on your compiler command line when you did the server. By default, header files given directly to gcc will be turned into pre-compiled header files and used in preference after that point. To test this, I created a qq.h file and executed gcc qq.h and out popped qq.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:

@$(CC) $(C_FLAGS) -c sgbd_server.c common.h

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.h had no (immediate) effect. You would have to make ; touch common.h ; make to 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 :-)


observations: you have an = in a #define which I suspect you don't mean to have there

#define CLIENT_FIFO_PREFIX = "./sgbd_client_"

should perhaps be

#define CLIENT_FIFO_PREFIX "./sgbd_client_"

comments: I've occasionally witnessed strange behavior from compilers when a prototype contains formal parameter names (in addition to the mandatory types) and the definition of the function uses different formal parameter names. ie. in your pipes.c Does you pipes.c include common.h?

Also, conditional guards #ifndef headerfilename #define headerfilename #endif are a good practise, but that is not directly related to your problem.

.PMCD.


I suspect you might have another file called common.h that's getting included instead of the file you expect. I copy and pasted your files and it compiled with no warning.


The includes in the files you posted look like they should work. A few ideas what else could be wrong:

  • Does common.h or console.h try to include sgbd_server.h, so that you end up with circular includes?
  • Do you use include guards (or even other #ifdefs) and maybe mixed them up somewhere, so that one of the files is excluded unintentionally?
  • Do you have another common.h somewhere in you include path that might get included instead of the one that you intended?
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号