在另一个系统上构建时禁用特定的C++行和 #includes
Disable specific C++ lines and #includes when building on another system
>我在我的项目中使用了Google glog日志记录系统。 具体来说,我在代码的不同位置使用以下类型的语句:
#include <glog/logging.h>
CHECK_EQ(foo,bar) << "Generic error message";
LOG(ERROR) << "Generic error message";
LOG(FATAL) << "Generic error message";
此日志记录系统对于帮助我暂存和验证代码非常重要。但是,有时我会将代码的生产运行转移到更大的服务器。 此大型生产服务器没有日志记录系统。(我尝试在此生产服务器上构建日志记录系统,但遇到了问题。
那么,假设我不能在生产服务器上使用日志记录系统,我该如何配置内容以使日志记录命令在服务器上处于非活动状态?
在询问SO之前尝试自己解决这个问题,我尝试了以下方法...
在名为"globals.h"的现有头文件中,我定义了:
#ifdef NOGLOG
#define MYLOG(i,m) std::cerr << #i << ": " << m
#else
#include <glog/logging.h>
#define MYLOG(i,m) LOG(i) << m
#endif
然后,我可以将LOG(ERROR) << "ab" << x << "cd"
之类的代码替换为MYLOG(ERROR,"ab" << x << "cd")
。 然后,当我使用像 make all CUSTOM="-DNOGLOG"
这样的命令进行构建时,我的 gcc 编译语句是用一个名为 $(CUSTOM)
的变量设置的,glog 语句不会被编译,而是编译简单的std::cerr
语句。
我在使用这种方法时遇到了两个问题:(1)我不知道如何让Eclipse IDE将$(CUSTOM)
插入到Eclipse生成的makefile中的gcc编译语句中;(2)将#include <glog/logging.h>
语句放在#else
体内会导致许多错误消息,如下所示。
In file included from /usr/include/errno.h:36,
from /usr/local/include/glog/logging.h:39,
from ../globals.h:23,
from ../COMPASS.h:11,
from ../COMPASS.cpp:13:
/usr/include/bits/errno.h: In function 'int* __errno_location()':
/usr/include/bits/errno.h:43: error: expected primary-expression before ',' token
注意:目前,我只是注释掉了CHECK_EQ
调用,并认为一旦我弄清楚如何解决与LOG
调用相关的此问题,我应该能够轻松地将解决方案扩展到CHECK_EQ
调用。
对于您的编译问题,
message << x << y
无效。
您可以使用类似以下内容:
// mystream.h
// class MyStreamImpl; // forward declaration for pimpl idiom if necessary
class MyStream
{
public:
MyStream(int level);
~MyStream();
MyStream& operator << (const char*);
MyStream& operator << (int);
// some other needed base type.
private:
int level;
// std::unique_ptr<MyStreamImpl> m_impl; // for pimpl idiom if necessary
};
MyStream MYLOG(int level) { return MyStream(level); }
// mylog.cpp
#ifdef NOGLOG
#include <iostream>
// implementation of all methods without glog
MyStream& MyStream::operator << (const char* message)
{
std::cerr << message;
return *this;
}
// other forwards to std::cerr
#else // NOGLOG
#include <glog/logging.h>
// implementation of all methods with glog
MyStream& MyStream::operator << (const char* message)
{
Log(level) << message;
return *this;
}
// other forwards to LOG()
#endif
我接受了 Jarod42 的答案,但我想在这里添加我自己的答案,我最终可能会使用它。 我认为Jarod42的可能更优雅,更好,但我在这里的黑客解决方案很容易实现。
我发现,与其尝试在编译步骤中向 CXXFLAGS 添加一个变量,然后我可以定义该变量来表示是否使用 glog 系统,不如简单地基于现有的 release
配置在 Eclipse 中创建另一个构建配置。 在这个新配置中,将其称为release_noglog
,然后我可以>转到 C/C++ 构建>设置的属性> GCC C++编译器>预处理器,并NOGLOG
添加为"定义的符号"。 这样,在 release
中构建不会给编译语句增加NOGLOG
,但内置release_noglog
会。
然后,我可以简单地使用以下宏定义:
#ifdef NOGLOG
#define MYLOG(i,m) std::cerr << #i << ": " << m
#define MYCHECK_EQ(i,j,m) CHECK_EQ(i,j) << m
#else
#define MYLOG(i,m) LOG(i) << m
#define MYCHECK_EQ(i,j,m) std::cerr << #i << " != " << #j << ": " << m
#endif
而且,我不会#include <glog/logging.h>
取决于是否定义了NOGLOG
,因为未解决的包含不会阻止编译。(至少,不是在有问题的特定系统上。
- 在 c++ 中是否有像 javascript 中那样"includes()"的方法
- 当 ESP32 在 Newlib/ 而不是 ESP32/ 中找到组件 #includes esp_types.h 时,E
- CMake 看不到源文件,找不到 #includes
- 我可以只从带有 Ctags 的C++文件中获取 #includes 吗?
- 关于循环依赖 #includes C++
- 编译器是否C++优化未使用的 #includes
- 在代码(头文件和实现文件)中放置#includes和Redundancies的位置
- 展开 #includes 到文本文件以进行C++
- VS2010 未正确处理预处理器条件下的"includes"
- No GTK includes
- 如何在编译DLL时添加includes
- c++ fails to find includes
- 为什么不为类使用cpp文件includes
- 将所有常用的全局变量移到一个头文件中,以减少#includes的数量,这是一种好的做法吗
- 你能把 #includes 放在C++标题中吗?
- C++ #includes和类型变量的设计问题
- 在另一个系统上构建时禁用特定的C++行和 #includes
- Understanding #includes c++
- 使用 Clang 的 libtool 匹配 #includes(或 #defines)的正确方法是什么?
- How do includes在C++中工作