本来是在ros1下调用glog库是正常的。
然后最近需要在ros2环境下工作,就顺便安装一下glog。
先说一下错误信息
‘GLOG_EXPORT’ does not name a type
这个会附带很长的相关报错。
例如下面这样
error <glog/logging.h> was not include correctly. See the documentation for how to consume the library.
解决方法:
在CMakeList中添加
add_compile_definitions(GLOG_USE_GLOG_EXPORT)
error: invalid new-expression of abstract class type’logger::Logger’
logger::Logger* logger = new Logger(google::base::GetLogger(google::INFO))
后面的note信息是:
because the following virtual functions are pure within ‘logger::Logger’:
class Logger : public google::base::Logger {
/usr/local/include/glog/logging.h
‘virtual void google::base::Logger::Write(bool, const char*, size_t)’
virtual void Write(bool force_flush,
查询了一下’invalid new-expression of abstract class type’是因为实例化了抽象类导致的。
而当前的Logger类的父类是google::base::Logger类,父类也是一个抽象类,所以应该是继承父类时,其纯虚函数Write未在子类中实现导致子类Logger也是抽象类。
分析出原因后,我发现我在Ros2安装时是源码安装的,其安装目录为usr/local/include,而其中的google::base::Logger类的Write是这样的
virtual void Write(bool force_flush, const std::chrono::system_clock::time_point& time stamp, const char* message, size_t message_len) = 0;
这与logger.h中的子类Write函数是无法对应的。
所以是最好在线安装,
因为我当前主机不能连接网络,所以去Ubuntu官网下载了gflags和glog的deb并安装了。
发现其安装目录是在usr/include下。
但我的工程死活索引不到usr/include下的logging.h文件(被自己菜到崩溃…)如果有懂的大佬可以教学一下。
所以我就直接修改了usr/local/include下的logging.h文件
将Write函数修改为与子类一致即可:
virtual void Write(bool force_flush, time_t time_stamp, const_char* message, int message_len) = 0;
问题解决。