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

查看:147
本文介绍了包含标题时防止多个#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 的 def,但是只要另一个文件包含这个头文件,(我相信#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,但 parent 也创建了 OTHER,OTHER 需要包含 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天全站免登陆