What I want is, as per the documentation, just use this code:
#include <sodium.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
int main(void)
{
if (sodium_init() == -1) {
return 1;
}
printf("Random data: %"PRIu32"\n", randombytes_random()); //returns a uint32_t piece of random data
return 0;
}
In the installation section for windows of the documentation, it states the precompiled windows version for MinGW32. As I'm using codeblocks with MinGW, I figured that those should work.
I downloaded the libsodium-1.0.13-mingw.tar.gz
file and extracted it. I took all the contents of the libsodium-win32
folder and put them in the MinGW root C:\Program Files (x86)\CodeBlocks\MinGW
(the folders include
, bin
and lib
match up as well, making me believe this is the proper way to go).
I then make a new project in codeblocks with the code snippet from above and compile with -std=c99 (although -std=gnu99 doesn't make a difference) and get the following warning:
*filepath*\testsodium\main.c 8 undefined reference to 'sodium_init'
*filepath*\testsodium\main.c 11 undefined reference to 'randombytes_random'
Which indicates to me that it for some reason can't find or doesn't actually include the <sodium.h>
. What's weird is that if I just take sodium_init()
(without checking the output in an if
statement), it gives me a warning stating:
*filepath*\testsodium\main.c 8 warning: ignoring return value of 'sodium_init', declared with attribute warn_unused_result [-Wunused-result]
Which indicates to me that it does actually gets parsed as it notices the lack of checking the result. Checking the build log I find that mingw32-gcc.exe
throws the -Wunused-result
warning while mingw32-g++
throws the error.
-------------- Build: Debug in testsodium (compiler: GNU GCC Compiler)---------------
mingw32-gcc.exe -Wall -g -Wall -std=c99 -c
*filepath*\testsodium\main.c -o obj\Debug\main.o
*filepath*\testsodium\main.c: In function 'main':
*filepath*\testsodium\main.c:8:5: warning: ignoring return value of 'sodium_init', declared with attribute warn_unused_result [-Wunused-result]
sodium_init();
^
mingw32-g++.exe -o bin\Debug\testsodium.exe obj\Debug\main.o
obj\Debug\main.o: In function `main':
*filepath*/testsodium/main.c:8: undefined reference to `sodium_init'
*filepath*/testsodium/main.c:9: undefined reference to `sodium_init'
*filepath*/testsodium/main.c:12: undefined reference to `randombytes_random'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
4 error(s), 1 warning(s) (0 minute(s), 0 second(s))
Can somebody help me figure out how to get sodium to work on my setup?