前面的cJSON库和环形缓冲库源文件数目都只有几个,所以我们使用的时候都是直接把这几个源文件放到我们的项目中使用,这次的zlog的源文件有四十多个,如果也和前面一样把源文件加入到项目中,会使得项目源文件管理起来复杂,因此我们需要将这些源文件编译成库文件。在我们的项目中使用库文件和相应的头文件即可。

但是因为这个库中的源文件会依赖很多linux上的库函数,因此,想要直接把文件加入到Clion项目中直接编译是不可能的,因此我们需要在linux上将该开源库编译成库文件,我们在CLion上直接使用库文件和头文件即可。

从这里我们也可以看出,在Windows上编译开源库,开发项目还是没有在linux上方便,linux上的编译环境配置起来还是很容易的,在Windows上就会经常遇到头文件找不到的错误,这也是很正常的,因为很多开源库的实现是要依赖操作系统的,因此有时候我们必须要在linux环境下进行构建。

zlog的GitHub地址:zlog

zlog的中文帮助文档:zlog使用手册

zlog博客:高效c/c++日志工具zlog使用介绍、zlog的安装与使用

如果没有linux编译环境,可以看这个GitHub:WinZlog,已经编译好了lib和dll文件。


我的这篇博文说明了怎么使用vscode链接linux服务器进行开发Vscode远程连接linux服务器(阿里云服务器)

下面我们就演示一下在linux服务器下编译zlog开源库,生成我们需要的库文件。

首先使用vscode远程连接上我的阿里云服务器。

下面我们文件的更改和命令行都可以在vscode中进行,比较方便。


先进入zlog目录,执行make

root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog# make
cd src && make all
make[1]: Entering directory '/code/zlog/src'
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb buf.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb category.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb category_table.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb conf.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb event.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb format.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb level.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb level_list.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb mdc.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb record.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb record_table.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb rotater.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb rule.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb spec.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb thread.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zc_arraylist.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zc_hashtable.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zc_profile.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zc_util.c
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zlog.c
cc -shared -Wl,-soname,libzlog.so.1.2 -o libzlog.so  buf.o category.o category_table.o conf.o event.o format.o level.o level_list.o mdc.o record.o record_table.o rotater.o rule.o spec.o thread.o zc_arraylist.o zc_hashtable.o zc_profile.o zc_util.o zlog.o  -pthread
# for use in test folder - linux and requirement for aix runtime
# resolving
cp -f libzlog.so libzlog.so.1
cp -f libzlog.so libzlog.so.1.2
cc -std=c99 -pedantic -c -O2 -fPIC -pthread  -Wall -Wstrict-prototypes -fwrapv -g -ggdb zlog-chk-conf.c
ar rcs libzlog.a buf.o category.o category_table.o conf.o event.o format.o level.o level_list.o mdc.o record.o record_table.o rotater.o rule.o spec.o thread.o zc_arraylist.o zc_hashtable.o zc_profile.o zc_util.o zlog.o
cc -o zlog-chk-conf zlog-chk-conf.o -L. -lzlog  -pthread
make[1]: Leaving directory '/code/zlog/src'
root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog#

看上面执行的命令,主要是生成了libzlog.so和libzlog.a。

继续执行make install

root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog# make install
cd src && make install
make[1]: Entering directory '/code/zlog/src'
mkdir -p /usr/local/include /usr/local/lib /usr/local/bin
cp -a zlog.h /usr/local/include
cp -a zlog-chk-conf /usr/local/bin
cp -a libzlog.so /usr/local/lib/libzlog.so.1.2
cd /usr/local/lib && ln -sf libzlog.so.1.2 libzlog.so.1
cd /usr/local/lib && ln -sf libzlog.so.1 libzlog.so
cp -a libzlog.a /usr/local/lib
make[1]: Leaving directory '/code/zlog/src'
root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog#

可以看到上面的命令主要作用是下面几条

  • cp -a zlog.h /usr/local/include:把zlog.h头文件复制到gcc编译器默认搜寻目录
  • cp -a libzlog.so /usr/local/lib/libzlog.so.1.2:把动态库文件libzlog.so复制到gcc编译器默认搜寻目录,并重命名为libzlog.so.1.2
  • cp -a libzlog.a /usr/local/lib:把静态库文件libzlog.a复制到gcc编译器默认搜寻目录

