2

I am writing a C++ OpenGL project using DevIL and I am getting compile-time errors while trying to work out how to load an image to use as a texture.

So far I have this

//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);
}

which presents me with the error: error C2110: '+' : cannot add two pointers

if I change the declaration of filename to string filename = "back.bmp"; and the if statement to

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

I get this linker error error LNK1104: cannot open file 'DevIL.libkernel32.lib'

I am certain that I have placed all the DevIL files where they need to be and added the dependencies in Project->Properties->Linker->Input->Additional Dependencies.

genpfault
  • 51,148
  • 11
  • 85
  • 139
OddCore
  • 1,534
  • 6
  • 19
  • 32

1 Answers1

2

Fix the compile error by ensuring you add C++ strings not C strings

throw runtime_error(std::string("Unable to load image") +filename);

Fix the link error by putting a space in between the libs in Additional Dependencies.

Also, if you have to use const_cast, odds are you're doing it wrong.

ILboolean ilLoadImage(const char *filename);

You don't need to cast to char * in order to pass .c_str() - .c_str() returns a const char *

Erik
  • 88,732
  • 13
  • 198
  • 189
  • The link error is persisting. I have added DevIL.lib, ILU.lib and ILUT.lib in Microsoft SDKs\Windows\7.0A\Lib and config.h, config.h.in, devil_cpp_wrapper.h, devil_internal_exports.h, il.h, ilu.h, ilu_region.h, ilut.h and ilut_config.h in the same directory as above, but instead of \Lib, in \Include\IL. My #include are: , and . Am I missing anything, or done anything wrong? – OddCore Apr 16 '11 at 14:05
  • You should set your "Additional Library Directories" to the location of your lib files, and add the libs space-separated to "Additional Dependencies". If you add anything to the SDK dir you're doing it wrong. – Erik Apr 16 '11 at 14:09
  • I did that. The problem is still there. Apparently, it applies to all DevIL libraries, not just DevIL.lib, but ilut.lib and ilu.lib, because I tried changing the order of them in "Additional Dependencies" and it's always the last one it can't find. I have also changed the "Additional Library Files" as you said, even though in the past when I put the OpenGL libraries in the SDK folder, and then not added them to the "Additional Library Files", it didn't complain. – OddCore Apr 16 '11 at 14:35
  • @OddCore: Try explicitly adding a space to the end of your lib list - sounds to me like your project file or one of the inherited property sheets is damaged. – Erik Apr 16 '11 at 16:29
  • I am sorry to have wasted your time, it turns out I was missing a ; at the end of the last .lib file in the "Additional Dependencies" list. And to think that I wasted 4 days on this.... – OddCore Apr 19 '11 at 10:43
  • @OddCore: Ouch, that hurts. Next time use the `...` button to setup libs? :P – Erik Apr 19 '11 at 10:44
  • Will do. Thanks for dedicating time to this. – OddCore Apr 19 '11 at 10:49
  • @OddCore: NP, that's what SO is for – Erik Apr 19 '11 at 10:53
  • Erik, hi! I'm sorry to temporarily revitalize this thread, but I thought I had resolved my error, but I didn't. So, I have this question: When loading the image, what does the if statement need to look like? because you mentioned that I don't need to cast but if(!iLoadImage(filename)) complains that "cannot convert parameter 1 from 'const char *' to 'const wchar_t *' " and if(!iLoadImage(filename.c_str())) complains that "left of .c_str must have class/struct/union". I also got rid of the declaration ILboolean ilLoadImage(const char *filename); cause it was causing errors, is that right to do – OddCore Apr 20 '11 at 13:35
  • ...I say declaration but I should really say definition, as I have it written in the .cpp file, not the header. – OddCore Apr 20 '11 at 13:37
  • @OddCore: You'll need to convert from `char *` to `wchar_t *` - that's a different thing, your best bet is to open a new question – Erik Apr 20 '11 at 13:42