简体   繁体   English

C ++预处理器包含并定义多个文件的故障

[英]C++ Preprocessor include and define trouble with multiple files

I'm going to abstract my problem to avoid including unneeded details, but if needed I can provide the source code. 我将抽象我的问题以避免包含不需要的细节,但如果需要我可以提供源代码。 I'm using visual studio. 我正在使用visual studio。

I have the following files - all header files have #pragma once: 我有以下文件 - 所有头文件都有#pragma一次:

  • A.cpp //(containing my main function) A.cpp //(包含我的主要功能)
  • Bh BH
  • B.cpp //(Plays no role) B.cpp //(不扮演角色)
  • Ch
  • C.cpp C.cpp

And here is how the preprocessor commands are set up: 以下是预处理器命令的设置方法:

A.cpp   #defines UseOptionOne
A.cpp   #includes B.h

B.h     #ifdef UseOptionOne   
            #defines Func as f1() //(calling a function that prints a msg)   
        #else                 
            #defines Func as [blank]  

A.cpp   #includes C.h
C.h     #includes B.h     // (B.h have #pragma once, so it doesnt get included again)

Here's how the function calls are set up: 以下是函数调用的设置方法:

A.cpp   main function uses Func          //- It prints as intended 
A.cpp   calls function in C.cpp // this function does the following:
        {
           #ifndef UseOptionOne
             exit(0)                    //- Doesn't happen, so UseOptionOne is defined
           #endif   
           uses Func                        //- DOES NOTHING?????
        }
A.cpp   uses Func                       //- It prints as intended 

I don't understand how this is possible? 我不明白这是怎么回事? UseOptionOne is confirmed to still be defined in Ch but the Func is defined differently??? 确认UseOptionOne仍然在Ch中定义,但Func的定义不同???

Can anyone explain this? 有谁能解释一下? or would you want me to provide you with my rather complicated solution or some code fragments maybe? 或者您希望我为您提供我相当复杂的解决方案或一些代码片段?

I'm really lost :( 我真的迷路了:(

EDIT: I have used breakpoints to confirm that the C.cpp function is called, the 'Func' is simply treated as blank 编辑:我使用断点来确认调用C.cpp函数,'Func'被简单地视为空白

EDIT2: I can't answer my own question due to lack of reputation, so im putting it here: 编辑2:由于缺乏声誉,我无法回答我自己的问题,所以我把它放在这里:
I created a new project implementing my abstract description and it did trigger the exit in the #ifndef 我创建了一个实现我的抽象描述的新项目,它确实触发了#ifndef中的退出
So there is no way any of you could solve the problem with this description. 因此,您无法通过此描述解决问题。 I'm just going to have to look through everything again and find the mistake/error. 我只需要再次查看所有内容并找出错误/错误。

My two cents: 我的两分钱:

UseOptionsOne is defined only for A.cpp and for classes that includes Ah: in your case, it's defined only in A.cpp UseOptionsOne仅为A.cpp和包含Ah的类定义:在您的情况下,它仅在A.cpp中定义

C.cpp has no reference to Ah, so it does not see the define. C.cpp没有引用Ah,因此它没有看到定义。 In this case, UseOptionOne is not declared in C.cpp, it uses the blank function. 在这种情况下,UseOptionOne未在C.cpp中声明,它使用空白函数。

when you go out of the scope of C.cpp, and return in the scope of A.cpp, the function works because in that scope UseOptionOne is defined. 当你超出C.cpp的范围并返回A.cpp的范围时,该函数可以工作,因为在该范围内定义了UseOptionOne。

If you want to use the define in all files, you can create a separate definitions.h header and put in it the definitions, and then include that file in all headers for which you want the definition to work. 如果要在所有文件中使用define,可以创建单独的definitions.h标头并将其放入定义中,然后将该文件包含在您希望定义起作用的所有标头中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM