简体   繁体   English

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

[英]gcc -lcrypt flag error in UNIX C - Undefined Reference to crypt()

I use crypt() function and the compile flag named -lcrypt the problem is that the compiler says undefined reference to crypt() . 我使用crypt()函数和名为-lcrypt的编译标志,问题是编译器说对crypt()未定义引用。 Can anyone tell me what I'm doing wrong? 谁能告诉我我做错了什么?

Makefile Makefile文件

    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 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
}

Pass -lm -lcrypt at the end of your compilation line. 在编译行的末尾传递-lm -lcrypt

LIBS=-lm -lcrypt

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

EDIT: 编辑:

The explanation of why it makes a difference (as requested in a comment) from gcc manual : gcc 手册解释为什么它会产生影响(如评论中所要求的):

-llibrary -llibrary

[...] [...]

It makes a difference where in the command you write this option; 它在您编写此选项的命令中有所不同; the linker searches and processes libraries and object files in the order they are specified. 链接器按照指定的顺序搜索和处理库和目标文件。

Thus, 'foo.o -lz bar.o' searches library 'z' after file 'foo.o' but before 'bar.o'. 因此,'foo.o -lz bar.o'在文件'foo.o'之后但在'bar.o'之前搜索库'z'。 If 'bar.o' refers to functions in 'z', those functions may not be loaded. 如果'bar.o'引用'z'中的函数,则可能无法加载这些函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM