这里的场景是:“baselogger.h”包含API,我在两种情况下实现这些API(case A和case B,A对应1.cpp,B对应2.cpp),那么如何编写宏来编译不同的使用不同的 cpp 在“baselogger.h”中实现 API 的情况?
You don't necessarily need a macro for this. You could instead compile and link with 1.cpp in case A and compile and link with 2.cpp in case B.
CMake example:
option (CASE_A "Descriptive description" ON)
if (CASE_A)
target_sources(target_name PRIVATE 1.cpp)
else ()
target_sources(target_name PRIVATE 2.cpp)
endif ()
Insted of option, you can have some other condition. This is often useful for porting API to different systems.
But a macro based solution works too:
// 1.cpp
#ifdef MACRO_CASE_A
// case A implementation for baselogger.h
#endif
// 2.cpp
#ifndef MACRO_CASE_A
// case B implementation for baselogger.h
#endif
In this approach, simply compile both sources always.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.