简体   繁体   中英

C++ preprocessor executing both #ifdef and #ifndef

So I'm currently working on something that uses OpenCL. The OpenCL spec provides the users with a directive which must be included before the inclusion of the header (cl.h)

#define CL_TARGET_OPENCL_VERSION 110

Which basically defines the version they want to use. Suppose I'm making a library and I want my users to define this instead of me defining this inside my files. What I did was.

-----main.cpp---
#define CL_TARGET_OPENCL_VERSION 110
#include "library.h"
-------x---------

----library.h-----
#ifdef CL_TARGET_OPENCL_VERSION
#pragma message("def")
#endif

#ifndef CL_TARGET_OPENCL_VERSION
#pragma message("ndef")
#endif
.... include other headers.
--------x---------

And the compiler prints both def and ndef messages. And the OpenCL library also throws a warning that it's undefined. I thought that the library header would get substituted into main and it'd only print the def message. Is there anything I understood wrong?

I'm particularly confused as to where does the preprocessor start? If it starts from main.cpp and goes from top to down, then it surely has defined the macro. After that it sees the library inclusion, then it should only print the def message but it prints both.

This leds me to believe the preprocessor does scan the header file before including it in main? Dunno the reason why. Also I have assured that the library header isn't included elsewhere.

One interesting thing I noticed was, if i did this

-----helper.h---
#define CL_TARGET_OPENCL_VERSION 110    
-------x---------

----library.h-----
#include helper.h    
#ifdef CL_TARGET_OPENCL_VERSION
#pragma message("def")
#endif

#ifndef CL_TARGET_OPENCL_VERSION
#pragma message("ndef")
#endif
.... include other headers.
--------x---------

It prints the def message "twice". If anybody can explain all this I'd be grateful.

EDIT:- The files I'm compiling are main.cpp library.h and library.cpp Library.cpp includes library.h from the start as usual. Maybe this other cpp is causing the problem?

In C/C++ programs, the compiler handles each .c and .cpp file separately.

The compilers build each source file (NOT the header files, only .c and .cpp files) independently from each other (this source files are called compilation unit ).

Thus, when your main.cpp is built, the compiler finds the #define CL_TARGET_OPENCL_VERSION 110 you have added on top of the main.cpp file, emiting the def message.

But when the compiler builds the library.cpp file, it does not find the version define, so it emits the ndef message.

So, following this explanation, it is completely normal that in your last case, when you add the define to the .h file, the compiler emits the def message twice, once for the main.cpp file and once for the library.cpp file.


Now, the problem is where should you add the define, in order to have the program built consistently, with the same version for all the .cpp files.

Usually, all the IDEs have some configuration page where you can add global defines, for all the project, which are "inserted" into all the compilation units before everything else. So when the IDE calls the compiler, it passes the same defines to all the compilation units. You should add this kind of defines in this page.

In your IDE (I am using Code::Blocks, v 17.12), you can find this page in the menu: Project / Build Options

For each type (Debug or Release), you have to go to the tab Compiler Settings , and there to the sub tab #defines . There you can add global defines, which can be different if you are building in Debug or in Release mode (of course, if you set the same in both modes, they would be the same).

Once you have added your define here, please, remove it from the main.cpp , library.h and any other place where you may have added it, in order to avoid duplicities.


From the comments about portability:

You have several options:

  • Always use Code::Blocks: this would be the easiest way, since you can pass the Code::Blocks project along with the source files, and everything would be already setup.

  • Use cmake , which is a script build system, where you can set defines and so in the same way as using an IDE. cmake is much widely used than Code::Blocks, so maybe it is a better option.

  • Add a new options.h header file, where you set all the defines , and include it to all your .c/.cpp. This setup has the additional benefit that for different systems, changing only the options.h file the build can be completely different. This is a manually setup of what the IDE is doing. It has the advantage that does not rely on external tools, but the disadvantage that you have to remember to add it in all the new .cpp files added to the project.

My recommendation is go with cmake , just as the others have said.

Prefer using #ifndef XXXX_h #define XXXX_h #endif over #pragma once

If your #include search path is sufficiently complicated, the compiler may be unable to tell the difference between two headers with the same basename (eg a/foo.h and b/foo.h) , so a #pragma once in one of them will suppress both. It may also be unable to tell that two different relative includes (eg #include "foo.h" and #include "../a/foo.h" refer to the same file, so #pragma once will fail to suppress a redundant include when it should have.

This also affects the compiler's ability to avoid rereading files with #ifndef guards, but that is just an optimization. With #ifndef guards, the compiler can safely read any file it isn't sure it has seen already; if it's wrong, it just has to do some extra work. As long as no two headers define the same guard macro, the code will compile as expected. And if two headers do define the same guard macro, the programmer can go in and change one of them.

#pragma once has no such safety net -- if the compiler is wrong about the identity of a header file, either way, the program will fail to compile. If you hit this bug, your only options are to stop using #pragma once, or to rename one of the headers. The names of headers are part of your API contract, so renaming is probably not an option.

(The short version of why this is problematic to use #pragma is that neither the Unix nor the Windows filesystem API offer any mechanism that guarantees to tell you whether two absolute pathnames refer to the same file.)

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.

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