I build shared library with cmake by:
cmake -B build_v140_x64 -G "Visual Studio 14 2015" -A x64 -DYAML_BUILD_SHARED_LIBS=on -DYAML_CPP_BUILD_TESTS=off
cmake --build build_v140_x64 --config Release
The demo yaml file is:
diagnoser:
trade-gw:
enable: false
test1: 10
I link the yaml-cpp.dll by:
find_library(
YAML_CPP_LIB
NAMES yaml-cpp.lib
PATHS ${THIRD_PARTY_LIB_PATH} NO_DEFAULT_PATH)
target_link_libraries(test_yaml_cpp PRIVATE ${YAML_CPP_LIB})
test code:
int main() {
try {
fs::path yamlPath = "C:\\my_proj\\cpp_test\\src\\test_yaml_cpp\\checker.yaml";
if (!fs::exists(yamlPath)) {
return -1;
}
YAML::Node root = YAML::LoadFile(yamlPath.string());
std::cout << "root size:" << root.size() << std::endl;
auto diag_node = root["diagnoser"];
if (diag_node) {
std::cout << diag_node["trade-gw"]["enable"].as<bool>() << std::endl;
}
std::cout << root["test1"].as<int>() << std::endl;
} catch (const YAML::ParserException& e) {
std::cout << "ParserException parse config file failed:" << e.msg << std::endl;
return -1;
} catch (const YAML::BadFile& e) {
std::cout << "BadFile parse config file failed:" << e.msg << std::endl;
return -2;
} catch (const std::exception& e) {
std::cout << "parse config file failed:" << e.what() << std::endl;
return -3;
}
return 0;
}
output:
parse config file failed:bad allocation
yaml-cpp version:yaml-cpp-0.7.0 os version:windows 11 22H2