将 C++ 静态库 (.lib) 函数包装成 DLL - dllexport

问题描述 投票:0回答:0

我有一个名为

SampleArithmetic.lib
的静态库文件(.lib)和它的头文件
SampleArithmetic.h
。现在使用这些文件,我想为
SampleArithmetic.lib
.

中存在的函数生成一个 DLL 文件

我做了什么:

  • 在 VS2022 中创建了一个类型为

    Class Library

    的新项目
  • 配置的包含文件夹路径(访问

    SampleArithmetic.h

  • 配置的库路径和所需的库(即

    SampleArithmetic.lib

  • 最后,将下面的代码添加到名为

    SampleArithmeticDLLGenerator.cpp

    的 .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);
          }
      }
    

问题:

我收到以下错误:

严重性代码描述项目文件行抑制状态 错误 C2065“SampleArithmetic”:未声明的标识符 CPlusPlusDLLWrapper C:\Users\Downloads\TestProject\TestCppLibIntegrationInCSharp\CPlusDLLWrapper\SampleArithmeticDLLGenerator.cpp 7

严重性代码描述项目文件行抑制状态 错误 C2065“obj”:未声明的标识符 CPlusPlusDLLWrapper C:\Users\Downloads\TestProject\TestCppLibIntegrationInCSharp\CPlusDLLWrapper\SampleArithmeticDLLGenerator.cpp 7

c++ visual-studio dll dllexport
© www.soinside.com 2019 - 2024. All rights reserved.