Xcode + yaml-cpp 開發配置

下載安裝:  yams-cpp release 0.5.3, 支持 yaml 1.2標準, 比老版本語法更優雅
 
解壓至yaml-cpp-release-0.5.3
cd yaml-cpp-release-0.5.3
mkdir build
cd build
cmake ..
make 
make install 
確認include文件安裝到/usr/local/include中,文件夾爲 yaml-cpp  
建立測試文件:
main.cpp內容以下 
#include <iostream>
#include <fstream>
#include <yaml-cpp/yaml.h>
#include <time.h>

int main(int argc, const char * argv[]) {
    // insert code here...
    YAML::Node config = YAML::LoadFile("config.yaml");

    const std::string username = config["username"].as<std::string>();
    const std::string password = config["password"].as<std::string>();

    time_t ltime;
    time(&ltime);
    config["lastLogin"] = ctime(&ltime);

    std::ofstream fout("config.yaml");
    fout << config;


    std::cout << "username: " << username <<" password " << password << std::endl;
    return 0;
}

在main.cpp目錄下建立 config.yaml, 內容爲:
username: user1
password: pass1 
 
 g++ main.cpp -I/usr/local/include -L/usr/local/lib -lyaml-cpp -o test
 ./test

執行成功後, 查看 config.yaml,的內容爲
lastLogin: "Wed Apr 27 16:48:33 2016\n"
username: user1
password: pass1
 

配置使用 Xcode , 使用C++進行yaml-cpp開發除要引入<yaml-cpp/yaml.h>文件外,還須要配置xcode項目配置文件:
 
在Xcode的build setting中
Linking     
     Other linker Flags 中添加 -lyaml-cpp 不然出如下錯誤
 
"YAML::LoadFile(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

 

參考:
http://www.xuebuyuan.com/866409.html
https://github.com/jbeder/yaml-cpp/wiki/Tutorial
http://stackoverflow.com/questions/2202179/problem-using-yaml-cpp-on-os-x
相關文章
相關標籤/搜索