DevIL and OpenGL in C++

DevIL and OpenGL in C++

本文关键字:C++ in OpenGL and DevIL      更新时间:2023-10-16

我正在使用DevIL编写一个c++ OpenGL项目,我在试图找出如何加载图像以用作纹理时出现编译时错误。

到目前为止我有这个

//Declarations
const char* filename = "back.bmp";
ILboolean ilLoadImage(const char *filename);
ILuint image;
ilGenImages(1, &image);
ilBindImage(image);
//Load the image
if (!ilLoadImage(filename))
{
throw runtime_error("Unable to load image" +filename);
}

显示错误:error C2110: '+' : cannot add two pointers

如果我将filename的声明更改为string filename = "back.bmp";并且将if语句更改为

if (!ilLoadImage(const_cast<char*>(filename.c_str())))

我得到这个链接器错误error LNK1104: cannot open file 'DevIL.libkernel32.lib'

我确信我已经将所有的DevIL文件放在了它们需要的地方,并在Project->Properties->Linker->Input->Additional dependencies中添加了依赖项。

通过确保添加 c++字符串而不是C字符串

修复编译错误
throw runtime_error(std::string("Unable to load image") +filename);

通过在附加依赖项中的库之间放置一个空格来修复链接错误。

同样,如果你必须使用const_cast,很可能你做错了。

ILboolean ilLoadImage(const char *filename);

你不需要强制转换为char *来传递.c_str() - .c_str()返回一个const char *