0

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?

  • 1
    You don't *link* with the actual library. Somewhere in the project settings of Code::Blocks there should be a tab or similar that contain liker settings. In it you need to add the actual library. Just including the header file is not enough. – Some programmer dude Aug 03 '17 at 13:38
  • What's the syntax for linking this library? I can't seem to find it in the documentation. – realbananas Aug 04 '17 at 10:26

1 Answers1

0

On code Blocks you have to link the library where ever they are. 1) You have to precise where are the include files. 2) Where are the lib file (ending by .a in mgw I think) If I remember right you do that in: Project>Build Option>Linker
A more complete answer is in the following post https://stackoverflow.com/a/23050127/7850639

Brighter side
  • 392
  • 2
  • 14
  • Ok, I found the "link libraries" and included the .a's there. How can I do 1)? As in: where and how do I "precise where the include files are"? I assume this has to be done in the "other linker options" field, but I have no clue about the syntax and can't seem to find it online. Now when building I get a "undefined reference to ' SystemFunction036@8'" , probably because it found the functions to be linked, but not the actual functions themselves or something. – realbananas Aug 04 '17 at 10:24
  • To add the include file you need to add them to the project (tab on the left for me but may have moved it 1 day). There you have your project and 2/3 folders contained: source, header, others (if you have image ) you need to add your files there. To do that you right click on it (the project) and add files or something like that. I don't know your new error will find out this evening. – Brighter side Aug 04 '17 at 15:57
  • But this should be a basic library, hence why we use and not some form of relative path "sodium.h". It should automatically include the file to build since it should be (become) a native mingw library. – realbananas Aug 07 '17 at 08:41
  • Hello, have you tried that https://stackoverflow.com/questions/5862757/how-do-i-link-to-a-library-with-codeblocks with your library. For your problem that you want to use instead of "sodium.h" it will depend if your file is in your project folder or in an include folder. – Brighter side Aug 07 '17 at 08:48