2

I'm trying to use a library in Qt5 build from there: Link

I have added the .lib and .h file to my qmake file like this:

INCLUDEPATH += C:\xxxxxx\Privoxy\include
LIBS += -LC:\xxxxxx\Privoxy\lib -lLibPrivoxy

And try to call StartPrivoxy function in my .cpp file:

#include <QCoreApplication>
#include "WinPrivoxy/libprivoxy.h"
QString file = QCoreApplication::applicationDirPath() + "/privoxy.conf";
StartPrivoxy(file.toLocal8Bit().data());

And when I hit compile, the compiler gave me this error:

error LNK2019: unresolved external symbol "__declspec(dllimport) int __stdcall StartPrivoxy(char *)" (__imp_?StartPrivoxy@@YGHPAD@Z)

The .lib file's .h and .c file are:

// libprivoxy.h

#ifndef _LIBPRIVOXY_EXPORT_H
#define _LIBPRIVOXY_EXPORT_H

#ifdef LIBPRIVOXY_EXPORTS
#define LIBPRIVOXY_API __declspec(dllexport)
#else
#define LIBPRIVOXY_API __declspec(dllimport)
#endif

LIBPRIVOXY_API int __stdcall StartPrivoxy(char *config_full_path);

LIBPRIVOXY_API void __stdcall StopPrivoxy();

LIBPRIVOXY_API int __stdcall IsRunning();

#endif
// libprivoxy.c

#include "libprivoxy.h"
#include "miscutil.h"
#include <assert.h>

char g_privoxy_config_full_path[1024] = { 0 };
extern HMODULE g_hLibPrivoxyModule = NULL;
extern int g_terminate;
extern void close_privoxy_listening_socket();

LIBPRIVOXY_API int __stdcall StartPrivoxy(char *config_full_path)
{
    g_terminate = 0;

    strcpy_s(g_privoxy_config_full_path, 1024, config_full_path);

    // start privoxy
    WinMain( NULL,NULL,NULL, 0);

    return 0;
}

LIBPRIVOXY_API void __stdcall StopPrivoxy()
{
    g_terminate = 1;
    close_privoxy_listening_socket();
}

LIBPRIVOXY_API int __stdcall IsRunning()
{
    return 1 == g_terminate ? 0 : 1;
}

I'm using Qt 14.0.0 and Visual Studio 2017 Enterprise on Windows 10 18363.592

Hhry
  • 823
  • 1
  • 8
  • 19

1 Answers1

1

According to https://doc.qt.io/qtcreator/creator-project-qmake-libraries.html and https://doc.qt.io/qt-5/third-party-libraries.html your qmake setup is accurate (maybe add DEPENDPATH += ...).

The problem seems to be the .dll import because normally this external symbol unresolved error is thrown when there are problems with linked dlls (https://social.msdn.microsoft.com/Forums/vstudio/en-US/d94f6af3-e330-4962-a150-078da57ee5d0/error-lnk2019-unresolved-external-symbol-quotdeclspecdllimport-public-thiscall?forum=vcgeneral)

When searching through the privoxy git i can not find a .dll. Did you compile a .dll for privoxy and put it in the path you specified ? (Compile a DLL in C/C++, then call it from another program)

As far as i understand you, you want to use libprivoxy as source code. Then you do not need to import a .dll that does not exist because you did not compile it.

Note that there is a difference between statical and dynamical linking and as far as i understand you you want to link statically (.dll is an abbreviation for dynamically linked library)

ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • I used the Options ``Lib_Debug`` generated a ``LibPrivoxy.lib`` file instead of ``.dll`` file. – Hhry Feb 09 '20 at 10:32
  • I have to compile it into a library, Directly using ``privoxy`` in my project causing tons of errors. – Hhry Feb 09 '20 at 10:33
  • i do not know the option `Lib_Debug` and a `.lib` file can either be a static library (generated by `LIB.exe`, the library manager) or the import library of a `.dll` (generated by `LINKER.exe`, the linker). You can determine this like described in https://stackoverflow.com/questions/6402586/know-if-lib-is-static-or-import – ralf htp Feb 09 '20 at 11:06
  • It's static library. – Hhry Feb 09 '20 at 11:33
  • Maybe try this https://stackoverflow.com/questions/8581247/vs2010-link-in-a-single-library-statically. This will only work if the library was compiled correctly. Only for completeness, when linking dynamically the linking can be *explicit* or *implicit* (https://www.tenouk.com/ModuleBB.html) And delete the dll import in your code – ralf htp Feb 09 '20 at 11:46
  • How to set Additional Dependencies in Qt Creator? – Hhry Feb 09 '20 at 12:33
  • This is also in https://doc.qt.io/qtcreator/creator-project-qmake-libraries.html – ralf htp Feb 09 '20 at 12:37