Manos Kounelakis
Manos Kounelakis

Reputation: 3181

Use libsodium in NAPI

I am using a library which depends on libsodium (libpaseto). I have installed it on my machine and I am trying to build a nodejs addon.

I have the following binding.gyp file:

{
    "targets": [{
        "target_name": "testaddon",
        "cflags!": [ "-fno-exceptions"],
        "cflags_cc!": [ "-fno-exceptions" ],
        "cflags_cc":["-std=c++1z" ,"-lsodium","$(pkg-config --cflags libsodium)"],
        "ldflags_cc":["$(pkg-config --libs libsodium)"],
        "sources": [
            "cppsrc/main.cpp",
            "cppsrc/functionexample.cpp",
            "cppsrc/libpaseto/src/helpers.c",
            "cppsrc/libpaseto/src/paseto.c",
            "cppsrc/libpaseto/src/paseto_v2_local.c",
            "cppsrc/libpaseto/src/paseto_v2_public.c",
        ],
        'include_dirs': [
            "<!@(node -p \"require('node-addon-api').include\")"
        ],
        'libraries': [],
        'dependencies': [
            "<!(node -p \"require('node-addon-api').gyp\")"
        ],
        'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ,'SODIUM_STATIC=1',
            'HAVE_LIBM=1']
    }]
}

Here is the code for my addon:

#include "./headers/functionexample.h"
#include <iostream>
#include <fstream>
#include "libpaseto/include/paseto.h"
#include <exception>
Napi::Boolean addon::InitPasetoWrapped(const Napi::CallbackInfo &info)
{
    bool returnValue = paseto_init();
    Napi::Env env = info.Env();
    return Napi::Boolean::New(env, returnValue);
}

Napi::Object addon::Init(Napi::Env env, Napi::Object exports)
{
    exports.Set("add", Napi::Function::New(env, addon::addWrapped));
    exports.Set("getOsName", Napi::Function::New(env, addon::getOSNameWrapped));
    exports.Set("writeToFile", Napi::Function::New(env, addon::writeFileWrapped));
    exports.Set("initPaseto", Napi::Function::New(env, addon::InitPasetoWrapped));
    return exports;
}

I am able to compile it without a problem. I am exporting some methods in my nodejs code. However when I try to run the exported method initPaseto from my js code I get the following error:

node:symbol lookup error: undefined symbol:sodium init

How can I fix it?

Upvotes: 0

Views: 384

Answers (1)

Manos Kounelakis
Manos Kounelakis

Reputation: 3181

So I finally found the answer.In the libsodium documentation it mentions that you have to pass the -lsodium flag to be able to compile without problems.So what I had to do was to add this flag in my libraries in binding.gyp. So here is my final binding.gyp file:

{
    "targets": [{
        "target_name": "testaddon",
        "cflags!": [ "-fno-exceptions"],
        "cflags_cc!": [ "-fno-exceptions" ],
        "cflags_cc":["-std=c++1z"],

        "sources": [
            "cppsrc/main.cpp",
            "cppsrc/functionexample.cpp",
            "cppsrc/libpaseto/src/helpers.c",
            "cppsrc/libpaseto/src/paseto.c",
            "cppsrc/libpaseto/src/paseto_v2_local.c",
            "cppsrc/libpaseto/src/paseto_v2_public.c",
        ],
        'include_dirs': [
            "<!@(node -p \"require('node-addon-api').include\")"
        ],
        'libraries': ['-lsodium'],
        'dependencies': [
            "<!(node -p \"require('node-addon-api').gyp\")"
        ],
        'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ,'SODIUM_STATIC=1',
            'HAVE_LIBM=1']
    }]
}

Also this github issue helped me to understand how to add a library in my project: https://github.com/nodejs/node-gyp/issues/328

Upvotes: 0

Related Questions