![](/img/trans.png)
[英]Why do I get so many `LNK2005` errors despite using `#ifndef #define #endif` code blocks in my .h files?
[英]Why can the code between ifndef/endif still get run here?
#define cimg_use_jpeg 2
#ifndef cimg_use_jpeg
//code goes here
#endif
我真的不明白
每次类似的事情(所谓的不可能的事情)发生在我身上,原因是:我在编辑器中看到的代码不是运行的代码。 这可以通过多种方式发生。
无法执行。 也许您运行的可执行文件版本不正确。 打印__DATE__
和__TIME__
进行跟踪或记录有助于检测此类错误。
你写
#define yoursymbol
#ifdef yoursymbol
或者你写
#define yoursymbol somevalue
#if yousymbol == somevalue
不要混在一起
编辑:由于人们似乎误解了我的意思,这是一个澄清:在编写程序时,进行编码很重要,这样其他人才能理解您的意图,因此,如果您的意图只是检查是否已定义符号,则在指定时会造成混淆一个数值。
一定可以按预期工作。 假设cimg_use_jpeg
已按预期定义,则即使编译器也不会编译#ifndef/#endif
块中的代码。
所以问题是, cimg_use_jpeg
如您cimg_use_jpeg
? 还是在简化您的问题,并通过简化来提供实际可行的示例?
@AndersK。这有效,即未执行适当的操作。 代码产生“ BAZ未定义”
#define FOO 2
#define BAR
int main(int argc, char* argv[])
{
#ifndef FOO
std::cout << "FOO is undefined" << std::endl;
#endif
#ifndef BAR
std::cout << "BAR is undefined" << std::endl;
#endif
#ifndef BAZ
std::cout << "BAZ is undefined" << std::endl;
#endif
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.