ubuntu16.04yaml-cpp의 설치 및 사용

3063 단어
프로젝트에 시스템 파일을 설정해야 하기 때문에yaml을 사용해야 한다고 생각했습니다.그러나yaml-cpp의 설치와 사용 과정에서 몇 가지 문제가 발생하여 이틀 동안 조정하여 마침내 통하게 되었다.경험을 기록해 두고, 필요한 사람이 좀 굽은 길을 걷지 않기를 바란다.
1. 설치:
갱1: 반드시 0.5.2버전을 필요로 합니다. 제가 전에 다운로드한 0.5.3버전은 핵심 부분의 오류를 보고합니다.
공식git원https://github.com/jbeder/yaml-cpp/releases/tag/release-0.5.2tar. 다운로드gz의 원본 패키지는 다음 절차를 따릅니다.
cd yaml-cpp #         

mkdir build

cd build

cmake -DBUILD_SHARED_LIBS=ON ..

make

sudo make install

갱2:make 완료 시 반드시make install, 자신의cp를 원한다면/usr/local/lib와/usr/local/include 폴더 아래에 있어야 합니다./usr/lib와/usr/include 폴더가 아니라/usr/include 폴더에 있어야 합니다. 그렇지 않으면 라이브러리를 찾을 수 없고yaml을 찾을 수 없습니다.h 파일.
2. 사용:
yaml-cpp 5.0 버전 이후 사용 변경
버전 5.0 이전:

	1.  YAML::Parser parser(fin);
	2.  YAML::Node doc;
	3.  parser.GetNextDocument(doc);


버전 5.0 이후 GetNextDocument 함수가 없습니다.사용하다

	1.  YAML::Node doc = YAML::Load(fin);

 3.테스트 코드:
1.CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
project(test_yaml)


find_package(Boost REQUIRED COMPONENTS system)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

find_package(PkgConfig)
pkg_check_modules(NEW_YAMLCPP yaml-cpp>=0.5)
if(NEW_YAMLCPP_FOUND)
add_definitions(-DHAVE_NEW_YAMLCPP)
endif(NEW_YAMLCPP_FOUND)


link_directories(
    /usr/lib/
    )

include_directories(
    include
    yaml-cpp
)

add_executable(test ./src/main.cpp)
target_link_libraries(test ${catkin_LIBRARIES} yaml-cpp)

 
//main.cpp  
#include 
#include 
#include 
#include "yaml-cpp/yaml.h"

using namespace std;

//   yaml-cpp 0.5      ">>",            
//     ,     ">>"   
template
void operator >> (const YAML::Node& node, T& i)
{
  i = node.as();
}

void configure(const YAML::Node& node);
void nodePrint(const YAML::Node& node);

int main()
{
  YAML::Node config = YAML::LoadFile("../src/test.yaml");

  configure(config["subscribers"]);

  return 0;
}

void configure(const YAML::Node& node)
{
  for (unsigned int i = 0; i < node.size(); i++)
  {
    nodePrint(node[i]);
  }
}

void nodePrint(const YAML::Node& node)
{
  string name;
  string topic;
  double timeout;
  unsigned int priority;

  node["name"]       >> name;
  node["topic"]      >> topic;
  node["timeout"]    >> timeout;
  node["priority"]   >> priority;

  cout<

3.test.yaml
 
# yaml  
 
subscribers:
  - name:        "Default task"
    topic:       "input/cmd_default_check"
    timeout:     0.5
    priority:    1
    short_desc:  "Default controller"
  - name:        "Navigation stack"
    topic:       "input/cmd_serial_navi"
    timeout:     1.0
    priority:    3
    short_desc:  "Navigation controller"
publisher:       "output/cmd_vel"

좋은 웹페이지 즐겨찾기