【问题标题】:gcc -lcrypt flag error in UNIX C - Undefined Reference to crypt()UNIX C 中的 gcc -lcrypt 标志错误 - 未定义对 crypt() 的引用
【发布时间】:2017-01-26 17:16:50
【问题描述】:

我使用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

ma​​in.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
}

【问题讨论】:

  • 在这个问题上,这个问题被骗了很多......

标签: c unix


【解决方案1】:

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

LIBS=-lm -lcrypt

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

编辑:

gccmanual 对它产生影响的原因的解释(根据评论中的要求):

-图书馆

[...]

在命令的哪个位置编写此选项会有所不同;链接器在 它们被指定的顺序。

因此,“foo.o -lz bar.o”在文件“foo.o”之后但在“bar.o”之前搜索库“z”。如果‘bar.o’引用‘z’中的函数,那些函数 可能无法加载。

【讨论】:

  • 谢谢。对于更有用的答案,为什么有帮助的解释会很棒;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 2021-07-18
相关资源
最近更新 更多