How to access the methods in .lib file in Win 32 console application

141 views Asked by At

I am trying to access the .lib file using visual studio 2012. below are the steps I followed

  1. Created a class library, and included header(.hpp) and .lib file

    Header file :- extern "C" unsigned int GetKey();

    Class library

    public: static unsigned int GetKey1() { return GetKey(); }

  2. Built the application and it is successful, lets call it as classlibrary

  3. Created a win 32 console application and added the above dll as reference, tried to access the method in dll, built got succeed, but when i tried to run it , it showing file not found exception in classlibrary.dll or its dependencies like below.

Source file

using namespace ManagedMathFuncsLib;

int main()
{

   Class1 cs;

   cout<<"default value of variable from dll : "<< cs.GetKey1()<<endl;

   return 0;
}

Error An unhandled exception of type 'System.IO.FileNotFoundException' occurred in Unknown Module.

Additional information: Could not load file or assembly 'XXXXXXX.dll' or one of its dependencies. The specified module could not be found.

If there is a handler for this exception, the program may be safely continued.

         **Do i need the dll related to .lib file? Please help.**
1

There are 1 answers

4
xMRi On BEST ANSWER
  1. You have no lib file. This seams to be a managed assembly with .NET.
  2. A static function inside a class cannot be referenced as a function with extern "C" linkage.
  3. Libs are included into a project in defining them as an input file, or you add them to the linker options.

If you want to create a static C/C++ library (.lib) without a DLL.

  1. Launch the project Wizard
  2. Select Win32 projects
  3. In the Wizard select "Static library"

With such a static lib, you just need a header file and the lib to include it into another project.

There is no when you use a static lib.

PS: You should create a static libs for Debug and Release mode. Also you need to take care about the switches that control how the CRT is used inside the Lib. This Compiler switches should be identical in the lib and in the project were you use the lib.