0

I am trying to figure out how to include a DLL I created in one of my projects, but every guide tells me to use a .lib file which I don't have. I am using Visual Studio 2019 and compiling in plain C.

I first used Visual Studio's DLL template to make my DLL file in x64. This resulted in my folder appearing like this... enter image description here

Then I again used Visual Studio, but this time made a normal console application. I then first went into Properties -> C/C++ -> General -> Additional Include Directories and added the directory to MouseInput as show in image above. This then allowed me to include the "MouseInout.h" header file which worked fine. Next, I went Properties -> General -> Additional Library Directories and copied the path to the above Release directory. Lastly, I went Properties -> Input -> Additional Dependencies and tried adding MouseInput.lib (like most tutorials instruct you to do) but that failed and then I tried to just add MouseInput.obj but that also did not work.

Trying to build my console application results in a LINK2011 error saying, "precompiled object not linked in; image may not run" when I tried adding MouseInput.obj however, when I add MouseInput.lib (which just does not exist inside my project) I get a LINK1181 error saying, "cannot open file 'MouseInput.lib'". So, what exactly is the issue here? Where is my lib file?


Edit: I realized that I could go into my DLL's Configuration Properties -> General -> Configuration Type and change the project to a .lib file instead of a .dll file. However, from my understanding .lib is statically loaded, and I want it to be dynamically loaded. But there does not seem to be a way to use a dll without a lib so do I need to compile them separately? Will the code I have above work fine if I just include the dll in the same directory that the program was executed from?

  • Make sure that for your DLL `Linker -> Advanced -> Import Library` is set to `$(OutDir)$(TargetName).lib` – Axalo Apr 04 '23 at 00:05
  • @Axalo So inside of my console application I should just add ``$(OutDir)$(TargetName).lib`` to Import Library? Adding that still yields the same errors. – IgnoreExeption Apr 04 '23 at 00:09
  • Nope, DLL settings – Axalo Apr 04 '23 at 00:38
  • @Axalo Nope what? What is the solution? – IgnoreExeption Apr 04 '23 at 00:39
  • DLL project settings – Axalo Apr 04 '23 at 00:40
  • @Axalo Why are you typing like a half a sentence? Are you saying that I need to include ``$(OutDir)$(TargetName).lib`` at the location in my DLL properties? That is already present inside my DLL projects properties by default, so the issue still persists. – IgnoreExeption Apr 04 '23 at 00:44
  • You are supposed to get a .lib file after building a DLL, the import library. Stored in the solution folder, not the project folder. If you don't get one then you forgot to export the functions you want to call. Use [`__declspec(dllexport)`](https://stackoverflow.com/questions/538134/exporting-functions-from-a-dll-with-dllexport). – Hans Passant Apr 04 '23 at 15:13

1 Answers1

0

When you build the DLL, the compiler creates both the DLL file and a LIB file. The LIB file is called an import library. It does not contain any code but contains definitions of the functions in the DLL.

If the compiler does not generate the LIB file, then you have to enable Import Library in your DLL project, as @Axalo mentioned.

When you compile your application, the compiler needs the import library (LIB file). When you run your application, the DLL file is needed.

See this article for an example and detailed instructions.

rveerd
  • 3,620
  • 1
  • 14
  • 30
  • Yes, I get that, but I don't know how to generate a .lib and .dll file at the same time. – IgnoreExeption Apr 05 '23 at 06:07
  • _When you build the DLL, the compiler creates both the DLL file and a LIB file. If the compiler does not generate the LIB file, then you have to enable Import Library in your DLL project_. And _If you don't get one then you forgot to export the functions you want to call. Use `__declspec(dllexport)`_. What don't you understand? – rveerd Apr 05 '23 at 12:52
  • Go into your project's properties -> Configuration Properties -> General -> Configuration Type. Here you have to select whether you build your project in .lib or .dll when I want both. I need to the lib for linking with my projects and I need the dll so that each project can access the functions dynamically. The whole point of this question was to figure out how to use my dll files in my other projects. – IgnoreExeption Apr 05 '23 at 22:43