我们继续可以执行一下make test来看看

root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog# make test
cd test && make
make[1]: Entering directory '/code/zlog/test'
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_tmp.o -c test_tmp.c -I. -I../src
gcc -O2 -g -o test_tmp test_tmp.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_longlog.o -c test_longlog.c -I. -I../src
gcc -O2 -g -o test_longlog test_longlog.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_buf.o -c test_buf.c -I. -I../src
gcc -O2 -g -o test_buf test_buf.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_bitmap.o -c test_bitmap.c -I. -I../src
gcc -O2 -g -o test_bitmap test_bitmap.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_conf.o -c test_conf.c -I. -I../src
gcc -O2 -g -o test_conf test_conf.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_conf2.o -c test_conf2.c -I. -I../src
gcc -O2 -g -o test_conf2 test_conf2.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_hashtable.o -c test_hashtable.c -I. -I../src
In file included from ../src/zc_profile.c:9,from test_hashtable.c:15:
../src/fmacros.h:4: warning: "_DEFAULT_SOURCE" redefined4 | #define _DEFAULT_SOURCE|
In file included from /usr/include/x86_64-linux-gnu/bits/libc-header-start.h:33,from /usr/include/stdio.h:27,from test_hashtable.c:9:
/usr/include/features.h:227: note: this is the location of the previous definition227 | # define _DEFAULT_SOURCE 1|
gcc -O2 -g -o test_hashtable test_hashtable.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_hello.o -c test_hello.c -I. -I../src
gcc -O2 -g -o test_hello test_hello.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_hex.o -c test_hex.c -I. -I../src
gcc -O2 -g -o test_hex test_hex.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_init.o -c test_init.c -I. -I../src
gcc -O2 -g -o test_init test_init.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_level.o -c test_level.c -I. -I../src
gcc -O2 -g -o test_level test_level.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_leak.o -c test_leak.c -I. -I../src
gcc -O2 -g -o test_leak test_leak.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_mdc.o -c test_mdc.c -I. -I../src
gcc -O2 -g -o test_mdc test_mdc.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_multithread.o -c test_multithread.c -I. -I../src
gcc -O2 -g -o test_multithread test_multithread.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_record.o -c test_record.c -I. -I../src
gcc -O2 -g -o test_record test_record.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_pipe.o -c test_pipe.c -I. -I../src
gcc -O2 -g -o test_pipe test_pipe.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_press_zlog.o -c test_press_zlog.c -I. -I../src
gcc -O2 -g -o test_press_zlog test_press_zlog.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_press_zlog2.o -c test_press_zlog2.c -I. -I../src
gcc -O2 -g -o test_press_zlog2 test_press_zlog2.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_press_write.o -c test_press_write.c -I. -I../src
test_press_write.c: In function ‘work’:
test_press_write.c:33:3: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]33 |   write(fd, aa, sizeof(aa)-1);|   ^~~~~~~~~~~~~~~~~~~~~~~~~~~
gcc -O2 -g -o test_press_write test_press_write.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_press_write2.o -c test_press_write2.c -I. -I../src
test_press_write2.c: In function ‘work’:
test_press_write2.c:36:3: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]36 |   write(fd, log, sizeof(log)-1);|   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gcc -O2 -g -o test_press_write2 test_press_write2.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_press_syslog.o -c test_press_syslog.c -I. -I../src
gcc -O2 -g -o test_press_syslog test_press_syslog.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_syslog.o -c test_syslog.c -I. -I../src
gcc -O2 -g -o test_syslog test_syslog.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_default.o -c test_default.c -I. -I../src
gcc -O2 -g -o test_default test_default.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_profile.o -c test_profile.c -I. -I../src
gcc -O2 -g -o test_profile test_profile.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_category.o -c test_category.c -I. -I../src
gcc -O2 -g -o test_category test_category.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_prompt.o -c test_prompt.c -I. -I../src
gcc -O2 -g -o test_prompt test_prompt.o -L../src -lzlog -lpthread -Wl,-rpath ../src
gcc -O2 -g -Wall -D_GNU_SOURCE -o test_enabled.o -c test_enabled.c -I. -I../src
gcc -O2 -g -o test_enabled test_enabled.o -L../src -lzlog -lpthread -Wl,-rpath ../src
make[1]: Leaving directory '/code/zlog/test'
root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog#

