How to include functions from DLL

Gunnar Vestergaard 65 Reputation points
2023-02-27T16:37:51.5966667+00:00

Hello. I am a newcomer using Visual Studio 2022 community edition. This should be easy to solve. I only need to link to a library, as I use some functions in it. But I don't know how to do it.

I have been trying out an example application which I downloaded. This concerns the SP (SGML Parser) library made by James Clark. He is actually the father of XML 1.0 and a lot of other web standards. He is famous. He constructed a library that can be called. He also constructed the following small example which shows basic usage of SP (SGML Parser) library.

When trying to compile his example I get linker errors.

The source for the example:

#include <iostream>

// The next two lines are only to ensure bool gets defined appropriately.
#include "config.h"
#include "Boolean.h"

#include "ParserEventGeneratorKit.h"
#include <iostream>

std::ostream& operator<<(std::ostream& os, SGMLApplication::CharString s)
{
    for (size_t i = 0; i < s.len; i++)
        os << char(s.ptr[i]);
    return os;
}

class OutlineApplication : public SGMLApplication {
public:
    OutlineApplication() : depth_(0) { }
    ~OutlineApplication() {}
    void startElement(const StartElementEvent& event) {
        for (unsigned i = 0; i < depth_; i++)
            std::cout << "    ";
        std::cout << event.gi << '\n';
        depth_++;
    }
    void endElement(const EndElementEvent&) { depth_--; }
private:
    unsigned depth_;
};

int main(int argc, char** argv)
{
    ParserEventGeneratorKit parserKit;
    // Use all the arguments after argv[0] as filenames.
    EventGenerator* egp = parserKit.makeEventGenerator(argc - 1, argv + 1);
    OutlineApplication app;
    unsigned nErrors = egp->run(app);
    delete egp;
    return nErrors > 0;
}

When I compile it, I get 23 error messages. The linker can not find the functions that are referenced in this example. First error out of 23:

Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: virtual __cdecl SGMLApplication::~SGMLApplication(void)" (__imp_??1SGMLApplication@@UEAA@XZ) referenced in function "public: virtual __cdecl OutlineApplication::~OutlineApplication(void)" (??1OutlineApplication@@UEAA@XZ)	SP example 1	C:\Users\Dell\OneDrive\source\repos\SP example 1\SP example 1.obj	1	

Second error of 23:

Severity	Code	Description	Project	File	Line	Suppression State
Error	LNK2019	unresolved external symbol "__declspec(dllimport) public: __cdecl SGMLApplication::SGMLApplication(void)" (__imp_??0SGMLApplication@@QEAA@XZ) referenced in function "public: __cdecl OutlineApplication::OutlineApplication(void)" (??0OutlineApplication@@QEAA@XZ)	SP example 1	C:\Users\Dell\OneDrive\source\repos\SP example 1\SP example 1.obj	1	

I suspect that I need to edit properties in Visual Studio 2022 to link to the right library. How do I get rid of these 2 error messages? This should be easy to solve.

Thanks for reading this.

Windows development Windows API - Win32
0 comments No comments
{count} vote

Accepted answer
  1. RLWA32 49,536 Reputation points
    2023-02-27T17:15:53.8066667+00:00

    Generally a third-party DLL is distributed with an associated header file and an import library (.lib file). It is also important that you use the same version of VC++ that was used to create the DLL and its associated import library.

    If you have the import library for the DLL then you can include the following statement in your source code. Make sure to specify the actual name of the import library. It should provide the input needed by the linker to resolve the LNK2019 errors -

    #pragma comment(lib, "importlibrary.lib")
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.