是的,这个问题已经被问过很多次了,我一直在寻找和阅读论坛和 SO 帖子,但答案都与这个问题无关(或者看起来如此)。所以,我有这个主文件:
-- 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);
}
然后头文件:
-- 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);
两个函数pipe_open
andpipe_close
定义在pipe.c
and 中,基本上是返回0
and void
。最后一个文件在 Make 文件中单独编译。
我不是制作 Make 文件的专家,但为了这个问题,这里是:
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"
**注意** :该文件console.c
并实现了一些功能来控制控制台上的 I/O,没什么花哨的。如您所见,它也是单独编译的。
现在,当我输入时make client
,一切都很好,鸟儿在签名等等。但是当我输入时make server
,它会吐出来
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’
如果有任何区别,我正在 Linux amd64 上运行 GCC(我对此表示怀疑)。
现在,它为什么要警告我呢?这两个函数在 中声明common.h
,包含在sgbd_server.h
... 我在这里缺少什么?
感谢您的时间!
**更新**
谢谢大家的建议。我确实尝试过在我的包含路径中的其他地方是否会有一个文件common.h
以某种方式包含在内......虽然我没有找到任何会在编译过程中滑倒而不是本地common.h
(sig)的文件,但我找到了一些.ghc
文件坐在我的源文件夹中。由于它们没有被清理make clean
,我手动删除了这些文件。你猜怎么了?没有警告。这些文件是什么,为什么要创建它们?