How can I include <sodium.h> correctly in Visual Studio?

3.4k views Asked by At

I read section Compilation on Windows in installation page but I still very confused, I hope some experts can enlighten me.


I downloaded LATEST.tar.gz from here.

After that, I copied sodium.h and sodium folder in libsodium-1.0.12\src\libsodium\include to my project.

Here is the code:

#include <stdio.h>
#include "sodium.h"
#pragma warning (disable:4996)

void main()
{
    char myString[32];
    uint32_t myInt;

    /* myString will be an array of 32 random bytes, not null-terminated */
    randombytes_buf(myString, 32);

    /* myInt will be a random number between 0 and 9 */
    myInt = randombytes_uniform(10);

    printf("%d", myInt);

    system("pause");
}

And I get these errors when compiling:

Error LNK1120 2 unresolved externals

Error LNK2019 unresolved external symbol __imp__randombytes_buf referenced in function _main

Error LNK2019 unresolved external symbol __imp__randombytes_uniform referenced in function _main

I did not get error like "cannot open sodium.h".

How can I solve this?

Any help is appreciated.

1

There are 1 answers

2
FKEinternet On BEST ANSWER

Your errors are telling you there's a problem at link time - so your issue isn't with including sodium.h. There's a library that's not being added to your project. You can't just copy the library to your project directory, you need to tell Visual Studio to link it in.