简体   繁体   中英

Create a c++ library without a class name

I'm trying to create a C++ dynamic library that won't have a class. I'd like it to work similar to how you can include <string.h > and call strlen directly.

I can create a class that will compile, but won't link correctly with my library.

Here's the test library I'm working on now:

Header

#ifndef _DLL_H_
#define _DLL_H_

BOOL APIENTRY DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved);
extern "C" __declspec(dllexport) int testMethod(int a);

#endif

Cpp

#include "dll.h"

int testMethod(int num)
{
    std::cout << "test message" << std::endl;
    return 1;
}

BOOL APIENTRY DllMain (HINSTANCE hInst,     // Library instance handle.  ,
                   DWORD reason,        // Reason this function is being called.  ,
                   LPVOID reserved)     // Not used.  )
    {
    switch (reason)
    {
      case DLL_PROCESS_ATTACH:
        break;

      case DLL_PROCESS_DETACH:
        break;

      case DLL_THREAD_ATTACH:
        break;

      case DLL_THREAD_DETACH:
        break;
    }

// Returns TRUE on success, FALSE on failure 
return TRUE;
}

Finally, here's the class I'm using to test the Dll, which is told to link against the lib mingw outputs

#include <iostream>
#include "../Dll/dll.h"

using namespace std;

int main(int argc, char *argv[])
{
    testMethod(5);
}

I haven't used C++ in about a year so I'm pretty rusty

extern "C" __declspec(dllexport) int testMethod(int a);

This needs to be dllimport in the application which is linking against the DLL. Most people compile their DLL with a #define which controls if it is an export or an import.

#ifdef INSIDE_MYDLL
#define MYDLLAPI __declspec(dllexport)
#else
#define MYDLLAPI __declspec(dllimport)
#endif

extern "C" MYDLLAPI int testMethod(int a);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM