[英]“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.