可以看到在test文件夹下生成了很多的测试程序,我们切到test目录

root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog# cd test/
root@iZ2zeaqy08amcq9fk440kyZ:/code/zlog/test# ./test_hello
2021-07-31 22:09:37 INFO [58918:test_hello.c:30] hello, zlog
2021-07-31 22:09:37.911 hello, zlog
2021-07-31 22:09:37.911232 hello, zlogroot@iZ2zeaqy08amcq9fk440kyZ:/code/zlog/test# ./test_init
2021-07-31 22:10:25 INFO [58919:test_init.c:34] before update
TEST after update
2021-07-31 22:10:27 INFO [58919:test_init.c:52] init againroot@iZ2zeaqy08amcq9fk440kyZ:/code/zlog/test# ./test_level
TRACE trace hello, zlog - trace
INFO info hello, zlog - inforoot@iZ2zeaqy08amcq9fk440kyZ:/code/zlog/test# ./test_longlog
useage: test_longlog [count]

可以看到我们可以去执行这些测试程序


通过上面的步骤,我们生成了静态库文件和动态库文件,并将库文件和头文件都复制到了gcc编译器默认的搜寻路径了。

前面我们在test下测试了一些zlog的功能,下面我们开始自己写一点简单的demo来测试一下zlog。

zlog的使用很简单,只需要头文件包含zlog.h就可以,此外还需要一个配置文件.conf

//test_hello.c
#include <stdio.h>
#include "zlog.h"int main(int argc, char** argv)
{int rc;zlog_category_t *c;rc = zlog_init("test_hello.conf");if (rc) {printf("init failed\n");return -1;}c = zlog_get_category("my_cat");if (!c) {printf("get cat fail\n");zlog_fini();return -2;}zlog_info(c, "hello, zlog");zlog_fini();return 0;
}
//test_hello.conf
[formats]simple = "%m%n"[rules]my_cat.DEBUG    >stdout; simple

执行编译命令

root@iZ2zeaqy08amcq9fk440kyZ:/code# cc -c -o test_hello.o test_hello.c -I/usr/local/include
root@iZ2zeaqy08amcq9fk440kyZ:/code# cc -o test_hello test_hello.o -L/usr/local/lib -lzlog
root@iZ2zeaqy08amcq9fk440kyZ:/code# ./test_hello
2021-07-31 22:38:04.10 INFO   (my_cat:test_hello.c:24) - hello, zlog
sh: 1: /usr/bin/cronolog: not found

在当前目录下,我们会生成aa.log日志文件,里面的内容如下

2021-07-31 22:38:04.10 INFO   (my_cat:test_hello.c:24) - hello, zlog

