求助:基于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 Itemand select your class's.hand.cppfiles - 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>)
- In your class header, wrap the class declaration with the auto-generated export macro (it’ll be named something like
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 Toolsettov140(VS2015’s default toolset) - Set
Character Setto Use Unicode Character Set (match what your original class uses; most MFC projects rely on this)
- Set
- Under
Configuration Properties > C/C++ > General:- Add paths to any extra headers your class needs in
Additional Include Directories
- Add paths to any extra headers your class needs in
- Under
Configuration Properties > Linker > Input:- If you chose shared MFC linkage, MFC libraries should be linked automatically—no extra work needed here
- Under
Step 4: Test the DLL with a Calling App
To confirm everything works, build a quick test project:
- Create a new
Win32 ProjectorMFC 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.libfile lives) - Under
Configuration Properties > Linker > Input, add your DLL’s.libfile toAdditional Dependencies
- Under
- Copy the built
.dllfrom 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
相关产品推荐
相关产品推荐

