2013-09-29 96 views
0

目前正試圖編譯使用 g++ -o crack crack2.c -lcrypt -lpthread -lmalloc這個程序,我得到:`找不到-lmalloc`,錯誤:LD返回1個退出狀態

[email protected]:~/Desktop$ g++ -o crack crack2.c -lcrypt -lpthread -lmalloc

/usr/bin/ld: cannot find -lmalloc

collect2: error: ld returned 1 exit status

所以..如果我刪除-lmalloc,我得到undefined reference to 'passwordLooper(void*)'

真的不確定如何解決此問題。它必須與malloc或-lmalloc相關,因爲在我使用malloc()之前,一切都按預期工作並編譯了程序。

/* 
crack.exe 
*/ 
/* g++ -o crack crack.c -lcrypt -lpthread -lmalloc */ 
//define _GNU_SOURCE 
#include <malloc.h> 

#include <crypt.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <errno.h> 
#include <pthread.h> 
#include <string.h> 
#include <math.h> 



void *passwordLooper(void *passwordData); 
//void *threadFunction(void *threads); 
typedef struct{ 
    int keysize; 
    char *target; 
    char *salt; 
}passwordData; 

int main(int argc, char *argv[]){   /* usage = crack threads keysize target */ 
    int i = 0; 
    /* arg[0] = crack, arg[1] = #of threads arg[2] = size of password, arg[3] = hashed password being cracked */ 

    if (argc != 4) { 
     fprintf(stderr, "Too few/many arguements give.\n"); 
     fprintf(stderr, "Proper usage: ./crack threads keysize target\n"); 
     exit(0); 
    } 

    int threads = *argv[1]-'0';   // threads is now equal to the second command line argument number 
    int keysize = *argv[2]-'0';   // keysize is now equal to the third command line argument number 
    char target[9]; 
    strcpy(target, argv[3]); 
    char salt[10]; 

    while (i < 2){   //Takes first two characters of the hashed password and assigns them to the salt variable 
     salt[i] = target[i]; 
     i++; 
    } 


    printf("threads = %d\n", threads);  /*used for testing */ 
    printf("keysize = %d\n", keysize); 
    printf("target = %s\n", target);   
    printf("salt = %s\n", salt);   


    if (threads < 1 || threads > 8){ 
     fprintf(stderr, "0 < threads <= 8\n"); 
     exit(0); 
    }            /*Checks to be sure that threads and keysize are*/ 
    if (keysize < 1 || keysize > 8){            /*of the correct size */ 
     fprintf(stderr, "0 < keysize <= 8\n"); 
     exit(0); 
    } 

    pthread_t t1,t2,t3,t4,t5,t6,t7,t8; 

    struct crypt_data data; 
    data.initialized = 0; 
    //~ passwordData.keysize = keysize; 
    //~ passwordData.target = target; 
    //~ passwordData.salt = salt; 

    passwordData *pwd = (passwordData *) malloc(sizeof(pwd)); 
    pwd->keysize = keysize; 
    pwd->target = target; 
    pwd->salt = salt; 


    //~ if (threads = 1){ 
     //~ pthread_create(&t1, NULL, *threadFunction, threads); 
    //~ } 


    char unSalted[30]; 
    int j = 0; 
    for (i = 2; target[i] != '\0'; i++){  /*generates variable from target that does not include salt*/ 
     unSalted[j] = target[i]; 
     j++; 
    } 
    printf("unSalted = %s\n", unSalted); //unSalted is the variable target without the first two characters (the salt) 

    char password[9] = {0}; 
    passwordLooper(pwd); 


} 




/*_____________________________________________________________________________________________________________*/ 
/*_____________________________________________________________________________________________________________*/ 
void *passwordLooper(passwordData *pwd){ 
    char password[9] = {0}; 
    int result; 

    int ks = pwd->keysize; 
    char *target = pwd->target; 
    char *s = pwd->salt; 

    for (;;){ 
     int level = 0; 
     while (level < ks && strcmp(crypt(password, s), target) != 0) { 
      if (password[level] == 0){ 
       password[level] = 'a'; 
       break; 
      } 
      if (password[level] >= 'a' && password[level] < 'z'){ 
       password[level]++; 
       break; 
      } 
      if (password[level] == 'z'){ 
       password[level] = 'a'; 
       level++; 
      } 
     } 

     char *cryptPW = crypt(password, s); 
     result = strcmp(cryptPW, target); 

     if (result == 0){    //if result is zero, cryptPW and target are the same 
      printf("result = %d\n", result); 
      printf ("Password found: %s\n", password); 
      printf("Hashed version of password is %s\n", cryptPW); 
      break; 
     } 
     if (level >= ks){   //if level ends up bigger than the keysize, break, no longer checking for passwords 
      printf("Password not found\n"); 
      break; 
     } 
    } 
    exit(0); 
} 
+2

在聲明的函數原型'passwordLooper'不匹配的定義。你已經定義了'passwordLooper(passwordData *)'而不是'passwordLooper(void *)'。 – Anthony

+0

你爲什麼用g ++編譯C程序?無論如何,'malloc'在標準庫中,所以它不需要'-l'來鏈接它,正確的頭文件包括'#include '。 – rici

+0

去年我獨家使用C++,並且總是使用g ++編譯。現在我在這個項目中使用了C語言,並且C中不需要C++。我之前使用make for C,直到我必須鏈接這些文件。 只是一個愚蠢的錯誤。 – m96

回答

2

你的代碼似乎是純粹的C.你應該用gcc編譯它,而不是g ++。

malloc函數在<stdlib.h>中聲明並在標準C庫中實現。您不需要#include <malloc.h>-lmalloc

對於您的passwordLooper函數,您有兩個不一致的聲明。您的「前進宣言」:

void *passwordLooper(void *passwordData); 

不匹配的定義:passwordLooper

void *passwordLooper(passwordData *pwd){ 
    /* ... */ 
} 

「前進宣言」需要遵循的類型passwordData的定義。

你有這樣一行:

//define _GNU_SOURCE 

您需要取消註釋:

#define _GNU_SOURCE 

使型struct crypt_data可見。

當我做的所有這些改變,我能夠編譯它(至少在我的系統),具有:

gcc c.c -o c -lcrypt 
+0

謝謝。我能夠得到它編譯。不幸的是,我在'pwd-> keysize = keysize;''pwd-> target = target;''pwd-> salt = salt;'遇到了seg故障。 – m96

+0

1.「對於任何給定的輸入文件,文件名後綴決定了什麼樣的編譯完成」('g ++'手冊頁); 2.如果您的segv使用上述版本,請使用'-Wall'(「如果自動變量未經初始化就使用,則發出警告」),因爲這就是您的'pwd'高於上面的值。 – usr2564301

+0

@Jongware我想通了。這是因爲我錯誤地將該行的後半部分'passwordData * pwd; // =(passwordData *)malloc(sizeof(pwd));'註釋掉了。我在試圖弄清楚malloc情況時評論了它。 – m96

相關問題