ilyo
ilyo

Reputation: 36391

"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)

#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:

/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)

Upvotes: 0

Views: 457

Answers (1)

Jite
Jite

Reputation: 4368

Try gcc file.c -o file -lcrypt to link the libcrypt library if you're running Linux.

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.

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.

Upvotes: 1

Related Questions