9

ネットのどこかで見つけた以下のコードを使用していますが、ビルドしようとするとエラーが発生します。コンパイルは問題ありません。

エラーは次のとおりです。

/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 =
     "./0123456789ABCDEFGHIJKLMNOPQRST"
     "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;
 }
4

4 に答える 4

22

crypt.c:(.text+0xf1): undefined reference to 'crypt'リンカーエラーです。

-lcrypt:とリンクしてみてくださいgcc crypt.c -lcrypt

于 2011-05-13T08:51:46.883 に答える
2

コンパイル時に -lcrypt を追加する必要があります... ソースファイルが crypttest.c と呼ばれていると想像してください。

cc -lcrypt -o crypttest crypttest.c
于 2011-05-13T08:52:21.043 に答える
0

ライブラリへのリンクを忘れている可能性があります

  gcc ..... -lcrypt
于 2011-05-13T08:50:56.637 に答える
-1

これには、次の 2 つの理由が考えられます。

  1. crypt ライブラリとのリンク:-l<nameOfCryptLib>へのフラグとして使用しgccます。
    例: gcc ... -lcryptwherecrypt.hはライブラリにコンパイルされています。
  2. ファイルcrypt.hが にありませんinclude path<およびタグを使用できる>のは、ファイルがinclude path. crypt.hがインクルード パスに存在することを確認するに は、 次の-Iようにフラグを使用します。 gcc ... -I<path to directory containing crypt.h> ...
    gcc -I./cryptcrypt.hcrypt/ sub-directory

-Iフラグを使用したくない場合は#include<crypt.h>#include "crypt.h"

于 2011-05-13T09:21:34.127 に答える