ccryptographybitcoinsecp256k1

Failed to compile with secp256k1 library


So I was following this tutorial: https://nickfarrow.com/Cryptography-in-Bitcoin-with-C/ I installed libsecp256k1 from https://www.howtoinstall.me/ubuntu/18-04/libsecp256k1-dev/ but while compiling my program:

#include <secp256k1.h>
#include <stdio.h>
static secp256k1_context *ctx = NULL;

int main()
{
    ctx = secp256k1_context_create(
        SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
    /* Declare the private variable as a 32 byte unsigned char */
    unsigned char seckey[32];

    /* Load private key (seckey) from random bytes */
    FILE *frand = fopen("/dev/urandom", "r");

    /* Read 32 bytes from frand */
    fread(seckey, 32, 1, frand);

    /* Close the file */
    fclose(frand);

    /* Loop through and print each byte of the private key, */
    printf("Private Key: ");
    for (int i = 0; i < 32; i++)
    {
        printf("%02X", seckey[i]);
    }
    printf("\n");
}

i get:

josh@pc:~/Code$ gcc prvkey.c -o exec
/tmp/cc5OVPMJ.o: In function `main':
prvkey.c:(.text+0x1d): undefined reference to `secp256k1_context_create'
collect2: error: ld returned 1 exit status

Thanks in advance!


Solution

  • Try:

    gcc prvkey.c -o exec -lcrypto -lsecp256k1
    

    gcc -l links with a library file.

    Let me know if that works or any questions let me know.