gpt4 book ai didi

c - gcc 是如何编译的

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:11 26 4
gpt4 key购买 nike

我有两个文件 sendfdsock.c, accessories.c .在 accessories.c 中,我定义了各种常用函数(如 logp - 用于打印日志)并包含了各种常用库(如 sys/socket.h、string.h、stdio.h ....)。现在而不是包括 accessories.csendfdsock.c或先制作 acceessories.h然后将其包含在 sendfdsock.c 中, 我的计划是先制作 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 中定义的一些函数和变量我包含在 accessories.h 中的文件.这意味着 gcc 在创建目标文件时检查各种函数和变量的定义。 但是奇怪的是函数logp()没有错误或警告我在 accessories.c 中定义的并用在main() of sendfdsock.c的第一行.
也就是说,用户定义的函数没有错误/警告,但标准库则相反。 为什么?
这个编译是如何在 gcc 中完成的?有没有什么好的资源可以简要地告诉我关于这个编译和链接的东西。
我也分享了两个文件的代码(如果有人想看的话):

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 :

#include <stropts.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/un.h>




#define CONTROLLEN CMSG_LEN(sizeof(int))
static struct cmsghdr *cmptr = NULL; /* malloc'ed first time */

int send_err(int fd, int errcode, const char *msg);
int send_fd(int fd, int fd_to_send);

int main(int argc, char const *argv[])
{
logp(1,"started");
int fd_to_send;
if((fd_to_send = open("vi",O_RDONLY)) < 0)
printf("vi open failed");

struct sockaddr_un address;
int socket_fd, nbytes;
char buffer[256];

socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
if(socket_fd < 0)
{
printf("socket() failed\n");
return 1;
}

/* start with a clean address structure */
memset(&address, 0, sizeof(struct sockaddr_un));

address.sun_family = AF_UNIX;
snprintf(address.sun_path, sizeof(address.sun_path)-1, "./demo_socket");

if(connect(socket_fd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)) != 0)
{
printf("connect() failed\n");
return 1;
}

nbytes = snprintf(buffer, 256, "hello from a client");
// write(socket_fd, buffer, nbytes);

// nbytes = read(socket_fd, buffer, 256);
buffer[nbytes] = 0;

//printf("MESSAGE FROM SERVER: %s\n", buffer);

//sending the file descriptor
printf("From send_fd %d \n",send_fd(socket_fd,fd_to_send));
printf("Main end");
close(socket_fd);

exit(0);

}

int send_err(int fd, int errcode, const char *msg)
{
int n;

if ((n = strlen(msg)) > 0)
if (write(fd, msg, n) != n) /* send the error message */
return(-1);

if (errcode >= 0)
errcode = -1; /* must be negative */

if (send_fd(fd, errcode) < 0)
return(-1);

return(0);
}

int send_fd(int fd, int fd_to_send)
{

ssize_t temp;
struct iovec iov[1];
struct msghdr msg;
char buf[2]; /* send_fd()/recv_fd() 2-byte protocol */

iov[0].iov_base = buf;
iov[0].iov_len = 2;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;
if (fd_to_send < 0) {
msg.msg_control = NULL;
msg.msg_controllen = 0;
buf[1] = -fd_to_send; /* nonzero status means error */
if (buf[1] == 0)
buf[1] = 1; /* -256, etc. would screw up protocol */
} else {
if (cmptr == NULL && (cmptr = malloc(CONTROLLEN)) == NULL)
return(-1);
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
cmptr->cmsg_len = CONTROLLEN;
msg.msg_control = cmptr;
msg.msg_controllen = CONTROLLEN;
*(int *)CMSG_DATA(cmptr) = fd_to_send; /* the fd to pass */
buf[1] = 0; /* z ero status means OK */
}
buf[0] = 0; /* null byte flag to recv_fd() */
printf("before sendmsg \n");
temp = sendmsg(fd, &msg, 0);
if (temp != 2)
{
printf("inside sendmsg condition %d\n",temp);
return(-1);
}
printf("after sendmsg %d\n",temp);
return(0);

}

最佳答案

使用 -Wall -pedantic 编译并仔细检查:您可能会收到警告,告诉您您隐式定义了 int logp 如果 gcc 没有定义,这就是它所做的'找不到声明。

附言。如果您不确定声明和定义的区别,请开始阅读相关内容 ;-)

关于c - gcc 是如何编译的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11490553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com