0

Hello everyone!

I have a following situation: I need to import into my Visual Studio 2019 C++ project (call this A) methods from another very very big project (call this B). Actually I have a .sol file for this B project and could succesfully build static library (B.lib) as well as dynamic (B.dll). A problem is - I want to call some methods from B in A but i couldn't just load a header of B and create some class in A, which inherits the needed class in B with all needed methods, because B is really a very big project, and in the "main header" there are used lots of self-defined classes, which are just incorporated into project, and I will be really stuck trying to import a header, in order to make header work, in order to make the "main" header work in my project etc. I think you got the idea.

The second thing is more concrete: I tried to load DLL with LoadLibrary and GetProcAdress. It works. Actually in B for all methods, which I really need, it is defined a void external version of each of them, which takes a pointer to the class object as a parameter. A question is - how to get the instance of this class if there is no factory function in B - i.e. function returning this needed class object's pointer, which I could load from .dll into my A? I couldn't attach a proper code, since it is cw protected. Maybe it will be possible with .lib file? How do thois pair of things work together, that's really my question.

So, is there any way how I could get the methods from those libraries? Please explain more in detail, if you have time, I'm a junior in C++ development and I don't always clearly get what does it mean smth. like "just link .lib file and use it" - how to use?.. and so on

Thanks in advance for help!

gehirndienst
  • 424
  • 2
  • 13
  • Sorry, for some reason it is skipped the first greetings line :( Hello everyone and sorry for that, I don't know why is it happened. Don't want to show disrespect. – gehirndienst Mar 12 '20 at 08:51

2 Answers2

1

Static libraries increase the size of the code in your binary. They're always loaded and whatever version of the code you compiled with is the version of the code that will run.

Dynamic libraries are stored and versioned separately.And dynamic libraries aren't necessarily loaded -- they're usually loaded when first called -- and can be shared among components that use the same library (multiple data loads, one code load).

You could choose according to your needs whether to use dynamic or static linking.

Use static linking: To link with a .lib file, you just need to:

1,Add the path to the header file to the Additional Include Directories(property - >c/c++ -> General -> Additional Include Directories) 2,Add the path to the .lib file to the Additional Library Directories (property -> linker -> General -> Additional Library Directories) 3,Add the name of the .lib file with its extension to Additional Dependencies (property -> linker -> input -> Additional Dependencies)

For more details I suggest you could refer to the link: https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-static-library-cpp?view=vs-2019

Use dynamic linking: If you have a.dll and a.lib, you could use statically loaded dll.

1,Add the path to the header file to the Additional Include Directories(property - >c/c++ -> General -> Additional Include Directories) 2,Add the path to the .lib file to the Additional Library Directories (property -> linker -> General -> Additional Library Directories) 3,Add the name of the .lib file with its extension to Additional Dependencies (property -> linker -> input -> Additional Dependencies) 4,Add a Post-Build Event to your project, to add a command that copies the DLL to your build output directory( Properties > Build Events > Post-Build Event )

For more details I suggest you could refer to the link: https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=vs-2019

If the dll does not have a corresponding .lib file, then the DLL can only be loaded using dynamic loading.

1, create a function pointer whose pointer data type is to match the calling DLL export function. 2, through the Win32 API function LoadLibrary () explicitly call the DLL, this function returns the DLL's instance handle. 3, through the Win32 API function GetProcAddress () to get the function address of the DLL to be called, the result is assigned to the pointer type of the custom function. 4, use the function pointer to call the DLL function. 5. After the final call is completed, the DLL function is released via the Win32 API function FreeLibrary().

For more details I suggest you could refer to the link: https://learn.microsoft.com/zh-cn/windows/win32/dlls/using-run-time-dynamic-linking

Jeaninez - MSFT
  • 3,210
  • 1
  • 5
  • 20
  • Thanks a lot for the answer. I did everything what you said and now I have a problem. Imagine, that I have a main header **B.h** from the project of that library and now, following the given instructions, I must include this header in my project **A**, where I load .lib file. In the original project **B** that header is working perfectly, no problem, but when I include it into my project, as well as I wrote a path to it, a path to .lib file and added a name of a .lib file in additional dependencies - it results in lots of errors in **B.h** into my project **A**. What am I doing wrong? – gehirndienst Mar 13 '20 at 09:06
  • @ gehirndienst, **"it results in lots of errors in B.h into my project A. "** Could you please tell us what errors have occurred? – Jeaninez - MSFT Mar 16 '20 at 07:04
0

This can be achieved by statically linking the B to A. First you have to build the solution of B and obtain the lib file. Then add the lib file as additional dependency to the project of A. Finally separate the header files of B and add the location of header file to Additional Include Directories section of A.

Steps:

  1. Build the B and obtain the B.lib

  2. From B's source code extract the header files to a separate directory. (let's call this directory INC_DIR)

  3. Open the A's solution right click on A's project select properties to open property window.
  4. On property window go to C/C++ -> Additional Include directories. Add the location of INC_DIR to Additional Include directories
  5. On property window go to Linker -> General -> Additional Library Directories. Add the location of B.lib to Additional Library Directories.
  6. On property window go to Linker -> Input -> Additional dependencies. Add the name of the lib (B.lib) to Additional dependencies.

This links shows clearly what to do : https://stackoverflow.com/a/23882710/5746085.

Search on internet Static library linking + visual studio for further information.

hasi90
  • 84
  • 8
  • Thanks a lot for the answer! I think I've already tried to do this and I don't cleary understand a statement from the given link: *"Now you should be able to use the library. Remember to #include it in your files"* How to include it? And what is "it"? `#include "B.lib` won't work and I couldn't include headers directly, I specified above reasons for that. I did everything you said, but still not understand how to use the methods and/or the classes from **B.lib** in my **A** project. – gehirndienst Mar 12 '20 at 09:47
  • I have update the answer and added steps to follow. you can not do #include "B.lib. You can get the header files from B's source code. Header files are must need. – hasi90 Mar 12 '20 at 10:03
  • I did all the steps. Now: how to use methods from **B.lib**, if I cannot write something like `#include "B.h"` in my **A.h** or **A.cpp**? That's my question, sorry if I wrote it not so clear. – gehirndienst Mar 12 '20 at 10:13
  • You can include the header file using "#include B.h". So Project "A" can see the definitions of B. What i mentioned earlier is you can not include lib files. i.e #include B.lib is can not be done. – hasi90 Mar 12 '20 at 12:22
  • Yes, but sadly I cannot include a header. It containts one class, which inherits around 10 other classes, for each of them I need to include an appropriate header, and then I become errors and conflicts, because all those headers were hard coded in **B**. So, this way is very hard, that's why I was asking is there a way to avoid including a header but still have an access to methods? – gehirndienst Mar 12 '20 at 12:39