C++DevIL函数ilLoadImage-程序退出,访问违规

C++ DevIL function ilLoadImage - program exit, access violation

本文关键字:访问 退出 函数 ilLoadImage- 程序 C++DevIL      更新时间:2023-10-16

我有一个这样定义的文件路径:

const char* GROUND_TEXTURE_FILE = "objects/textures/grass.jpg";

这是我用来加载图像的函数:

bool loadTexImage2D(const string &fileName, GLenum target) {
    ...
    // this will load image data to the currently bound image
    // at first, we must convert fileName, for ascii, this method is fine?
    wstring file(fileName.begin(), fileName.end());
    if(ilLoadImage(file.c_str()) == IL_FALSE) { //here the program falls

我的代码出了什么问题?调用ilLoadImage时程序为什么会失败?我认为,file.c_str()作为wchar_t *类型是否应该正常工作?感谢回答:)

正如作者所说,您可以在不初始化lib:D的情况下做任何事情

#include <iostream>
#include <IL/il.h>
int main ()
{
    std::string filename = "objects/textures/grass.jpg";
    ilInit();
    if (!ilLoadImage(filename.c_str())) {
        std::cout << ilGetError() << std::endl;
        return 1;
    }
    std::cout << ilGetInteger(IL_IMAGE_WIDTH) << std::endl;
    std::cout << ilGetInteger(IL_IMAGE_HEIGHT) << std::endl;
    return 0;
}

构建:

g++ -Wall -pedantic --std=c++11 -g -o app main.cpp -lIL