You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:基于MFC的C++类生成可用库(Visual Studio 2015)

Hey there! Let's get your MFC-dependent C++ class packaged into a usable DLL so other apps can call it. I’ve been through this exact scenario with VS2015 before, so here’s a step-by-step breakdown that should work for you:

Step 1: Create the Correct MFC DLL Project
  • Open Visual Studio 2015, navigate to File > New > Project
  • In the project templates, go to Visual C++ > MFC > MFC DLL
  • Name your project (e.g., MyMFCLibrary), pick a save location, then click OK
  • In the MFC DLL Wizard:
    • Select Regular DLL using shared MFC DLL (this is the most flexible choice—it keeps your library lightweight by linking against MFC runtime DLLs)
    • Ensure "Export symbols" is checked (this auto-sets up the macros needed to expose your class/functions to other apps)
    • Click Finish to generate the project
Step 2: Integrate Your MFC-Dependent Class
  • Right-click your project in Solution Explorer, choose Add > Existing Item and select your class's .h and .cpp files
  • To make your class accessible to external apps:
    • In your class header, wrap the class declaration with the auto-generated export macro (it’ll be named something like MYMFCLIBRARY_API, matching your project name). Example:
      class MYMFCLIBRARY_API CMyMFCClass : public CSomeMFCBaseClass
      {
          // Your class methods and members here
      };
      
    • If your class uses MFC types in its public interface (like CString, CWnd*), make sure the header includes the required MFC headers (e.g., #include <afxwin.h>, #include <afxstr.h>)
Step 3: Fine-Tune Project Settings (Critical Fixes)

Default settings can sometimes cause hiccups—double-check these:

  • Right-click your project > Properties
    • Under Configuration Properties > General:
      • Set Platform Toolset to v140 (VS2015’s default toolset)
      • Set Character Set to Use Unicode Character Set (match what your original class uses; most MFC projects rely on this)
    • Under Configuration Properties > C/C++ > General:
      • Add paths to any extra headers your class needs in Additional Include Directories
    • Under Configuration Properties > Linker > Input:
      • If you chose shared MFC linkage, MFC libraries should be linked automatically—no extra work needed here
Step 4: Test the DLL with a Calling App

To confirm everything works, build a quick test project:

  • Create a new Win32 Project or MFC Application
  • Add your class’s header to the test project
  • In the test project’s properties:
    • Under Configuration Properties > C/C++ > General, add the path to your DLL’s header files
    • Under Configuration Properties > Linker > General, add the path to your DLL’s output folder (where the .lib file lives)
    • Under Configuration Properties > Linker > Input, add your DLL’s .lib file to Additional Dependencies
  • Copy the built .dll from your library’s output folder to the test app’s executable folder (or set the test app’s working directory to the DLL’s output folder)
  • Now you can instantiate your MFC class in the test app and call its methods to verify functionality
Troubleshooting Common Issues
  • Linker errors about missing MFC symbols: Make sure your library and calling app use the same MFC linkage (shared vs static). If your library uses shared MFC, the calling app must also link against shared MFC (set in Properties > Configuration Properties > General > Use of MFC).
  • Compile errors about undefined macros: Ensure your class header includes the project’s stdafx.h (where the export macro is defined) before declaring your class.
  • Calling app can’t find the DLL: Double-check the DLL is in the same folder as the test app’s .exe, or in a directory listed in the system PATH.

内容的提问来源于stack exchange,提问作者overflowBeppe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:24:41