visual stdio使用spdlog教程

这个文档的初衷是我想使用spdlog这个开源库,发现需要cmake编译成库文件使用,我就记录一下visual studio 下的配置过程,并写了测试程序,将代码会传送到GitHub方便后续自己查看学习。也方便读者进行阅读。

  • spdlog项目地址

https://github.com/gabime/spdlog

  • 本文档
  1. 将开源项目编译成库文件
  2. 加载库文件使用开源项目

开始干活

visual studio and cmake

第一步:下代码

git clone https://github.com/gabime/spdlog.git

image-20211020221315426

我自己fork了一份,所以我本地使用我自己仓库的代码进行开发,大家使用作者仓库的就好。

本地命令流程:

$ git clone git@github.com:sexjun/spdlog.git
Cloning into 'spdlog'...
remote: Enumerating objects: 23116, done.
remote: Counting objects: 100% (1493/1493), done.
remote: Compressing objects: 100% (583/583), done.
remote: Total 23116 (delta 879), reused 1206 (delta 754), pack-reused 21623
Receiving objects: 100% (23116/23116), 40.21 MiB | 425.00 KiB/s, done.
Resolving deltas: 100% (15179/15179), done.

第二步:cmake生成vs工程

等待将代码下载下来后,进入代码我们可以看到cmake 的脚本文件:CMakeLists.txt

我们建立一个build文件夹,在这个文件夹里进行编译操作。

image-20211020221735766

第三步:vs编译

编译完成后就会生成一个:vs的工程。我们使用vs打开这个工程。

image-20211020221816877

打开这个工程后我们可以看到:总共有6个工程。

image-20211020221901337

首先我们第一步是:全部生成

  1. 选中ALL_BUILD->鼠标右键->生成

    image-20211020222009278

  2. 我们可以在输出窗口看到:下图代表成功

    image-20211020222032408

第四步:vs安装

然后我们需要安装我们刚刚编译的东西,则执行:

image-20211020222429050

看到输出:我们就可以看到安装到哪里了。当然默认是:debug 64位的,需要其他版本修改这里后,重复执行上述步骤即可。

image-20211020222509496

通过输出窗口的输出:我们可以看到安装路径在:

image-20211020223604543

头文件跟库文件都在:

image-20211020223628663

1>-- Install configuration: "Debug"
1>-- Installing: C:/Program Files (x86)/spdlog/include
1>-- Installing: C:/Program Files (x86)/spdlog/lib/pkgconfig/spdlog.pc
1>-- Installing: C:/Program Files (x86)/spdlog/lib/cmake/spdlog/spdlogConfigTargets.cmake
1>-- Installing: C:/Program Files (x86)/spdlog/lib/cmake/spdlog/spdlogConfigTargets-debug.cmake
1>-- Installing: C:/Program Files (x86)/spdlog/lib/cmake/spdlog/spdlogConfig.cmake
1>-- Installing: C:/Program Files (x86)/spdlog/lib/cmake/spdlog/spdlogConfigVersion.cmake

这里安装失败的话,则需要使用管理员权限打开vs,方法为:

可以 win+q 搜索: devenv.exe进行下面操作

  1. 打开VS的安装目录,找到devenv.exe,右键,选择“兼容性疑难解答”
  2. 选择“疑难解答程序”
  3. 选择“该程序需要附加权限”
  4. 确认用户帐户控制后,点击测试程序
  5. 回到兼容性疑难解答对话框,点击下一步,然后选择“是,为此程序保存这些设置”

第五步:写个代码测试一下

现在我们就可以测试一下刚才的库文件了。

测试方法:

  1. 创建一个vs工程

image-20211020222729726

  1. 开始测试

配置头文件路径:

配置头文件路径和配置库文件路径都不是必选的,因为windows默认的安装路径就是环境变量的位置。

但是配置的好处就是不会有代码标红报错。

C:/Program Files (x86)/spdlog/include/

image-20211020222952661

image-20211020223048585

配置库文件路径:

这一步一般是可以忽略的,因为从上面安装我们可以获取以下路径为安装

C:/Program Files (x86)/spdlog/lib/

image-20211020223122583

  1. 执行测试

测试代码:

// spdlog_vs_pjx.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "spdlog/spdlog.h"
#include "spdlog/cfg/env.h"  // support for loading levels from the environment variable
#include "spdlog/fmt/ostr.h" // support for user defined types
#include "spdlog/cfg/env.h"

int main()
{
    // Set the log level to "info" and mylogger to "trace":
    // SPDLOG_LEVEL=info,mylogger=trace && ./example
    spdlog::cfg::load_env_levels();
    // or from command line:
    // ./example SPDLOG_LEVEL=info,mylogger=trace
    // #include "spdlog/cfg/argv.h" // for loading levels from argv
    // spdlog::cfg::load_argv_levels(args, argv);
    std::cout << "Hello World!\n";
    spdlog::info("Welcome to spdlog version {}.{}.{}  !", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);

    spdlog::warn("Easy padding in numbers like {:08d}", 12);
    spdlog::critical("Support for int: {0:d};  hex: {0:x};  oct: {0:o}; bin: {0:b}", 42);
    spdlog::info("Support for floats {:03.2f}", 1.23456);
    spdlog::info("Positional args are {1} {0}..", "too", "supported");
    spdlog::info("{:>8} aligned, {:<8} aligned", "right", "left");

    // Runtime log levels
    spdlog::set_level(spdlog::level::info); // Set global log level to info
    spdlog::debug("This message should not be displayed!");
    spdlog::set_level(spdlog::level::trace); // Set specific logger's log level
    spdlog::debug("This message should be displayed..");

    // Customize msg format for all loggers
    spdlog::set_pattern("[%H:%M:%S %z] [%^%L%$] [thread %t] %v");
    spdlog::info("This an info message with custom format");
    spdlog::set_pattern("%+"); // back to default format
    spdlog::set_level(spdlog::level::info);
    return 0;
}


测试执行结果:

image-20211020223345453

第六步:上传到GitHub

上传的网页网址为:https://github.com/sexjun/spdlog

获取代码的方式:git@github.com:sexjun/spdlog.git

  • 9
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
Visual Studio Code 是一款轻量级的代码编辑器,适用于多种编程语言。以下是 Visual Studio Code 的使用教程: 1. 下载和安装 Visual Studio Code:在官网上下载适合自己操作系统的版本,然后按照提示进行安装。 2. 打开 Visual Studio Code:安装完成后,打开 Visual Studio Code,你会看到一个简洁的编辑器界面。 3. 创建新文件:点击左侧的“文件夹”图标,选择一个文件夹作为工作区,然后点击“新建文件”按钮,输入文件名和后缀名,即可创建新文件。 4. 编写代码:在新建的文件中,输入代码即可。Visual Studio Code 支持多种编程语言,可以根据需要选择相应的语言。 5. 调试代码:Visual Studio Code 内置了调试功能,可以帮助你找出代码中的错误。在编辑器中打开要调试的文件,然后点击左侧的“调试”图标,选择“添加配置”按钮,选择相应的调试器,然后按照提示进行配置即可。 6. 安装插件:Visual Studio Code 支持插件扩展,可以通过安装插件来增强编辑器的功能。点击左侧的“扩展”图标,搜索需要的插件,然后点击“安装”按钮即可。 7. 使用快捷键:Visual Studio Code 内置了很多快捷键,可以帮助你更快地完成操作。可以在编辑器中点击“文件”菜单,选择“首选项”-“键盘快捷方式”来查看和修改快捷键。 以上就是 Visual Studio Code 的使用教程,希望对你有所帮助。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早睡的叶子

你的鼓励就是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值