0

I have a static library file (.lib) called SampleArithmetic.lib and the header file for it SampleArithmetic.h. Now using these files, I want to generate a DLL files for the functions present in the SampleArithmetic.lib.

What I did:

  • Created a new project in VS2022 of type Class Library

  • Configured include folder path (to access SampleArithmetic.h)

  • Configured lib path and the lib required (i.e. SampleArithmetic.lib)

  • and finally, added the code below in a .cpp file called SampleArithmeticDLLGenerator.cpp

      #include "SampleArithmetic.h"
      #include "pch.h"
    
      extern "C" {
          __declspec(dllexport) int add(int a, int b)
          {
              SampleArithmetic obj;
              return obj.Add(a, b);
          }
    
          __declspec(dllexport) int subtract(int a, int b)
          {
              SampleArithmetic obj;
              return obj.Subtract(a, b);
          }
    
          __declspec(dllexport) int multiply(int a, int b)
          {
              SampleArithmetic obj;
              return obj.Multiply(a, b);
          }
      }
    

PROBLEM:

I am getting the following errors:

Severity Code Description Project File Line Suppression State Error C2065 'SampleArithmetic': undeclared identifier CPlusPlusDLLWrapper C:\Users\Downloads\TestProject\TestCppLibIntegrationInCSharp\CPlusPlusDLLWrapper\SampleArithmeticDLLGenerator.cpp 7

Severity Code Description Project File Line Suppression State Error C2065 'obj': undeclared identifier CPlusPlusDLLWrapper C:\Users\Downloads\TestProject\TestCppLibIntegrationInCSharp\CPlusPlusDLLWrapper\SampleArithmeticDLLGenerator.cpp 7

CristiFati
  • 38,250
  • 9
  • 50
  • 87
skm
  • 5,015
  • 8
  • 43
  • 104
  • 3
    Not exactly matching the error messages, but anything included *before* `pch.h` will be ignored by the compiler. (It assumes that part to be precompiled too). – BoP May 05 '23 at 11:01
  • @BoP: And that was exactly the issue. It resolved the problem. Thanks. – skm May 05 '23 at 11:05
  • Does this answer your question? [What is "pch.h" and why is it needed to be included as the first header file?](https://stackoverflow.com/questions/54121917/what-is-pch-h-and-why-is-it-needed-to-be-included-as-the-first-header-file) – CristiFati May 06 '23 at 07:52
  • @BoP Consider converting the comment in to an answer. – Minxin Yu - MSFT May 08 '23 at 05:32

0 Answers0