C++ - Undefined reference to “sodium_init”

2019-09-18 18:43发布

问题:

I am attempting to make a testing application using libsodium, however I am getting the error:

main.cpp:6: undefined reference to `sodium_init'

I ran the following commands to install in as the root user.

$ ./configure
$ make && make check
$ make install

This the code that is having the issue.

#include <stdio.h>
#include <sodium.h>

int main(int argc, char **argv)
{
    if (sodium_init() == -1)
    {
        return 1;
    }

    printf("libsodium had no issues!\n");
    return 0;
}

I am using CodeLite as my IDE, and my C++ compiler options are the following:

-g;-O0;-Wall;-lsodium

The options were default and I added -lsodium to the list.

Attempting to compile main.cpp directly from the terminal with the following command g++ -lsodium main.cpp throws the same error.

Could someone please help me with my issue.

回答1:

Libraries for linking are searched in order, so you need to place the libraries after your local translation units:

g++ main.cpp -lsodium

In your IDE, make sure you add -lsodium as a linker argument.