2011-02-18 215 views
3

我在寫我的第一個程序用C一類「未定義的符號 首次在文件中引用 」;我設法平衡了大部分的語法錯誤,但是當gcc嘗試將目標文件鏈接在一起時,我遇到了一個奇怪的錯誤。它打印酷似如下: 鏈接錯誤

gcc -o proj04.support.o proj04.driver.o 
Undefined      first referenced 
symbol        in file 
convert        proj04.driver.o 

我環顧四周,幾個答案,但沒有真正意義的我。我會發布我用來製作下面的程序的文件,如果你有答案,我會非常感謝幫助。這似乎是一個非常基本的錯誤,所以它可能是我沒有做的傻事。

的Makefile(第一張貼這一點,因爲我懷疑問題就在這裏)

# Comments 
# Comments 

proj04: proj04.support.o proj04.driver.o 
     gcc -o proj04.support.o proj04.driver.o 

proj04.support.o: proj04.support.c 
     gcc -Wall -c proj04.support.c 

proj04.driver.o: proj04.driver.c 
     gcc -Wall -c proj04.driver.c 

頭文件(由教授提供的,不可改變的,一條線長):

int convert(int, unsigned, char[], int) 

實現文件

#include <stdio.h> 
#include "/user/cse320/Projects/project04.support.h" 
#include <string.h> 

void formatdisplay(char[], int); 

int convert(int I, unsigned base, char result[], int display) 
{ 
    int quotient, dividend, remainder; 
    const int divisor = base; 
    int count = 0; 
    char ending[] = " base "; 

    dividend = I; 
    remainder = 0; 
    quotient = 1; 

    while (quotient != 0) 
    { 
    if (count <= strlen(result)) 
    { 
     quotient = (dividend/divisor); 
     remainder = (dividend % divisor); 
     //convert to ascii char 
     result[count] = remainder; 
     count++; 
    } 
    } 

    formatdisplay (result, display); 

    strrev(result); 

    if (I >= 0) { result[0] = '+'; } 
    if (I < 0) { result[0] = '-'; } 

    printf("%s" , strcat (result, ending)); 

}  

void formatdisplay (char str[], int disp) 
{  
    if (disp < 0) 
    { 
    unsigned i = 0; 
    for (i; i < strlen(str)-1; i++) 
    { 
     if (str[i] = '\0') { str[i] = '0'; } 
    } 
    } 
    if (disp >= 0) 
    { 
    unsigned i = 0; 
    for (i; i < strlen(str)-1; i++) 
    { 
     if (str[i] = '\0') { str[i] = ' '; } 
    } 
    } 
} 

驅動程序文件(不是真的還沒有實現)

#include <stdio.h> 
#include "/user/cse320/Projects/project04.support.h" 

int main() { 
    char Result1[32]; 
    int T = convert(10, 2, Result1, 1); 
} 
+0

你確定引用了`project04.support.h`文件嗎?它在函數聲明後似乎缺少分號。 – CiaPan 2014-10-21 06:34:56

回答

3

是的,這個問題很可能是在Makefile:

proj04: proj04.support.o proj04.driver.o 
     gcc -o proj04.support.o proj04.driver.o 

-o選項gcc帶有一個參數,輸出文件名。所以這是要求gcc鏈接文件proj04.driver.o,產生一個輸出文件proj04.support.o

gcc -o proj04 proj04.support.o proj04.driver.o應該會更好。

+0

修正了錯誤,但現在又給了我另一個函數strrev的相同錯誤。而不是「未定義的符號轉換」,我有「未定義的符號strrev」。 Strrev在中聲明,我已包括在內。不過,我可能會在文件proj04.support.c中錯誤地聲明strrev。 – 2011-02-18 00:40:12

3

我有幾乎相同的問題。

Undefined first referenced symbol in file isThreeOfAKind /var/tmp//ccIQWbaj.o 

我的問題是我錯過了我的一個函數中的一個字母,所以聲明和函數沒有對齊。例如:

void isThreeOfAKind (void); 
void isThreeOfAkind {} 

錯過了大寫字母K,寫下了小寫字母k。

當我把k改成大寫K後,它編譯得很好。

我不知道它是否有幫助,但它可能是那樣容易。

3

我不是專家,但作爲安德烈亞斯Joensson(誰,由函數名判斷是寫我是完全一樣的節目)說上面這個問題似乎時有申報和使用功能之間的一些不匹配的發生。

你的函數convert被聲明爲返回一個int,但是我找不到返回值。這可能是問題所在。