linux glog 0.50版本 设置交叉编译器生成静态库

本文档详细介绍了如何从源码下载、修改交叉编译配置、设置生成静态库,到编译执行及功能测试的全过程。通过修改CMakeLists.txt将glog编译为静态库libglog.a,然后在_build目录下执行cmake和make命令。最后,编写测试代码glog.cpp,成功验证了静态库的功能。
摘要由CSDN通过智能技术生成

目标:设置glog的交叉编译器,生成静态库libglog.a;
步骤:
(1)下载最新版本的glog源码,最新版本0.050(2021.08.23)
https://github.com/google/glog
(2)源码目录如下表:
在这里插入图片描述
(3)修改交叉编译器的文件,在目录toolchains
复制一份gcc-cxx11.cmake,更名为gcc-cxx11-crosscompiling.cmake,修改交叉编译器
在这里插入图片描述

在这里插入图片描述
(4)在CMakeLists.txt中可以设置编译为静态库还是动态库
在这里插入图片描述
将ON修改为OFF,生成静态库libglog.a;
(5)新建_build目录,并执行cmake命令,生成makefile文件,cmake命令的参数详细讲解请参考这里

cmake -H. -B_build -DCMAKE_TOOLCHAIN_FILE="${PWD}/toolchains/gcc-cxx11-crosscompiling.cmake" && cd -

(6)执行make命令,生成静态库;

ubuntu0compile:~/glog-master/_build$ make
Scanning dependencies of target glog
[  4%] Building CXX object CMakeFiles/glog.dir/src/demangle.cc.o
[  9%] Building CXX object CMakeFiles/glog.dir/src/logging.cc.o
[ 13%] Building CXX object CMakeFiles/glog.dir/src/raw_logging.cc.o
[ 18%] Building CXX object CMakeFiles/glog.dir/src/symbolize.cc.o
[ 22%] Building CXX object CMakeFiles/glog.dir/src/utilities.cc.o
[ 27%] Building CXX object CMakeFiles/glog.dir/src/vlog_is_on.cc.o
[ 31%] Building CXX object CMakeFiles/glog.dir/src/signalhandler.cc.o
[ 36%] Linking CXX static library libglog.a
[ 36%] Built target glog
Scanning dependencies of target signalhandler_unittest
[ 40%] Building CXX object CMakeFiles/signalhandler_unittest.dir/src/signalhandler_unittest.cc.o
[ 45%] Linking CXX executable signalhandler_unittest
[ 45%] Built target signalhandler_unittest
Scanning dependencies of target stacktrace_unittest
[ 50%] Building CXX object CMakeFiles/stacktrace_unittest.dir/src/stacktrace_unittest.cc.o
[ 54%] Linking CXX executable stacktrace_unittest
[ 54%] Built target stacktrace_unittest
Scanning dependencies of target demangle_unittest
[ 59%] Building CXX object CMakeFiles/demangle_unittest.dir/src/demangle_unittest.cc.o
[ 63%] Linking CXX executable demangle_unittest
[ 63%] Built target demangle_unittest
Scanning dependencies of target symbolize_unittest
[ 68%] Building CXX object CMakeFiles/symbolize_unittest.dir/src/symbolize_unittest.cc.o
[ 72%] Linking CXX executable symbolize_unittest
[ 72%] Built target symbolize_unittest
Scanning dependencies of target logging_unittest
[ 77%] Building CXX object CMakeFiles/logging_unittest.dir/src/logging_unittest.cc.o
[ 81%] Linking CXX executable logging_unittest
[ 81%] Built target logging_unittest
Scanning dependencies of target stl_logging_unittest
[ 86%] Building CXX object CMakeFiles/stl_logging_unittest.dir/src/stl_logging_unittest.cc.o
[ 90%] Linking CXX executable stl_logging_unittest
[ 90%] Built target stl_logging_unittest
Scanning dependencies of target utilities_unittest
[ 95%] Building CXX object CMakeFiles/utilities_unittest.dir/src/utilities_unittest.cc.o
[100%] Linking CXX executable utilities_unittest
[100%] Built target utilities_unittest

![(https://img-blog.csdnimg.cn/f774a65bfc844dc3aa507e2508f817a3.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hoYWlqaXV3bw==,size_16,color_FFFFFF,t_70)
(7)静态库功能测试,在_build/glog会生成可用的头文件,但是开源库里的头文件使用的路径需要修改一下,等编译报错时将头文件的路径glog/去掉,不使用绝对路径就可以了,编写测试代码glog.cpp,并编译执行;


/********************************************************
//glog.cpp
********************************************************/
#include <stdio.h>

#include "glog/logging.h"


int main()
{
   //初始化参数
   FLAGS_logtostderr = false;  //TRUE:标准输出,FALSE:文件输出
   FLAGS_alsologtostderr = true;  //除了日志文件之外是否需要标准输出
   FLAGS_colorlogtostderr = false;  //标准输出带颜色
   FLAGS_logbufsecs = 0;   //设置可以缓冲日志的最大秒数,0指实时输出
   FLAGS_max_log_size = 10;  //日志文件大小(单位:MB)
   FLAGS_stop_logging_if_full_disk = true; //磁盘满时是否记录到磁盘
   google::InitGoogleLogging("mqttserver");
   google::SetLogDestination(google::GLOG_INFO,"./test");
   LOG(INFO) << "this is log";
   LOG(WARNING) << "this is warnning";
   LOG(ERROR) << "this is error";
   google::ShutdownGoogleLogging();
}

编译命令:

g++ glog.cpp -o test -L. -I./ -lglog -pthread
ubuntu0compile:~/glog-master/_build$ ./test 
I20210823 19:12:40.152302 20951 glog.cpp:22] this is log
W20210823 19:12:40.152426 20951 glog.cpp:23] this is warnning
E20210823 19:12:40.152499 20951 glog.cpp:24] this is error

