C++ DevIL函数ilLoadImage - 程序退出,访问冲突

ket*_*sek 5 c++ devil

我有一个以这种方式定义的文件路径:

const char* GROUND_TEXTURE_FILE = "objects/textures/grass.jpg";
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

我的代码有什么问题?ilLoadImage调用时为什么程序会掉线?我认为,file.c_str()作为一种wchar_t *类型应该可以正常工作吗?谢谢你的回答:)

Hug*_*rmo 1

正如作者所说,您可以在不初始化库的情况下做任何事情: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;
}
Run Code Online (Sandbox Code Playgroud)

建造:

g++ -Wall -pedantic --std=c++11 -g -o app main.cpp -lIL
Run Code Online (Sandbox Code Playgroud)