Windows 10及Visual Studio 2015下安裝libconfig
发布日期:2021-05-06 19:48:20 浏览次数:19 分类:技术文章

本文共 3548 字,大约阅读时间需要 11 分钟。

Windows 10及Visual Studio 2015下安裝libconfig

前言

筆者一開始打算在Windows 10下安裝,並搭配Visual Studio 2015使用。但是因為官方的教程在這方面並沒有太多著墨,筆者自己踩了坑後(詳見),決定轉而使用vcpkg來安裝。

安裝vcpkg

vcpkg是由微軟開發,用於在Windows, Mac及Linux下管理C/C++套件的工具。

vcpkg的安裝十分簡單,其步驟都寫在官方的Readme裡了。

官方建議的安裝路徑為C:\src\vcpkgC:\dev\vcpkg,在此筆者選擇的是C:\src\vcpkg

首先到C:\src(要自己創建這個目錄):

cd C:\src

然後下載vcpkg

git clone https://github.com/microsoft/vcpkg

接著進入該專案,執行bootstrap-vcpkg.bat

cd vcpkgbootstrap-vcpkg.bat

安裝完成後試著下vcpkg這個指令,如果成功的話會打印出一連串的help message。

安裝libconfig

要用vcpkg安裝套件也很簡單,只要在vcpkg install後面加上套件名稱即可:

vcpkg install libconfig

如上,但是有一點要注意的是,它默認安裝的版本是在x86系統上使用的libconfig:x86-windows,如果想要在x64系統上使用,則應使用以下指令:

vcpkg install libconfig:x64-windows

在Visual Studio 2015中使用

設定Properties

Solution Explorer中,先點選要配置的專案,然後選取Properties

Properties
左側選取VC++ Directories,然後配置相關路徑。
Executable Directories中加上C:\src\vcpkg\installed\x64-windows\bin
Include Directories中加上C:\src\vcpkg\installed\x64-windows\include
Library Directories中加上C:\src\vcpkg\installed\x64-windows\lib
VC++ Directories
接著選取左側Linker->General,在Additional Library Directories處加上C:\src\vcpkg\installed\x64-windows\lib
linker general
然後點選Linker->Input,在Additional Dependencies處加上libconfig++.lib
linker input

dll檔

在link時會需要用到libconfig.dlllibconfig++.dll這兩個檔案,要把它們從C:\src\vcpkg\installed\x64-windows\bin複製到<Visual_Studio_Project_Directory>\x64\Release下面。

至此libconfig已經配置完成,可以來跑跑看簡單的範例程式了。

範例程式

以下是一個簡單的C++範例程式,用於從config.txt中讀取鍵值對。

其中要注意的是,對於C++,應該include libconfig.h++;如果是用C語言,則應include libconfig.h

#include 
#include
#include
int main(int argc, char** argv) {
libconfig::Config cfg; cfg.readFile("config.txt"); std::cout << "==============================" << std::endl; std::cout << "configs: " << std::endl; constlibconfig::Setting& root = cfg.getRoot(); for (libconfig::SettingConstIterator it = root.begin(); it != root.end(); ++it) {
switch (it->getType()) {
case libconfig::Setting::TypeInt: {
int val = root.lookup(it->getName()); std::cout << std::setw(25) << it->getName() << std::setw(20) << " (TypeInt) : " << val << std::endl; break; } case libconfig::Setting::TypeInt64: {
long long val = root.lookup(it->getName()); std::cout << std::setw(25) << it->getName() << std::setw(20) << " (TypeInt64) : " << val << std::endl; break; } case libconfig::Setting::TypeFloat: {
float val = root.lookup(it->getName()); std::cout << std::setw(25) << it->getName() << std::setw(20) << " (TypeFloat) : " << val << std::endl; break; } case libconfig::Setting::TypeString: {
std::string val = root.lookup(it->getName()); std::cout << std::setw(25) << it->getName() << std::setw(20) << " (TypeString) : " << val << std::endl; break; } case libconfig::Setting::TypeBoolean: {
bool val = root.lookup(it->getName()); std::cout << std::setw(25) << it->getName() << std::setw(20) << " (TypeBoolean) : " << (val ? "true" : "false") << std::endl; break; } case libconfig::Setting::TypeGroup: case libconfig::Setting::TypeArray: case libconfig::Setting::TypeList: std::cout << std::setw(25) << it->getName() << " (TypeGroup, TypeArray or TypeList)" << std::endl; break; default: std::cout << std::setw(25) << it->getName() << " (None)" << std::endl; } }}

以下是config.txt

mybool = true;myint = 123;myfloat = 3.14;mystring = "abc";

其執行結果為:

==============================configs:                   mybool    (TypeBoolean) : true                    myint        (TypeInt) : 123                  myfloat      (TypeFloat) : 3.14                 mystring     (TypeString) : abc==============================

參考連結

上一篇:PCL MLS論文Computing and Rendering Point Set Surfaces研讀筆記
下一篇:c++11 std::unique_ptr error cmake 3.11.3 bootstrap

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2025年04月04日 20时42分13秒