Preventing multiple #define when including headers

笑着哭i 提交于 2021-02-05 11:07:14

问题


coming from python and am a bit tripped up on what the proper approach to this is.

I am trying to include this library in my project: https://github.com/nothings/stb/blob/master/stb_image.h

to do so, i have to #define STB_IMAGE_IMPLEMENTATION exactly once before importing the file (as per that file's doc)

This makes sense, where I am confused is, I have CLASS.h/cpp and in .h I define functions that use typedefs from that file, so I have

#define STB_IMAGE_IMPLEMENTATION

#include <stb_image.h> 

in that header file, and can't move these lines to .cpp as headers needs the defs for function def, but as soon as another file includes this header, (#ifndef wont help, i believe), that will be defined twice

I have a structure where TOP creates the CLASS above, but parent also creates OTHER, and OTHER needs to include PARENT, which includes CLASS, which triggers the issue (and prevents me from just moving the #define to PARENT) Note the actual class structure is more complex then this, but this idea seems to be a core issue, and I'm looking for the general best practice.

So, is there some way to ensure these #defines are defined before anything else, and done only once? This seems like a fundamental thing but I can't figure it out - What's the best approach?

This code is a library, and doesn't have a defined entry if that matters


回答1:


Create a cpp file (or whatever extension you are using for your source files) whose sole purpose is to have

#define STB_IMAGE_IMPLEMENTATION

#include <stb_image.h> 

and don't forget to include this cpp file into your project so that it is compiled and the result is linked into your program. In all other places where you need something from this library, just include the stb_image.h header as usual.

These "implementation" macros are a "trick" used by some library authors to make "installing" their library easy. The idea is that when a specific macro (chosen by the library authors) is defined before the header of that library is included, some code with the actual implementation will be added. That is why this must be in a source file instead of a header and only in one source file (otherwise you get multiple definitions for the same functions).




回答2:


You should have the #define STB_IMAGE_IMPLEMENTATION macro definition in exactly one source file that includes <stb_image.h> (a .cpp file, not in a header).

Since you can also only have one source file that defines main(), it is simple to put the #define in the same file as main() (as long as it also includes <stb_image.h>), but it can be used in any other source file if you prefer. You could even create a source file stb_image_imp.cpp that contains just the two lines shown, and link that into your program (or library) too.

All other source files in the project should only include <stb_image.h> without also defining the macro.




回答3:


#define is a preprocessor directive and doesn't actually get run everytime the header is accessed so you should 't have any problems

if you are using visual studio you can also do #pragma once to only parse the file once stopping anything from happening twice



来源:https://stackoverflow.com/questions/64058036/preventing-multiple-define-when-including-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!