0

I'm a NOOB, trying to figure out how to pass a word and a salt to the DES crypt function, and print out an encrypted (well, poorly encrypted) password. I have read the manual pages, and a few posts on here (regarding both cyppt and the crack problem set I'm working through), but am still unsure as to what my syntax needs to be per my code below, which has an 'undefined reference to crypt.'

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <crypt.h>

#define _XOPEN_SOURCE

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

int main(void) {

    char *password = "lol";

    char *salt = "50";

    char *encrypted = crypt(password, salt);

    printf("%s\n", encrypted);
    return 0;
}

// should print 50cI2vYkF0YU2 per CS50 crack exercise example.

I've consulted the following before asking this question, and still don't understand how to get my simple example working. http://pubs.opengroup.org/onlinepubs/009695299/functions/crypt.html https://www.quora.com/How-does-the-crypt-function-work-with-some-examples-of-crypt-function-in-C https://cs50.stackexchange.com/questions/23583/cs-des-based-crypt-function crypt() function in C not working

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mike
  • 201
  • 2
  • 14
  • 2
    Undefined reference errors are usually linker errors, not compiler errors. You may just need to tell the linker (e.g., with `-lcrypt`) that you want to link with the `crypt` library. – Mike Holt Mar 20 '19 at 16:38
  • 3
    Why are you replicating the library declaration of `crypt`? Delete that line. The whole point of including a header file is that you pick up those declarations. – Tom Karzes Mar 20 '19 at 16:43
  • I copied your code into a file named `testcrypt.c`, and it compiled just fine with `gcc -o testcrypt testcrypt.c -lcrypt`. And for future reference, when using function calls from installed libraries, you can usually figure out which library to link with by viewing the man page. In this case, `man 3 crypt` will show you the man page for the `crypt` function, and it states "Link with -lcrypt". – Mike Holt Mar 20 '19 at 16:43
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Mike Kinghan Mar 20 '19 at 16:46
  • Please provide a *copy of the verbatim error code and the command you used to compile* in the question itself! – Antti Haapala -- Слава Україні Mar 20 '19 at 16:47
  • Thanks, but how do I link? I'm still getting errors when trying to run on repl.it, https://repl.it/@MichaelB4/FormalSteepExperiment. Errors are – Mike Mar 20 '19 at 16:48
  • exit status 1 /tmp/main-7dd4df.o: In function `main': main.c:(.text+0x34): undefined reference to `crypt' clang: error: linker command failed with exit code 1 (use -v to see invocation) – Mike Mar 20 '19 at 16:48
  • 1
    I took a look at repl.it, but I can't seem to find any discussion of how to link with external libraries. It's possible it just doesn't support that for the C language yet, or that if it is supported, it's an obscure, poorly documented, and/or hidden feature. – Mike Holt Mar 20 '19 at 17:05
  • Thanks Mike Holt. I hadn't considered it could be an issue with Repl.it. It works fine with another compiler. Thanks for taking the time to solve my problem. – Mike Mar 20 '19 at 17:21

1 Answers1

0

when compiling use this: gcc yourfilename.c -lcrypt