【C语言开源库】C语言开源库zlog的使用相关推荐

  1. c语言比较好的gui界面库,几款开源的图形界面库(GUI Libraries)

    国外: 遵循BSD许可协议的C++ GUI库,建立在Windows API之上,但仍可以通过使用WineLib在Linux/xNix上使用.也支持Pocket PC和基于Windows CE的应用程序 ...

  2. c语言 解析pdf 开源库,使用第三方开源库mupdf,实现pdf转png

    step1: 下载SumatraPDF工程: https://github.com/Bitterbell/Pdf-Reader muPdf 库是一个开源的 pdf 读取器,但是在 github 上下载 ...

  3. 介绍一个开源的SIP(VOIP)协议库PJSIP

    本文系转载,出处不可考. 假设你对SIP/VoIP技术感兴趣,哪希望你不要错过:),假设你对写出堪称优美的Code感兴趣 ,那么你也不可错过:) 这期间我想分析一下一个实际的协议栈的设计到实现的相关技 ...

  4. beeshell —— 开源的 React Native 组件库

    背景 beeshell 是一个 React Native 应用的基础组件库,基于 0.53.3 版本,提供一整套开箱即用的高质量组件,包含 JavaScript(以下简称 JS)组件和复合组件(包含 ...

  5. css 解析 开源库_干货 | python库大全,全面高效

    黑客技术点击右侧关注,了解黑客的世界! Java开发进阶点击右侧关注,掌握进阶之路! Python开发点击右侧关注,探讨技术话题!来源丨速学Pythonhttps://mp.weixin.qq.com ...

  6. [2021]Linux下C语言qrencode二维码生成库的基本使用和ARM开发板移植

    文章目录 一.前言 二.准备所用到的环境以及版本信息 1.Ubuntu和内核版本 2.gcc和g++版本 3.交叉编译gcc和g++版本 4.开发板信息 三.开发环境编译&安装qrencode ...

  7. Winform(C#) 国内开源美化控件主题库2:花木兰控件库

    Winform(C#) 国内开源美化控件主题库2:花木兰控件库 地址 博客:https://www.cnblogs.com/tlmbem/控件的介绍. gitee:https://gitee.com/ ...

  8. 共有65款 计算机视觉库/人脸识别开源软件

    转载:https://www.cnblogs.com/Anita9002/p/5038533.html. 引自:http://www.oschina.net/project/tag/316/openc ...

  9. 计算机视觉库/人脸识别开源软件

    中文车牌识别系统 EasyPR EasyPR 是一个开源的中文车牌识别系统. EasyPR是一个中文的开源车牌识别系统,其目标是成为一个简单.灵活.准确的车牌识别引擎. 相比于其他的车牌识别系统,Ea ...

  10. linux c 界面库,几款开源的图形界面库(GUI Libraries)

    国外: 遵循BSD许可协议的C++ GUI库,建立在Windows API之上,但仍可以通过使用WineLib在Linux/xNix上使用.也支持Pocket PC和基于Windows CE的应用程序 ...

最新文章

  1. spring手动控制事务开启_Spring 基于AOP的事务控制
  2. Centos下安装Mysql
  3. audio标签控制音量_HTML5中audio与video标签的使用
  4. camel apache_Apache Camel 3的工作终于开始了
  5. delphi 中如果不进行 closehandle 会怎么样_心理学:当你迷茫了,请坚持做三件事,你的未来会越来越好...
  6. LL(1)分析表的构造
  7. Python 标准库 —— json
  8. 为什么专家都看好这本书
  9. 移动微技(Mobile Widget)应用开发权威指南
  10. JVM 图形化监控工具
  11. python 导入离线地图_PyQGIS开发 -- 离线地图
  12. JavaScript模板引擎
  13. 了结MySQL information_schema
  14. IT精英?OR IT民工?
  15. 苹果在 WWDC 上宣布放弃英特尔转向 ARM 芯片,会有哪些优势?你的 Mac 将何去何从?
  16. iOS 苹果官方Demo合集
  17. apt-get安装问题:请尝试不指明软件包的名字来运行“apt-get -f install”
  18. 教你“偷偷”的获取微信小游戏好友关系链数据
  19. 拓嘉启远:关于拼多多搜索溢价,你了解多少
  20. 诺基亚安卓手机_神速!HMD公布诺基亚手机安卓10升级计划

热门文章

  1. 十段均衡器开源 android,音频EQ(均衡器) - osc_1jvmu893的个人空间 - OSCHINA - 中文开源技术交流社区...
  2. 创建三维零件的DH坐标系
  3. Open_source_tools
  4. 掌握 Microsoft Excel 宏和 Excel VBA
  5. 如何卸载office201032位_如何卸载流氓版office2010
  6. js实现简易五子棋游戏
  7. Redis 菜鸟教程学习笔记- Redis 数据结构
  8. 仿淘宝收货地址,本地数据库
  9. qj71c24n通讯实例_三菱Q系列串行通信模块QJ71C24N概述与特点
  10. 安装labelImg(Ubuntu / Linux)