VS2019: Linking Static Library Functions to DLL

Viewed 281

I have three projects in my visual studio solution: project A, project, B, and project C. Projects A and B compile to A.lib and B.lib, respectively, and project C compiles to C.dll. I'd like to take the functions in A.lib and B.lib and put them in C.dll - what can I do to make this happen? I have a feeling that I'm using __declspec(dllexport) at the wrong time, or that I'm forgetting to link something.

=== PROJECT A ===

foo.h

#include <iostream>
__declspec(dllexport) void foo();

foo.cpp

#include "foo.h"
void foo()
{
    std::cout << "Foo!\n";
}

=== PROJECT B ===

goo.h

#include <iostream>
__declspec(dllexport) void goo();

goo.cpp

#include "goo.h"
void goo()
{
    std::cout << "Goo!\n";
}

=== PROJECT C ===

// What goes here?
1 Answers

There is no need to use __declspec(dllexport) in static libraries.

foo.h can be modified as:

#include <iostream>
 void foo();

Do not forget to add lib file in Dll's properties. For your reference: Create and use a static library and Create and use your own Dynamic Link Library (C++)

MyDLL: Header.h

#pragma once
#include"foo.h"
#include"goo.h"

#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport)
#else
#define MATHLIBRARY_API __declspec(dllimport)
#endif

extern "C" MATHLIBRARY_API  void Myfun();

Cpp file

#include"Header.h"
void Myfun()
{   
        foo();
        goo();
}

The test project:

#include <iostream>
#include"Header.h"//from MyDll
int main()
{
    Myfun(); 
}

In test project, I only input Mydll.lib. And I also include directories of foo.h and goo.h.

Output:

Foo!
Goo!
Related