1

I am trying out a simple test DLL project. Under my solution, I have two projects - first C++ Dll (library) and second C++ exe (driver). Below I've attached a snapshot of the basic project setup:

project setup

dllmain.h

#ifndef DLLMAIN_H_
#define DLLMAIN_H_

#ifdef FFMPEGLIB_EXPORTS
#define FFMPEGLIB_API __declspec(dllexport)
#else
#define FFMPEGLIB_API __declspec(dllimport)
#endif // FFMPEGLIB_EXPORTS

static void TestFoo();

extern "C" FFMPEGLIB_API void Test(int* num);

extern "C" FFMPEGLIB_API void ProxyFoo();

#endif

dllmain.cpp

#include "dllmain.h"
#include "A.h"

void TestFoo()
{
    A a;
    a.foo();
}

void Test(int* num)
{
    *num = *num + 1;
}

void ProxyFoo()
{
    TestFoo();
}

driver.cpp

#include <iostream>

#include "dllmain.h"

int main(int argc, char** argv)
{
    int mum  = 4;
    Test(&num);

    std::cout << num;

    ProxyFoo();

    return 0;
}

The library project compiles normally, but the exe fails to compile with a linker error:

linker error

Code        Description                                 Project     File
LNK2001     unresolved extern symbol _imp_ProxyFoo      driver      driver.obj
LNK2001     unresolved extern symbol _imp_Test          driver      driver.obj
LNK1120     2 unresolved externals                      driver      driver.exe

I have two questions here:

  1. Why does the function name of dllmain.h get mangled in spite of being marked as extern "C"?

  2. Why can I not create an instance of test class A from extern methods? What would be a good way of doing that?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
uzer
  • 35
  • 5

2 Answers2

2

Why the function name of dllmain.h getting mangled in spite being marked as extern "C"?

Because __declspec(dllimport).

Why can I not create instance of test class A from extern methods? What would be good way of doing it?

I think that's fine, but you didn't provide any class A code. Just do this:

class __declspec(dllexport) A
{
    /* ... */
};

Why EXE compile failed?

This is because you have not imported the LIB file of the DLL into the project.

There are two ways to import it:

  1. Add #program comment(lib, "<YOUR_LIB_FILE>.lib") to the code file.
  2. Add <YOUR_LIB_FILE>.lib to Properties -> Linker -> Input -> Additional Dependencies.

Microsoft documentation: https://learn.microsoft.com/en-us/cpp/build/importing-and-exporting

Sprite
  • 3,222
  • 1
  • 12
  • 29
  • So it's seems to be working now... with only one bit of change - defining static lib under the Linker -> input Can you provide some reference on linking the dll without the static library – uzer Nov 25 '20 at 21:48
  • @uzer But this would be troublesome, because you would need to define the function type and get the address for each `extern "C"` exported function, which is almost impossible for exported C++ classes. Reference: https://learn.microsoft.com/en-us/cpp/build/linking-an-executable-to-a-dll?view=msvc-160#how-to-link-explicitly-to-a-dll – Sprite Nov 25 '20 at 21:53
  • Yes I looked into that approach and it seems bit convoluted for simple task... anyways thanks for clearing this out :) – uzer Nov 25 '20 at 21:55
  • In this case its not called a static library. It's an import library. Related: [https://stackoverflow.com/questions/3573475/how-does-the-import-library-work-details](https://stackoverflow.com/questions/3573475/how-does-the-import-library-work-details) – drescherjm Nov 25 '20 at 22:19
-1

You need to put the extern "C" thing around your function definitions that you intend to export in dllmain.cpp so it matches the linkage of your declaration.

Also, you need to do the declexport thing too.

extern "C"
{
    __declspec(dllexport) void Test(int* num)
    {
       *num = *num + 1;
    }

    __declspec(dllexport) void ProxyFoo()
    {
        TestFoo();
    }
}
selbie
  • 100,020
  • 15
  • 103
  • 173
  • Answer updated. You need to declexport them as well. – selbie Nov 25 '20 at 21:27
  • Applying the `extern "C"` and `__declspec(dllexport)` to the function *declarations* will suffice, they do not need to be applied to the function *definitions* as well. – Remy Lebeau Nov 25 '20 at 21:33