gpt4 book ai didi

c++ - 双重包含和仅 header 库 stbi_image

转载 作者:可可西里 更新时间:2023-11-01 18:38:57 26 4
gpt4 key购买 nike

我有一个包含 a.h 的 main.cpp(它有自己的 a.cpp)a.h 仅包含 header 库“stbi_image.h”:

#ifndef STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#endif

( https://github.com/nothings/stb )

*.cpp 使用#pragma once 包含它自己的 *.h

但我仍然得到:

LNK1169 one or more multiply defined symbols found LNK2005 stb-failure reason already defined in a.obj file = main.obj ... and a bunch of others

这对我来说似乎是正确的,但正如我在这个问题中的理解: Multiple definition and header-only libraries

也许我应该将内联/静态添加到我需要的 stb_image.h 函数中?我做错了什么吗?

提前致谢

最佳答案

  1. 也许我应该将内联/静态添加到我需要的 stb_image.h 函数中?

不,您已经有办法将“stb_image 函数”声明为静态或外部:

#define STB_IMAGE_STATIC
  1. 我做错了什么吗?是的,你编译'stb_image'两次,每次你包含'stb_image.h'因此,整个设计可以是:

Image.h:

#ifndef _IMAGE_H_
#define _IMAGE_H_

Class Image.h {
public:
Image() : _imgData(NULL) {}
virtual ~Image();
...
void loadf(...);
...

unsigned char* getData() const { return _imgData; }
protected:
unsigned char* _imgData;
};
#endif

图像.cpp:

#include "Image.h"

#define STB_IMAGE_IMPLEMENTATION // use of stb functions once and for all
#include "stb_image.h"

Image::~Image()
{
if ( _imgData )
stbi_image_free(_imgData);
}

void Image::load(...) {
_imgData = stbi_load(...);
}

main.cpp

#include "Image.h" // as you see, main.cpp do not know anything about stb stuff

int main() {
Image* img = new Image(); // this is my 'wrapper' to stb functions
img->load(...);

myTexture(img->getData(), ...);

return 0;
}

关于c++ - 双重包含和仅 header 库 stbi_image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43348798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com