简体   繁体   English

如何在gcc中完成编译

[英]How compiling is done in gcc

I got two files sendfdsock.c, accessories.c . 我有两个文件sendfdsock.c, accessories.c In accessories.c I have defined various common functions (like logp - to print log) and included various common libraries (like sys/socket.h, string.h, stdio.h ....). 在accessories.c中,我定义了各种常用功能(如logp-打印日志),并包括了各种常用库(如sys / socket.h,string.h,stdio.h ....)。 Now instead of including accessories.c in sendfdsock.c or first making acceessories.h and then including it in sendfdsock.c , what I have planned is to first make object files of both sendfdsock.c and accessories.c and then linking them together in send. 现在,我计划首先制作sendfdsock.c and accessories.c目标文件,然后将它们链接在一起,而不是在sendfdsock.c中包括accessories.c sendfdsock.c或先创建acceessories.h ,然后再将其包括在sendfdsock.c 。在发送。 The commands to do this (I am not making any accessories.h file I am direct linking accessories.c ): 要执行此操作的命令(我没有制作任何accessories.h文件,而是直接链接accessories.c ):

gcc -c sendfdsock.c
gcc -c accessories.c
gcc -o send sendfdsock.o accessories.o

But after first command gcc -c sendfdsock.c I am getting some errors and warnings. 但是在第一个命令gcc -c sendfdsock.c我得到了一些错误和警告。 The reason of these errors are that sendfdsock.c needs some functions and variables that are defined in those .h files which I have included in accessories.h . 这些错误的原因是sendfdsock.c需要某些功能和变量,这些功能和变量是在我在accessories.h .h包含的.h文件中定义的。 This means gcc checks for definition of various functions and variables while making object files. 这意味着gcc在创建目标文件时会检查各种函数和变量的定义。 But the eerie is there is no error or warning for the function logp() that I have defined in accessories.c and used in the first line of main() of sendfdsock.c . 但是令人毛骨悚然的是,我在accessories.c定义并在main() of sendfdsock.cmain() of sendfdsock.c第一行中使用的函数logp()没有错误或警告。
That is there is no error/warning for user-defined functions but opposite for standard libraries. 用户定义的函数没有错误/警告,而标准库则没有。 WHY? 为什么?
How this compiling is done in gcc? 如何在gcc中进行编译? Is there any good source which can briefly tell me about this compiling and linking stuff. 是否有任何好的资料可以简要地告诉我有关此编译和链接的内容。
I am also sharing the code of the two files(if any one want to see): 我还共享了两个文件的代码(如果有人想看):

accessories.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 : 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);

}

Compile with -Wall -pedantic and check carefully: you probably get a warning, that tells you that you implicitly defined int logp That's what gcc does if it doesn't find a declaration. 使用-Wall -pedantic编译并仔细检查:您可能会收到一条警告,告知您隐式定义了int logp这就是gcc在找不到声明的情况下所做的事情。

Ps. 附言 If you're not sure about the difference of declaration and definition, start to read about that ;-) 如果不确定声明和定义的区别,请开始阅读;-)

extern for those variables is what you are looking for. 这些变量的extern是您要寻找的。

Oh .h files declare things but does not define it. 哦.h文件声明了东西,但没有定义它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM