2012-10-10 23 views
1

我學習C通過在來這裏之前翻譯一些我已經在Python所做的事情C.我已經試着在網上看的盡我所能,但它似乎真的很難找到我正在尋找的答案。未定義的參考作用 - 學習C

以下是米勒,拉賓測試了許多的素性我的(所以遠)翻譯。

#include <stdio.h> 
#include <math.h> 

_Bool prime(long long n); 

int main() { 
    int i = 6; 
    for (i; i < 9; i++) { 
     if (prime(i)) { 
      printf("%i\n", prime(i)); 
     } 
    } 
} 

_Bool prime(long long n) { 
    int s = 0; 
    int r = 0; 
    int a_index = 0; 
    // printf("the value of a_index when initialised is: %d\n", a_index); 
    // printf("the value of s when initialised is: %d\n", s); 
    int *a_list; 
    _Bool is_prime = 1; 
    _Bool composite_part_a = 1; 
    _Bool composite_part_b = 1; 
    long long d = n - 1; 
    while (d % 2 == 0) { 
     s++; 
     d = d/2; 
    } 

    if (4759123141 <= n && n < 2152302898747) { 
     // malloc 
     a_list[0] = 2; 
     a_list[1] = 3; 
     a_list[2] = 5; 
     a_list[3] = 7; 
     a_list[4] = 11; 
    } 
    else if (9080191 <= n && n < 4759123141) { 
     // malloc 
     a_list[0] = 2; 
     a_list[1] = 7; 
     a_list[2] = 61; 
    } 
    else if (1373653 <= n && n < 9080191) { 
     // malloc 
     a_list[0] = 31; 
     a_list[1] = 73; 
    }   
    else if (4 <= n && n < 1373653) { 
     a_list = (int *) malloc(sizeof(int) * 2); 
     a_list[0] = 2; 
     a_list[1] = 3; 
     printf("the value of a_list[0] upon its first assignment is: %d\n", a_list[0]); 
     // printf("the first element of a_list is: %d\n", a_list[0]); 
     // printf("the second element of a_list is: %d\n", a_list[1]); 
    } 
    else if (n == 3 | n == 2) { 
     return 1; 
    } 
    else if (n % 2 == 0 | n == 1) { 
     return 0; 
    } 

    printf("the value of a_list[0] over here is: %d\n", a_list[0]); 
    // printf("%d\n", a_list[1]); 
    for (a_index; a_index < sizeof(a_list)/sizeof(int); a_index++) { 
     printf("test"); 
     if ((long long)pow(a_index[a_list], d) % n != 1) { 
      composite_part_a = 1; 
     } 
     else { 
      composite_part_a = 0; 
     } 


     // printf("the value of r is: %d\n", r); 
     // printf("the value of s is: %d\n", s); 
     for (r; r < s; r++) { 
      printf("%lld\n", (int)pow(a_list[a_index], exp2(r) * d) % n); 
      if ((long long)pow(a_index[a_list], exp2(r) * d) % n != -1) { 
       composite_part_b = 1; 
      } 
      else { 
       composite_part_b = 0; 
       break; 
      } 
      } 

     if (composite_part_a && composite_part_b) { 
      return 0; 
     } 
     } 

    return is_prime; 
    } 

與學習C++的問題是沒有太多好的文學作品純粹初學者,什麼我聽到ķ& R的外側,但是這是在郵件,我不能讓我的手放在它現在。該程序返回這些錯誤:

3.c: In function ‘prime’: 
3.c:52:26: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default] 
/tmp/ccGQnk9T.o: In function `prime': 
3.c:(.text+0x272): undefined reference to `pow' 
3.c:(.text+0x2b9): undefined reference to `exp2' 
3.c:(.text+0x2db): undefined reference to `pow' 
3.c:(.text+0x30b): undefined reference to `exp2' 
3.c:(.text+0x32d): undefined reference to `pow' 
collect2: ld returned 1 exit status 

首先,我不包括介紹戰俘和其他?我知道提出兩個問題是不合適的,我的主要問題是關於pow和exp2,但是如果您確實有關於malloc的建議,請隨時添加它。

+0

它不是你的程序可執行文件,它是'gcc'編譯器(和鏈接器),它返回「未定義的引用」錯誤。 –

回答

5

你需要鏈接與數學庫,以及它不包括默認情況下。

類似於下面的命令:

$ gcc 3.c -lm 

通知的-lm說法......它告訴鏈接器添加庫(該-l部分)和庫的名稱(m部分)。

+0

圖例。我想我必須對大多數圖書館這樣做? – nebffa

+0

但是你真的想從編譯器中得到所有的警告和調試信息,所以使用'gcc -Wall -g 3.c -lm -o prog3'編譯並學習使用'gdb prog3'來調試你的程序。 –

+0

@nebffa如果它是一個默認沒有鏈接的庫(除純粹的標準C庫外沒有鏈接),那麼是的。儘管數學庫是標準的一部分,但它默認情況下沒有鏈接,可能主要是因爲現在的幾天有歷史原因。 –

3

數學函數是libm的一部分。當您使用-lm進行編譯時,將它們鏈接起來。

+0

你會如何做到這一點? – nebffa

+0

如果您使用的是gcc,那麼:'gcc file.c -o file -lm' –

0

您需要包括<stdlib.h>的malloc和free。

對於數學的東西的工作,你必須掛靠, gcc 3.c -lm 哪裏-l是圖書館標誌,m是告訴它使用數學庫

你也需要優質的定義移動到上方主要的東西需要按順序申報。

既然你是剛開始在這裏是編譯器

-g此使用的valgrind或GDB時,會提供更好的調試一些其他有用的標誌。

-o可以定義編譯後的文件名如:gcc 3.c -o 3將創建./3代替./a.out

1

3.C:52:26:警告:內建函數「的malloc」不兼容的隱式聲明[默認啓用]

是由丟失引起的包括,malloc()函數在stdlib.h中被定義,從而需要包括。

3.C :(文字+ 0x272):未定義的引用'戰俘」(其餘)

由缺少的環節到libm造成的。 math.h中的大多數(如果不是全部)方法都不是標準的libc,而是始終鏈接到libm中。

如何編譯器之間的鏈路是不同的,但是對於GCC(和其他許多UNIX編譯):

3.C的gcc -o 3 -lm

其中 「-lm」 告訴GCC鏈接libm中。

+0

嗯......我現在才明白提出一個問題的原因。您只能接受其中一個問題的答案。我會選擇這是正確的,如果它不是我已經說是主要部分。但是這非常清楚,非常感謝你發佈這個。 – nebffa