UNIX C中的gcc -lcrypt标志错误 - 未定义对crypt()的引用

时间:2012-10-14 16:32:46

标签: c unix

我使用crypt()函数和名为-lcrypt的编译标志,问题在于编译器对crypt()的未定义引用。谁能告诉我我做错了什么?

生成文件

    CC = gcc
    CFLAGS=-Wall -lm -lcrypt
    OBJS = get_passwords_hashed.o
    PROG = get_passwords_hashed.exe

    #adicionar or mudar o OBJS se tiver outras files para o programa


    #GENERIC

    all:    ${PROG}

    clean:
            rm ${OBJS} *~ ${PROG}

    ${PROG}: ${OBJS}
            ${CC} ${OBJS} -o $@

    .c.o:
            ${CC} $< -c -o $@
    # $@ - turns .c into .o 
    ###################################
    #dependencias
    so_final.o: get_passwords_hashed.c get_passwords_hashed.h

的main.c

#include <stdio.h>
#include <string.h>
#include <crypt.h>

int testar_pass(char ant[],char (*pointer_hashes)[max_chars_string]){ // ponteiro para array de chars - char ** ant
     char * password ;
     char * encrypted;
     password = malloc(strlen(ant)*sizeof(char)); //password calculada recebida anteriror
     encrypted = malloc(strlen(ant)*sizeof(char));//hash
     strcpy(password,ant);
     encrypted = crypt(password,"10");
     if(strcmp(*pointer_hashes,encrypted) == 0){
         return 1;
         }
     else return 0;// devolve erro
}

1 个答案:

答案 0 :(得分:8)

在编译行的末尾传递-lm -lcrypt

LIBS=-lm -lcrypt

${CC} ${OBJS} -o $@ ${LIBS}

修改

gcc manual解释为什么它会产生影响(在评论中提出要求):

  

-llibrary

     

[...]

     

在您编写此选项的命令中,它会有所不同;链接器搜索并处理库中的库和目标文件   命令他们被指定。

     

因此,'foo.o -lz bar.o'在文件'foo.o'之后但在'bar.o'之前搜索库'z'。如果'bar.o'指的是'z'中的函数,那么这些函数   可能没有加载。