简体   繁体   English

使用crypt函数的“错误:链接程序命令失败,并显示退出代码”

[英]“error: linker command failed with exit code” for using crypt function

I am trying to use crypt function like this (I'm new to C, this is just for learning) 我正在尝试使用像这样的crypt函数(我是C的新手,这只是为了学习)

#include<stdio.h>
#define _XOPEN_SOURCE
#include <unistd.h>


char *crypt(const char *key, const char *salt);

int main()
{
    char* key="ilya";
    char* salt="xx";

    char* password=(char*)crypt(key, salt);

    printf("%s\n", password);

    return 0;
}

I compile it using make filename And I get the following error: 我使用make filename对其进行编译,然后出现以下错误:

/home/bla/password.c:20: undefined reference to `crypt'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Why is that? 这是为什么?

(I know it is a very lousy way to encrypt things, this is just for learning purposes) (我知道这是一种非常糟糕的加密方式,这只是出于学习目的)

Try gcc file.c -o file -lcrypt to link the libcrypt library if you're running Linux. 如果您正在运行Linux,请尝试使用gcc file.c -o file -lcrypt链接libcrypt库。

You can remove the (char*) cast from calling crypt() , it already returns a char * and also the declaration of the crypt() function since it's already provided from unistd.h . 您可以从调用crypt()删除(char*) ,它已经返回了char *并且还返回了crypt()函数的声明,因为它已经从unistd.h提供了。

I also suggest you change this: 我还建议您更改此:

char *key
char *salt

to

const char *key
const char *salt

Since they are pointing to read-only memory and will produce a SIGSEGV (Segmentation fault signal) if you try to modify the content they point to. 由于它们指向只读存储器,并且如果您尝试修改它们指向的内容,将产生SIGSEGV (分段故障信号)。

暂无
暂无

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

相关问题 铛错误:链接器命令失败,退出代码为1104 - clang error: linker command failed with exit code 1104 在此系统中不断收到此错误“链接器命令失败,退出代码为1” - keep getting this error in this system “linker command failed with exit code 1” 链接 C 文件错误:链接器命令失败,退出代码为 1 - Linking C files Error: linker command failed with exit code 1 C的makefile? :链接器命令失败,退出代码为1 - C makefile ? : linker command failed with exit code 1 C:链接器命令失败,退出代码为1 - C: linker command failed with exit code 1 Objective-C调用C函数-链接器命令失败,退出代码为1 - Objective-C call C function - linker command failed with exit code 1 为什么我得到 clang: error: linker 命令失败,退出代码为 1? - Why do I get clang: error: linker command failed with exit code 1? 铛:错误:链接器命令失败,退出代码为1(使用-v查看调用)MINIX3 - clang: error: linker command failed with exit code 1 (use -v to see invocation) MINIX3 “ ld:找不到体系结构x86_64的符号”上的“铛:错误:链接器命令失败,退出代码为1” - “clang: error: linker command failed with exit code 1” on “ld: symbol(s) not found for architecture x86_64” CS50 clang-10:错误:linker 命令失败,退出代码为 1(使用 -v 查看调用) - CS50 clang-10: error: linker command failed with exit code 1 (use -v to see invocation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM