我有两个文件sendfdsock.c, accessories.c
。在accessories.c中,我定义了各种常用函数(如logp - 打印日志),并包含各种常用库(如sys / socket.h,string.h,stdio.h ....)。现在不是在accessories.c
中包含sendfdsock.c
或首先制作acceessories.h
然后将其包含在sendfdsock.c
中,而是我计划的首先制作两个{{1}的目标文件然后在发送中将它们链接在一起。执行此操作的命令(我没有制作任何sendfdsock.c and accessories.c
文件,我直接链接accessories.h
):
accessories.c
但是在第一个命令gcc -c sendfdsock.c
gcc -c accessories.c
gcc -o send sendfdsock.o accessories.o
之后我收到了一些错误和警告。这些错误的原因是gcc -c sendfdsock.c
需要在sendfdsock.c
中包含的那些.h
文件中定义的一些函数和变量。这意味着gcc在制作目标文件时检查各种函数和变量的定义。 但令人毛骨悚然的是,我在accessories.h
中定义并在logp()
的第一行中使用的accessories.c
函数没有错误或警告。 />
也就是说,用户定义的函数没有错误/警告,但标准库没有错误/警告。的 WHY吗
这个编译是如何在gcc中完成的?有没有什么好的消息来源可以简单地告诉我这个编译和链接的东西
我也在分享这两个文件的代码(如果有人想看的话):
main() of sendfdsock.c
:
accessories.c
#include <malloc.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <error.h>
#include <signal.h>
#define PORT "4444" //port we are listening on
int sendall(int fd, char *buf, int *len)
{
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n;
while(total < *len) {
n = send(fd, buf+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}
*len = total; // return number actually sent here
return n==-1?-1:0; // return -1 on failure, 0 on success
}
int recvall(int fd, char *buf, int *len)
{
int total = 0; // how many bytes we've sent
int bytesleft = *len; // how many we have left to send
int n;
while(total < *len) {
n = recv(fd, buf+total, bytesleft, 0);
if (n == -1) { break; }
total += n;
bytesleft -= n;
}
*len = total; // return number actually sent here
return n==-1?-1:0; // return -1 on failure, 0 on success
}
void logp(int typ, char* msg) // typ --> type(category) of message [1-Normal Log, 2-Warning(any unexpected thing happened), 3-Error, 4-Debugging Log ]
{
int fd;
time_t now;
ssize_t wlength=0;
char * dat;
char * str;
int size = 45+strlen(msg);//14+24+5+sizeof msg+1
str= (char *) malloc(size);
time(&now);//system time in seconds
dat = ctime(&now); // converting seconds to date-time format
dat = strtok(dat,"\n");
//Appending type of log
switch(typ)
{
case 1:
strcpy(str,"__LOG__ | ");
strcat(str,dat);
break;
case 2:
strcpy(str,"__WARN__ | ");
strcat(str,dat);
break;
case 3:
strcpy(str,"__ERR__ | ");
strcat(str,dat);
break;
case 4:
strcpy(str,"__DEBUG__ | ");
strcat(str,dat);
break;
default:
strcpy(str,"__UNDEF__ | ");
strcat(str,dat);
break;
}
strcat(str," | ");
strcat(str,msg);//appending message
strcat(str,"\n");
fd = open("log", O_WRONLY | O_CREAT | O_APPEND, 0644); // should be opened somewhere else
if (fd == -1)
printf("Could not open log - %s\n",strerror(errno));
else
{//need to add lock to the file and printing error message
while ( wlength < strlen(str) )
{
wlength = write(fd, str,strlen(str));
if (wlength == -1)
{
printf("Error : writing log\n");
break;
}
}
}
}
void errorp(char *where, int boolean, int errn,char *what)
{
char errmsg[21+strlen(where)];
strcpy(errmsg,"Where - ");
strcat(errmsg,where);
strcat(errmsg," | Error - ");
if(boolean == 1)//we got error number
{
strcat(errmsg,strerror(errn));
//fprintf(stderr,"ERROR - In %s and error is %s\n",where ,strerror(errn));
logp(3,errmsg);
}
else if(boolean == 0)//we got a message
{
strcat(errmsg,what);
//fprintf(stderr,"ERROR - In %s and error is %s\n",where ,what);
logp(3,errmsg);
}
else//we got nothing
{
strcat(errmsg,"No Message");
//fprintf(stderr,"ERROR - In %s\n",where);
logp(3,errmsg);
}
}
:
sendfdsock.c
答案 0 :(得分:4)
使用-Wall -pedantic
进行编译并仔细检查:您可能会收到警告,告诉您隐式定义int logp
如果找不到声明,那就是gcc所做的。
聚苯乙烯。如果您不确定声明和定义的区别,请开始阅读;-)
答案 1 :(得分:-1)
extern
这些变量就是你要找的。 p>
哦.h文件声明了一些东西,但没有定义它。