Windows新手求助:yaml-cpp源码生成DLL及使用方法(VS2015)
Hey there! I totally get how frustrating it is when you've nailed building yaml-cpp on Ubuntu but hit a brick wall on Windows—especially with Visual Studio 2015, which has its own set of quirks. Let's walk through this step by step to get a working DLL built and integrated into your project, no missing pieces this time.
Grab the yaml-cpp source code (version 0.6.3 is recommended for better compatibility with VS 2015) and extract it to a folder on your Windows machine—say, C:\yaml-cpp-src. This version plays nicely with older VS versions without requiring too many modern C++ features that might cause issues.
yaml-cpp uses CMake to generate project files, so you'll need a compatible version of CMake (3.17.x or newer works well with VS 2015). Here's how to set it up:
- Open the CMake GUI.
- In the "Where is the source code" field, browse to your extracted yaml-cpp folder (
C:\yaml-cpp-src). - In the "Where to build the binaries" field, create a new folder (e.g.,
C:\yaml-cpp-build)—this keeps your source code clean from build artifacts. - Click "Configure". When prompted, select Visual Studio 14 2015 as the generator, choose either "Win32" or "x64" depending on your project's architecture, then click "Finish".
- Wait for the initial configuration to complete. Then, find these variables in the list and tweak them:
- Set
BUILD_SHARED_LIBSto ON—this tells CMake to build a DLL instead of a static library. - Set
YAML_CPP_BUILD_TESTSto OFF (optional, but saves build time by skipping the test suite).
- Set
- Click "Configure" again to apply these changes, then click "Generate" to create the Visual Studio solution files.
Now let's turn those generated files into a working DLL:
- Navigate to your build folder (
C:\yaml-cpp-build) and openyaml-cpp.slnwith VS 2015. - In the top menu bar, set the build configuration to Release (or "Debug" if you need debug symbols—just remember to match this with your project later).
- Right-click the
yaml-cppproject in the Solution Explorer and select "Build". - Once the build finishes, you'll find your files in the build output folder:
- Release:
C:\yaml-cpp-build\Release\yaml-cpp.dll(the actual DLL) andyaml-cpp.lib(the import library your project will link against) - Debug:
C:\yaml-cpp-build\Debug\yaml-cppd.dllandyaml-cppd.lib(note the 'd' suffix for debug builds)
- Release:
Now let's set up your project to use yaml-cpp:
- Organize your files (optional but keeps things tidy):
- Create a folder like
C:\yaml-cppwith two subfolders:includeandlib. - Copy the entire
yaml-cppfolder fromC:\yaml-cpp-src\includeintoC:\yaml-cpp\include—this contains all the header files you'll need. - Copy the appropriate import library (
yaml-cpp.libfor Release,yaml-cppd.libfor Debug) intoC:\yaml-cpp\lib. - Copy the corresponding DLL (
yaml-cpp.dlloryaml-cppd.dll) to your project's output directory (where your .exe gets built) or add its folder to your system PATH—this ensures Windows can find it when your program runs.
- Create a folder like
- Configure your VS project settings:
- Right-click your project in the Solution Explorer and select "Properties".
- Under "Configuration Properties":
- C/C++ > General: Add
C:\yaml-cpp\includeto the "Additional Include Directories" field. - Linker > General: Add
C:\yaml-cpp\libto the "Additional Library Directories" field. - Linker > Input: Add
yaml-cpp.lib(Release) oryaml-cppd.lib(Debug) to the "Additional Dependencies" field.
- C/C++ > General: Add
- Make sure to apply these settings to both "Release" and "Debug" configurations if you use both.
- Test with sample code:
Add this simple snippet to your project to verify everything works:
Build and run your project—if it prints out the name and age correctly, you're good to go!#include <iostream> #include "yaml-cpp/yaml.h" int main() { YAML::Node config = YAML::Load("{name: John, age: 30}"); std::cout << "Name: " << config["name"].as<std::string>() << std::endl; std::cout << "Age: " << config["age"].as<int>() << std::endl; return 0; }
- If you get linker errors, double-check that your project's architecture (x86/x64) matches the one you used to build the DLL.
- If your program crashes or says it can't find the DLL at runtime, make sure the DLL file is in the same folder as your executable or in a directory listed in your system PATH.
- Ensure VS 2015 is updated to Service Pack 1—this fixes a lot of compatibility issues with CMake-generated projects.
内容的提问来源于stack exchange,提问作者Izik

