1、版本选择
建议使用yaml-cpp-0.6.0这个版本。它有以下特点:
1)只需要c++11
2)去掉对boost等其他的依赖,干净
下载链接:https://github.com/jbeder/yaml-cpp/releases/tag/yaml-cpp-0.6.0
2、编译和安装
将yaml-cpp装在指定目录下,工程下有cmakelists.txt文件,新建build,然后cmake
在x86平台:
cmake .. -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=/gzy_mnt/YAML_X86
而使用交叉编译工具进行编译arm链接库
cmake .. -DBUILD_SHARED_LIBS=ON \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_SYSTEM_NAME=Linux \
-DCMAKE_SYSTEM_VERSION=1 \
-DCMAKE_SYSTEM_PROCESSOR=aarch64 \
-DCMAKE_C_COMPILER=/gzy_mnt/aarch64-linux-gds/bin/aarch64-linux-gnu-gcc \
-DCMAKE_CXX_COMPILER=/gzy_mnt/aarch64-linux-gds/bin/aarch64-linux-gnu-g++ \
-DCMAKE_INSTALL_PREFIX=/gzy_mnt/YAML_ARM
再make
make -j8
再安装
make install
这样将yaml-cpp安装在指定环境下
3、demo使用
1)config.yaml配置如何
custom_db:
db_domain: 10.0.0.8
db_username: root
db_passwd: my_passwd
db_schema: test
redis:
redis_domain: 10.0.0.10
redis_passwd: 123456
hello:
num_config: [1141studio]
name_config: [powered, by, 1141studio]
2)config_test.cpp
#include <iostream>
#include <string>
#include <stdio.h>
#include <unistd.h>
#include "yaml-cpp/yaml.h"
#include <vector>
const std::string DB_CONF = "config.yaml";
int main(int argc, char * argv[]) {
/*----------------------------------- test yaml ----------------------------------------*/
printf("hello world\n");
std::cout << "this code is only for test yaml" << std::endl;
/* Node conf. */
YAML::Node conf = YAML::LoadFile(DB_CONF);
/*----------------------------------- display db_config ----------------------------------------*/
std::cout << "Database:"<< std::endl;
std::cout << "domain: " << conf["custom_db"]["db_domain"].as<std::string>() << std::endl;
std::cout << "username:" << conf["custom_db"]["db_username"].as<std::string>() << std::endl;
std::cout << "passwd: " << conf["custom_db"]["db_passwd"].as<std::string>() << std::endl;
std::cout << "schema: " << conf["custom_db"]["db_schema"].as<std::string>() << std::endl;
/*----------------------------------- display redis ----------------------------------------*/
std::cout << "Redis" << std::endl;
std::cout << "redis_domain: " << conf["redis"]["redis_domain"].as<std::string>() << std::endl;
std::cout << "redis_passwd: " << conf["redis"]["redis_passwd"].as<std::string>() << std::endl;
/*----------------------------------- display hello ----------------------------------------*/
std::cout << "HelloServer" << std::endl;
/* vector of name string. */
std::vector<std::string> name_vec = conf["hello"]["num_config"].as<std::vector<std::string> >();
if(!name_vec.empty())
std::cout << name_vec[0] << std::endl;
return 0;
}
3)CMakeLists.txt配置情况如下
cmake_minimum_required(VERSION 3.5.1)
project(nanodet-mnn)
SET(CMAKE_BUILD_TYPE "Release")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/)
# yaml-cpp的包含目录
include_directories("/gzy_mnt/YAML_X86/include")
file(GLOB SOURCE_FILES *.cpp)
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries (
${CMAKE_PROJECT_NAME}
pthread
/gzy_mnt/YAML_X86/lib/libyaml-cpp.so
)