2011-05-13 89 views
5

我正在使用下面的代碼,我發現網絡中的某個地方,並且當我嘗試構建它時出現錯誤。彙編是好的。未定義的引用'crypt'

以下是錯誤:

/tmp/ccCnp11F.o: In function `main': 

crypt.c:(.text+0xf1): undefined reference to `crypt' 

collect2: ld returned 1 exit status 

這裏是代碼:

#include <stdio.h> 
#include <time.h> 
#include <unistd.h> 
#include <crypt.h> 

int main() 
{ 
    unsigned long seed[2]; 
    char salt[] = "$1$........"; 
    const char *const seedchars = 
    "./ABCDEFGHIJKLMNOPQRST" 
    "UVWXYZabcdefghijklmnopqrstuvwxyz"; 
    char *password; 
    int i; 

    /* Generate a (not very) random seed. 
     You should do it better than this... */ 
    seed[0] = time(NULL); 
    seed[1] = getpid()^(seed[0] >> 14 & 0x30000); 

    /* Turn it into printable characters from `seedchars'. */ 
    for (i = 0; i < 8; i++) 
    salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f]; 

    /* Read in the user's password and encrypt it. */ 
    password = crypt(getpass("Password:"), salt); 

    /* Print the results. */ 
    puts(password); 
    return 0; 
} 
+0

可能重複://計算器。 com/questions/2565427/crypt-function-and-link-error-undefined-reference-to-crypt) – jww 2016-11-23 01:50:55

回答

12

crypt.c:(.text+0xf1): undefined reference to 'crypt'是一個連接錯誤。

嘗試鏈接-lcryptgcc crypt.c -lcrypt

+0

沒有編譯錯誤,只鏈接錯誤。預處理器指令是不相關的。 – 2016-11-23 07:48:46

0

機會是你忘了鏈接庫

gcc ..... -lcrypt 
1

您對編譯時添加-lcrypt ......試想一下,源文件被稱爲crypttest.c,你會做:

cc -lcrypt -o crypttest crypttest.c 
+1

很多編譯器上的鏈接標誌需要結束 – sehe 2011-05-13 08:57:58

+0

不知道,謝謝 – roirodriguez 2011-05-13 09:01:35

0

這可能是由於兩方面的原因:

  1. 鏈接到crypt庫:使用-l<nameOfCryptLib>作爲標記gcc
    示例:gcc ... -lcrypt其中crypt.h已被編譯爲庫。
  2. 檔案crypt.h不在include path中。只有當文件位於include path時,纔可以在頭文件周圍使用<>標籤。要確保crypt.h存在於包含路徑中,請使用-I標誌,如下所示:gcc ... -I<path to directory containing crypt.h> ...
    示例:gcc -I./crypt其中crypt.h存在於當前目錄的crypt/ sub-directory中。

如果你不想使用-I標誌,改變[隱窩功能和鏈接錯誤「未定義的引用‘地穴’」(HTTP的#include<crypt.h>#include "crypt.h"