2

I created a very simple Qt project that's using libsodium. (I can create a same project and build well with Visual Studio 2010/2013.) But Qt Creator cannot build:

main.obj:-1: error: LNK2019: unresolved external symbol sodium_init referenced in function main

This is my project:

testSodium.pro:

QT       += core
QT       -= gui
TARGET = testSodium
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
DEFINES += SODIUM_STATIC
INCLUDEPATH += F:/libsodium-1.0.2-msvc/include
LIBS += -LF:/libsodium-1.0.2-msvc/Win32/Release/v120/static/ -llibsodium
SOURCES += main.cpp

main.cpp:

#include <sodium.h>

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

Can anyone help me?

(I'm using Qt Creator 3.3.1, Qt 5.4.1 MSVC 2010 32 bit)
libsodium: https://download.libsodium.org/libsodium/releases/libsodium-1.0.2-msvc.zip

temoto
  • 5,394
  • 3
  • 34
  • 50
aviit
  • 1,957
  • 1
  • 27
  • 50
  • Do you have libsodium.a actually built in this directory? Try to change to -llibsodium.a in pro file – demonplus Apr 10 '15 at 08:50
  • I'm using MSVC. .a is for mingw or linux, right? – aviit Apr 10 '15 at 08:52
  • And what is the actual name of your dll? – demonplus Apr 10 '15 at 08:53
  • I built libsodium from source or get the lib from above link but both failed. Same error. – aviit Apr 10 '15 at 08:53
  • The name is: libsodium.lib in all cases. – aviit Apr 10 '15 at 08:55
  • I tried in .pro file: LIBS += F:/libsodium-1.0.2-msvc/Win32/Release/v120/static/libsodium.lib but failed too. – aviit Apr 10 '15 at 08:57
  • And why you use Win32? Do you have 32 bit project? – demonplus Apr 10 '15 at 08:58
  • Can you take a look at libsodium-1.0.2-msvc.zip? I just try to build only. 32 or 64 is not a problem. I want to build this simple project in Qt that's using libsodium on Windows. – aviit Apr 10 '15 at 09:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/74912/discussion-between-aviit-and-demonplus). – aviit Apr 10 '15 at 09:16
  • 1
    @aviit I just built it without any problem, using Qt Creator 3.1.1, Qt 5.3.0 MSVC 2010 32 bit. (http://imgur.com/RBMjmvC) – Tay2510 Apr 10 '15 at 09:18
  • @Tay2510: Can you show me the .pro file? It's strange for me – aviit Apr 10 '15 at 09:26
  • @aviit I just use your `.pro` file, and the only differences are the `TARGET` name and my libsodium library is stored in disk `C`, so basically it works on my machine. – Tay2510 Apr 10 '15 at 09:28
  • I have installed Qt from qt-opensource-windows-x86-msvc2013_64_opengl-5.4.1.exe but in Qt Creator about says that: Qt Creator 3.3.1, Qt 5.4.1 MSVC 2010 32 bit. I have both msvc2010 and 2013 on my machine. I'm trying your version 5.3.0. – aviit Apr 10 '15 at 09:35
  • 2
    @aviit Qt version doesn't matter. Instead, the compiler version is the point. Please make sure your Qt and libsodium library are built with the same version of compiler (MSVC 2010 or 2013). For example, there could be incompatibility if you use MSVC2013 to compile Qt-MSVC2013 and libsodium-MSVC2010 (I guess it, since it works on my machine) – Tay2510 Apr 10 '15 at 09:50
  • @Tay2510: yes, I know that but I tried with all versions msvc 10, 11, 12 (from libsodium or built by myself) – aviit Apr 10 '15 at 11:59
  • @aviit In Qt creator, which compiler did you use to build the project? – Tay2510 Apr 10 '15 at 12:11
  • @aviit Problem solved? What's going on? – Tay2510 Apr 10 '15 at 12:59
  • The problem was solve (the answer now deleted) was a mix in the x86 vs. x64 binaries - this has a duplicate; http://stackoverflow.com/a/24291364/3747990 – Niall Apr 13 '16 at 08:36

1 Answers1

2

In fact, libsodium is a library written in pure C.

That means if you want to import it into a C++ project, you have to add an extern "C" identifier.

So you should include the header file like this:

#ifdef __cplusplus
extern "C"{
#endif

#include <sodium.h>

#ifdef __cplusplus
}
#endif
Community
  • 1
  • 1
DemoHn
  • 23
  • 4
  • Is standard practice for C libraries, that will be used by other people, to do that on their own headers. It saves other programmers having to do that on their own each time they want to call a C library from a C++ program. – rxantos Jan 30 '22 at 16:53