make和#include的问题

时间:2014-02-15 21:59:33

标签: c makefile

我的标题和Makefile之间的某处我没有正确地执行依赖项,并且它没有编译。这实际上只与每个代码的前几行有关,但我发布了所有代码供参考

我正在尝试将who克隆分成3部分。 Here is the original for reference。练习是make以utmp为主,所以你also need utmplib

所以我把它分成3个文件,第一个是show.h

#include    <stdio.h>
#include    <sys/types.h>
#include    <utmp.h>
#include    <fcntl.h>
#include    <time.h>
#include    <stdlib.h>
#define SHOWHOST

void show_info(struct utmp *);
void showtime(time_t);

然后我有show.c

/*
 *  *   show info()
 *   *          displays the contents of the utmp struct
 *    *         in human readable form
 *     *            * displays nothing if record has no user name
 *      */
void show_info( struct utmp *utbufp )
{
    if ( utbufp->ut_type != USER_PROCESS )
        return;

    printf("%-8.8s", utbufp->ut_name);      /* the logname  */
    printf(" ");                    /* a space  */
    printf("%-8.8s", utbufp->ut_line);      /* the tty  */
    printf(" ");                    /* a space  */
    showtime( utbufp->ut_time );            /* display time */
#ifdef SHOWHOST
    if ( utbufp->ut_host[0] != '\0' )
        printf(" (%s)", utbufp->ut_host);   /* the host */
#endif
    printf("\n");                   /* newline  */
}

void showtime( time_t timeval )
/*
 *  *   displays time in a format fit for human consumption
 *   *  uses ctime to build a string then picks parts out of it
 *    *      Note: %12.12s prints a string 12 chars wide and LIMITS
 *     *      it to 12chars.
 *      */
{
    char    *ctime();       /* convert long to ascii    */
    char    *cp;            /* to hold address of time  */

    cp = ctime( &timeval );     /* convert time to string   */
                    /* string looks like        */
                    /* Mon Feb  4 00:46:40 EST 1991 */
                    /* 0123456789012345.        */
    printf("%12.12s", cp+4 );   /* pick 12 chars from pos 4 */
}

最后,`who3.c'

/* who3.c - who with buffered reads
 *    - surpresses empty records
 *    - formats time nicely
 *    - buffers input (using utmplib)
 */
#include "show.h"

int main()
{
    struct utmp *utbufp,    /* holds pointer to next rec    */
            *utmp_next();   /* returns pointer to next  */

    if ( utmp_open( UTMP_FILE ) == -1 ){
        perror(UTMP_FILE);
        exit(1);
    }
    while ( ( utbufp = utmp_next() ) != ((struct utmp *) NULL) )
        show_info( utbufp );
    utmp_close( );
    return 0;
}

所以我创建了Makefile

who3:who3.o utmplib.o
    gcc -o who who3.o utmplib.o
who3.o:who3.c show.c
    gcc -c who3.c show.o
show.o:show.c
    gcc -c show.c show.h
utmplib.o:utmplib.c
    gcc -c utmplib.c
clean:
    rm -f *.o

不幸的是,当我make时出现错误:

gcc -o who who3.o utmplib.o
who3.o: In function `main':
who3.c:(.text+0x38): undefined reference to `show_info'
collect2: error: ld returned 1 exit status
make: *** [who3] Error 1

正如我之前所说,我没有正确地完成我的依赖,而且我不确定我做错了什么。如何正确执行依赖项?

1 个答案:

答案 0 :(得分:2)

您的makefile中用于构建show.o的命令的目标文件列表中的依赖项似乎缺少who3

此外,who3.o的命令看起来不对。您只编译-c,但是您正在传递一个目标文件作为输入(show.o)。您应该从规则中删除show.o,并且show.c也不属于who3.o的依赖项列表。

此外,show.o的命令看起来不对。您不应该将头文件(show.h)传递给编译器;它们只需要在源文件中被引用为#include

此外,您对实际调用的默认值不一致。您说规则(who3)中它是who3: ...但该命令实际上会构建一个名为whogcc -o who ...)的任务。