预处理器开关确定类的版本
我有一个类,有两种可能的实现,具体取决于预处理器开关。 我处理这个问题的方法是创建“src\CompSwitch1\class.h”和“src\CompSwitch2\class.h”。 在我的标准包含文件中,我使用
#ifdef CompSwitch1
#include "CompSwitch1\class.h"
#elif CompSwitch2
#include "CompSwitch2\class.h"
#else
#error "Specify CompSwitch1 or CompSwitch2"
#endif
这适用于大多数需要两个版本的类。 但是,在其中一个上,我收到链接器错误(lnk2019:无法解析的外部符号)。 我正在使用 MS Visual Studio 2005 和 2008,并且它都出现在它们上。
在 .h 文件的顶部,我针对预处理器选项进行了测试。 另外,虽然为了简洁起见我只引用了 .h 文件,但在相应的目录中每个文件都有一个 .cpp 文件。
I have a class with two possible implementations, depending on a preprocessor switch. The way I have handled this is to create "src\CompSwitch1\class.h" and "src\CompSwitch2\class.h". In my standard include file, I use
#ifdef CompSwitch1
#include "CompSwitch1\class.h"
#elif CompSwitch2
#include "CompSwitch2\class.h"
#else
#error "Specify CompSwitch1 or CompSwitch2"
#endif
This works for most of my classes that need two versions. However, on one of them, I get a linker error (lnk2019: unresolved external symbol). I'm using MS Visual Studio 2005 and 2008, and it appears on both of them.
At the top of the .h file, I test against the preprocessor option.
Also, although I only referenced the .h file for brevity, there is also a .cpp file for each of these, in the appropriate directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您可能已经包含了其中一个类的头文件,但链接了另一个类的目标文件,或者两者都没有链接
It sounds like you might have included the header file for one of the classes, but linked the object file for for the other one, or neither
它真的应该是 #ELIF DEFINED( CompSwitch2 ) 。 否则,您假设“CompSwitch2”已定义为值 1 ...
it should really be #ELIF DEFINED( CompSwitch2 ) really. Otherwise you are assuming "CompSwitch2" has been defined with a value of 1 ...
尝试在 cpp 实现文件中包含以下预处理器行后将其放入标头中:
如果编译错误的标头/cpp 对,您至少应该得到编译错误而不是链接错误。 有更容易识别/修复的方法;)
另一种可能性是 cpp 文件根本不包含在编译中。 在 cpp 文件中放入一条消息杂注,以查看它们是否完全被编译:
或者尝试在构建目录中识别是否创建了与 cpp 编译单元相关的目标文件。
希望有帮助,
奥瓦内斯
Try to put in the cpp implementation files after they include your header the following preprocessor line(s):
If you compile wrong header/cpp pairs you should get at least compilation errors and not linking errors. There are much easier to identify/fix ;)
Another possibility is that the cpp-files are not included into compilation at all. Put a message pragma inside the cpp file to see if they get compiled at all:
Or try to identify in the build directory if there object files created, which relate to cpp-compilation units.
Hope that helps,
Ovanes
您可以使用预处理的 cpp 文件(扩展所有包含和宏的阶段)。
在 VS 2008 中,右键单击“解决方案资源管理器”->“属性”->“C++”->“预处理器”中的文件,然后设置“生成预处理文件”并设置“包含行号 (/P)”。
之后再次右键单击您的文件并选择“编译”。 扩展名为“i”的文件(例如main.i)将在cpp 所在的同一目录中创建。 打开它并查看包含哪些包含文件。
这种方法很难解决困难的编译问题(例如,系统头文件中的某些宏替换了代码中的某些内容)。
You can use preprocessed cpp file (stage where all includes and macros are expanded).
In VS 2008 right click on your file in Solution Explorer->Properties->C++->Preprocessor and set "Generate Preprocessed File" set "With Line Numbers (/P)".
After that right click again your file and choose "Compile". File with extension "i") (e.g. main.i) will be created in the same directory where cpp resides. Open it and see which include file is included.
This method is very handful to solve hard compilation problems (e.g. some macro from system header files replaces something in your code).