如何使用预处理器指令包含两次具有不同代码的文件?
How to use preprocessor directives to include file twice with different code?
我正在尝试为我的本机C++项目制作一个 C++/CLI 包装器,以便我可以在 C# 中使用它。到目前为止,我已经包装了我需要的类和函数,但我一直在尝试找到一种方法来对 Enums 做同样的事情,而无需将代码复制并粘贴到包装器中。
我尝试查找包装枚举的方法,但似乎没有办法使用来自本机C++代码的枚举,而无需在包装层中转换/重写代码。所以我找到了一个解决方案,您可以使用预处理器指令并将枚举包含在包装器中两次,但由于某种原因它似乎不起作用。
本机代码 (Enums.h)
#pragma once
#if defined MANAGED && !defined ENUMS_MANAGED_H
#define ENUMS_MANAGED_H
#define NAMESPACE managed
#define ENUM public enum class
#elif !defined MANAGED && !defined ENUMS_NATIVE_H
#define ENUMS_NATIVE_H
#define NAMESPACE native
#define ENUM enum class
#endif
namespace NAMESPACE
{
ENUM numbers
{
ONE = 1,
TWO = 2,
}
}
托管代码 (Wrapper.h)
#pragma once
#ifndef WRAPPER_H
#define WRAPPER_H
#include "Native.h" //Other native code which also includes Enums.h
#define MANAGED
#include "Enums.h"
namespace managed
{
managed::numbers num = managed::numbers::ONE; //managed does not contain 'numbers'
}
#endif //!WRAPPER_H
C# 代码(主.cs)
using managed; //managed project added as reference
static void main(String[] args)
{
Console.WriteLine((int) numbers.ONE); //managed does not contain 'numbers'
}
我希望能够在托管项目中与本机::数字一起使用托管::数字。我还希望能够在 C# 中使用数字枚举。但是,Visual Studio无法做到这一点。我也尝试重新排列包含,但这似乎不起作用。
我在托管类中创建了一个测试文件,其中包含 Enums.h,但不包含 Native.h:
测试文件.h
#ifndef TESTFILE_H
#define TESTFILE_H
#define MANAGED
#include "Enums.h"
namespace managed
{
managed::numbers num = managed::numbers::ONE; //Works
}
#endif //!TESTFILE_H
这有效,但我还需要能够包含 Native.h 并从那里使用枚举
编辑: 使用@robthebloke答案, 我的工作是在 Enums.h 中要做的:
#pragma once
// to disable error in EnumsImpl.h
#define BUILDING_ENUMS
#include "EnumsImpl.h"
#undef BUILDING_ENUMS
然后在 Wrapper.h 中:
#pragma once
#ifndef WRAPPER_H
#define WRAPPER_H
#include "Native.h"
#define BUILDING_ENUMS
#define MANAGED
#include "EnumsImpl.h"
#undef MANAGED
#undef BUILDING_ENUMS
#endif
有没有办法只使用一个文件而不必使用 EnumsImpl.h?
EnumsImpl.h
// guard against incorrect usage.
#if !defined(BUILDING_ENUMS)
#error "Do not include this file directly, include Enums.h instead"
#endif
// choose macros for below
#if defined MANAGED
#define NAMESPACE managed
#define ENUM public enum class
#elif !defined MANAGED
#define NAMESPACE native
#define ENUM enum class
#endif
namespace NAMESPACE
{
ENUM numbers
{
ONE = 1,
TWO = 2,
}
}
#undef NAMESPACE
#undef ENUM
枚举.h
#pragma once
// to disable error in EnumsImpl.h
#define BUILDING_ENUMS
// unmanaged
#include "EnumsImpl.h"
// managed
#define MANAGED
#include "EnumsImpl.h"
// remove temp defines
#undef MANAGED
#undef BUILDING_ENUMS
另一种选择...
#define DefEnum(NAMESPACE, ENUM)
namespace NAMESPACE
{
ENUM numbers
{
ONE = 1,
TWO = 2,
};
}
DefEnum(dave, enum class);
相关文章:
- 在C++代码中包含opencv时,使用ctypes创建.so文件
- 此代码编译良好,但文件未创建?请指出错误
- 找不到Linux Visual Studio代码C++文件
- C++(.cpp文件和.h文件)拆分代码并添加一个函数,提取 - 这很容易吗?
- Visual Studio代码文件未输出
- 是否可以使用 GCC 编译具有特定编译器标志的代码文件的一部分?
- 如何从ifstream加载LLVM比特代码文件
- 在同一代码文件中包括 Directx 9 和 10
- 如果我只想要架构良好的工作区,但不一定是分开的编译,如何在C++中组织头/代码文件
- 使用 Eclipse 运行.exe C 代码文件
- 是否可以稍后在代码(C++ I/O 文件)中使用输出文件作为输入文件
- 匿名命名空间-在头文件和代码文件中都有意义
- 代码文件外的代码文档
- 在静态库中硬编码的代码文件和头路径
- 更快地解析代码文件
- 为什么有些代码文件使用 close() 而不包含 unistd.h
- 在代码(.c文件)我如何找到linux发行版名称版本
- 在一个代码文件中使用头文件,反之亦然
- SonarQube未分析带有警告的C++代码文件不在项目目录下
- Qt 创建器代码文件重构