2014-02-15 48 views
0

我的頭文件和我的Makefile之間的某處我沒有正確執行依賴關係,也沒有編譯。這真的只與每個代碼的前幾行有關,但我發佈了所有代碼以供參考。make和#include的問題

我試圖將who克隆拆分爲3部分。 Here is the original for reference。這次演習是對UTMP它make,所以你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 */ 
        /*.  */ 
    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 

正如我剛纔所說,我沒有正確完成我的依賴,而我不知道我做錯了什麼。我如何正確執行我的依賴關係?

回答

2

看起來你是在你的makefile建設失蹤的依賴從命令的目標文件的列表show.o

此外,who3.o的命令看起來不正確。您僅編譯-c,但您傳遞的是目標文件作爲輸入(show.o)。您應該從規則中刪除show.oshow.c也不屬於who3.o的依存關係列表。

此外,show.o的命令看起來不正確。您不應該將頭文件(show.h)傳遞給編譯器;他們只需要在源文件中被引用爲#include

另外,您對實際調用的默認值不一致。您在規則(who3: ...)中說它是,但該命令實際上會構建一個名爲whogcc -o who ...)的任務。

+0

我能看到你的建議的例子嗎? – JFA

+0

@JFA:我建議修改你的makefile,哪一點不清楚? –

+0

通過你的第一個陳述,你說前兩行應該說:who3:who3.o utmplib.o show.o,第二行也應該反映出來。 – JFA