文章参考:
(1)http://docs.cocos.com/cocos2d-x/manual/zh/installation/CMake-Guide.html
(2)https://blog.csdn.net/kenstandlee/article/details/117335670
(3)https://www.cnblogs.com/cristiano-duan/p/12275271.html

附件是VS2010的工程,C++日志类,谷歌的东西,很好用,也很强大哦! glog简介 Google glog是一个基于程序级记录日志信息的c++库,编程使用方式与c++的stream操作类似,例: LOG(INFO) << "Found " << num_cookies << " cookies"; “LOG”宏为日志输出关键字,“INFO”为严重性程度。 主要支持功能: 1, 参数设置,以命令行参数的方式设置标志参数来控制日志记录行为; 2, 严重性分级,根据日志严重性分级记录日志; 3, 可有条件地记录日志信息; 4, 条件中止程序。丰富的条件判定宏,可预设程序终止条件; 5, 异常信号处理。程序异常情况,可自定义异常处理过程; 6, 支持debug功能。可只用于debug模式; 7, 自定义日志信息; 8, 线程安全日志记录方式; 9, 系统级日志记录; 10, google perror风格日志信息; 11, 精简日志字符串信息。 开源代码托管 开源代码地址:https://github.com/google/glog 其实官方开源代码已经有大量demo可以参考了,也提供了VS可以直接打开编译的项目。 如何使用 1:把glog文件夹拷贝到源代码目录 2:在工程设置中添加附加包含目录(glog\include;)和附加库目录(glog\lib;),在附件依赖项中添加对应lib文件,一一对应关系如下: MDd libglog32MDd.lib MD libglog32MD.lib MTd libglog32MTd.lib MT libglog32MT.lib 建议使用MDd和MD方式,带上对应的dll(在glog\bin目录,需要时拷贝到bin文件输出目录)可以避免使用MTd,MT引起的内存泄露是值得的。 #include #include using namespace std; //包含glog头文件,建议放在stdafx.h中 //#define GOOGLE_GLOG_DLL_DECL // 使用静态库的时候用这个,不过我测试静态库有内存泄露,所以不推荐使用静态库 #define GLOG_NO_ABBREVIATED_SEVERITIES #include "glog/logging.h" //获取当前程序的运行目录 string GetAppPathA() { char szExePath[MAX_PATH] = {0}; GetModuleFileNameA(NULL,szExePath,MAX_PATH); char *pstr = strrchr(szExePath,'\\'); memset(pstr+1,0,1); string strAppPath(szExePath); return strAppPath; } void main() { //glog初始化 google::InitGoogleLogging("重签程序"); string strLogPath = GetAppPathA().append("LogInfo\\"); CreateDirectoryA(strLogPath.c_str(),NULL); google::SetLogDestination(google::GLOG_INFO,strLogPath.c_str()); //功能测试 LOG(INFO)<<"log start...";//普通日志 LOG(WARNING)<<"Warning log";//警告日志 LOG(ERROR)<<"Error log";//错误日志 int i = 4; LOG_IF(INFO,i == 4)<<"Log if Test"; //以上就是我常用的几个日志函数了,当然还有很多更加强大的日志相关函数,大家如有有兴趣,可以参照官方给的示例使用, //开源代码地址:https://github.com/google/glog MessageBoxA(NULL,"Test Over",":)",MB_ICONINFORMATION); } 测试程序中,我使用的动态链接库方式。(Debug模式中代码生成为MDd,Release为MD)。lib是截止现在2015-11-04-21:35是最新的。采用VS2010编译,MTd,MT,MDd,MD方式编译在测试项目中都有提供。 博文地址: http://blog.csdn.net/sunflover454/article/details/49643625
Linux上使用glog静态库的步骤如下所示: 1. 首先,将glog静态库文件libglog.a和头文件src/glog复制到您的工程目录中。这些文件可以从引用中提供的资源中获取。 2. 接下来,创建一个名为main.cpp的文件,并将以下代码添加到该文件中: ``` /******************************************************** main.cpp ********************************************************/ #include <stdio.h> #include "glog/logging.h" int main() { // 初始化参数 FLAGS_logtostderr = false; // TRUE:标准输出,FALSE:文件输出 FLAGS_alsologtostderr = true; // 除了日志文件之外是否需要标准输出 FLAGS_colorlogtostderr = false; // 标准输出带颜色 FLAGS_logbufsecs = 0; // 设置可以缓冲日志的最大秒数,0指实时输出 FLAGS_max_log_size = 10; // 日志文件大小(单位:MB) FLAGS_stop_logging_if_full_disk = true; // 磁盘满时是否记录到磁盘 google::InitGoogleLogging("mqttserver"); google::SetLogDestination(google::GLOG_INFO, "./test"); LOG(INFO) << "this is log"; LOG(WARNING) << "this is warning"; LOG(ERROR) << "this is error"; google::ShutdownGoogleLogging(); } ``` 3. 编译和链接您的代码。您可以使用以下命令编译您的代码: ``` g++ main.cpp -o main -lglog ``` 这将生成一个名为main的可执行文件。 4. 运行您的程序。您可以使用以下命令运行您的程序: ``` ./main ``` 这将执行您的代码,并在"./test"目录下生成日志文件。 请注意,如果在编译过程中遇到任何错误,请根据引用中提供的信息检查头文件的路径是否正确,并确保已正确安装gflags和glog,如引用所述。 希望这可以帮助您在Linux上使用glog静态库。如果您还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值