包含标头时防止多个#define [英] Preventing multiple #define when including headers

查看:48
本文介绍了包含标头时防止多个#define的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自python,对解决此问题的正确方法有些了解.

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

我正在尝试将此库包含在我的项目中: https://github.com/nothings/stb/blob/master/stb_image.h

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

为此,我必须在导入文件之前(根据该文件的文档)#define STB_IMAGE_IMPLEMENTATION一次

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

这很有意义,我感到困惑的是,我有CLASS.h/cpp,在.h中,我定义了使用该文件中的typedef的函数,所以我有

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> 

在该头文件中,并且不能将这些行移动到.cpp,因为头需要函数def的defs,但是只要另一个文件包含此头,(#ifndef都帮不上忙,我相信),那将是定义两次

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

我有一个结构,其中TOP在上面创建了CLASS,但是父级也创建了OTHER,而OTHER需要包括PARENT的PARENT,而CLASS会触发问题(并阻止我将#define移至PARENT).实际的类结构比这要复杂得多,但是这个想法似乎是一个核心问题,我正在寻找一般的最佳实践.

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.

那么,有什么方法可以确保这些#defines在其他任何事情之前都被定义,并且只能被定义一次?这似乎是一件基本的事情,但我无法弄清楚-最好的方法是什么?

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

推荐答案

创建一个cpp文件(或用于源文件的任何扩展名),其唯一目的是

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> 

并且不要忘记将此cpp文件包含到您的项目中,以便对其进行编译并将结果链接到您的程序中.在需要从该库中获取内容的所有其他地方,只需照常包含 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).

这篇关于包含标头时防止多个#define的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