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.
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 *
Related
Hi I'm trying to make mfc simple project to my final assignment in oop course, but after I install the mfc
I get error of:
1. STRCPY_S
The file that have the error is read only and I can't change it. I've tried to add CRT comment and change the precomplier but no result.
error description here
the line is gives the error:
inline void __cdecl strcpy_s(
_Out_writes_z_(_S1max) char *_S1,
_In_ size_t _S1max,
_In_z_ const char *_S2)
{
ATLMFC_CRT_ERRORCHECK(::strcpy_s(_S1, _S1max, _S2));
}
I'm trying to run this .cpp file in Codeblocks (only change is adding #include for windows.h to beginning).
It uses OpenGL, GLUT, and GLUI. I think I've gotten OpenGL and GLUT to work in Codeblocks but GLUI is still giving me some issues. At first, I downloaded GLUI from here and copied glui.h into C:\Program Files (x86)\CodeBlocks\MinGW\include\GL.
I was getting an error around line 455:
void control_cb( int control ){
if (control == 5){
GLUI_Master.close_all();
(...)
};
which stated there was an undefined reference to GLUI_Master.
I then realized that the GitHub repository also had a GLUI header file so I deleted the other header file and copied this new one into the same place as above.
I was able to get a little further now, getting an error around line 508:
void createSettingsMenu(){
(...)
new GLUI_Button( settings, "Update", 5,control_cb);
//settings->set_main_gfx_window(mainWindow);
(...)
};
which stated there was no matching function for call to:
GLUI_Button::GLUI_Button(GLUI*&, const char [7], int, void(&) (int)).
I'm not sure what this means, but it also says
**note: candidate: GLUI_Button::GLUI_Button()
note: candidate expects 0 arguments, 4 provided**
around line 847:
GLUI_Button( void ) {
sprintf( name, "Button: %p", this );
type = GLUI_CONTROL_BUTTON;
h = GLUI_BUTTON_SIZE;
w = 100;
alignment = GLUI_ALIGN_CENTER;
can_activate = true;
};
I'm assuming this is related to the error but I'm not sure how to fix this -- I thought this error was surely caused by an issue in setting up GLUI with Codeblocks.
I'm trying to modify VLFeat source code in particular the function vl_vlad_code in this file. Now, since it is the first time that I edit the source code of such a library, I started with something simple, like printing an Hello World! at the beginning of the fucntion:
void
vl_vlad_encode (void * enc, vl_type dataType,
void const * means, vl_size dimension, vl_size numClusters,
void const * data, vl_size numData,
void const * assignments,
int flags)
{
printf("Hello World!");
...
Then (following this document convention):
I removed the binary code with rm -rf VLFEATROOT/bin
I recompiled the library with make in VLFEATROOT (with no errors)
However, when I call vl_vlad_code from my application nothing is printed. Notice that the library works fine in my C++ application, it just "ignores" my changes.
Just for completeness, I use Ubuntu 16.04 and this are the compiling options regarding VLFeat that I use in my Eclipse CDT project:
... -I/home/luca/vlfeat ... -L/home/luca/vlfeat/bin/glnxa64 ... -lvl
UPDATE:
Following suggestions in the comments, I tried to write something to a file in this way:
void
vl_vlad_encode (void * enc, vl_type dataType,
void const * means, vl_size dimension, vl_size numClusters,
void const * data, vl_size numData,
void const * assignments,
int flags)
{
FILE *f = fopen("/home/luca/file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
/* print some text */
const char *text = "Write this to the file";
fprintf(f, "Some text: %s\n", text);
And no file is created!
It is possible that when you run the application you are actually linking with a system-wide installed library. As a test, try adding the directory where you built the library to the LD_LIBRARY_PATH environment variable then run your program.
From the command line you could do:
export LD_LIBRARY_PATH=/path/to/your/lib:${LD_LIBRARY_PATH}
If you are using an IDE, you should set the variable inside of its environment settings.
Paths in this variable will be searched before your system libs are searched.
You can always find out exactly where the libraries your executable is linking to are located by running:
ldd /path/to/your/executable
The problem was a copy of libvl.so in /usr/loca/lib. After deleting it the problem was solved and the message is correctly printed.
I'm extremely confused and have been going in circles for about the past hour with this now.
I am attempting to add the ATIDAQ C Library to my project zip link here for the ATIDAQ C Library. In my project I have added:
C/C++->General->Additional Include Directories: <PATH_TO_LIB>\ATIDAQ C Library\ATIDAQ
However I keep getting the error LNK2001: unresolved external symbol _createCalibration
main.cpp
extern "C" {
#include <ftconfig.h>
}
int main(int argc, char* argv[]) {
char *calfilepath; // name of calibration file
unsigned short index; // index of calibration in file (second parameter; default = 1)
Calibration *cal = NULL; // struct containing calibration information
unsigned short i, j; // loop variables
index = 1;
calfilepath = argv[1];
cal = createCalibration(calfilepath, index);
printf(" Serial: %s\n", cal->Serial);
printf(" Body Style: %s\n", cal->BodyStyle);
}
Which is I think caused by the line C/C++->General->Additional Include Directories: <PATH_TO_LIB>\ATIDAQ C Library\ATIDAQ. The IDE finds the Calibration *createCalibration(char *CalFilePath, unsigned short index); function (as this is not syntax highlighted and tab completes).
I'm really unsure what is going, wrong. I think I am missing something with the linker, but I am just not sure as to what.
The ATIDAQ C Library doesn't provide a .lib for you to link against; instead, you should instruct the Visual C++ compiler to compile the necessary C source files in the same folder. In this case, createCalibration is implemented in ftconfig.c.
I have included a header file for a library in my ARC-enabled Objective-C project.
I know the library is not compiled with ARC enabled, but the problem is the header file of the library, specifically these lines:
template <typename Type_>
static inline Type_ &MSHookIvar(id self, const char *name) {
Ivar ivar(class_getInstanceVariable(object_getClass(self), name));
void *pointer(ivar == NULL ? NULL : reinterpret_cast<char *>(self) + ivar_getOffset(ivar));
return *reinterpret_cast<Type_ *>(pointer);
}
I get this error:
Cast of an Objective-C pointer to 'char *' is disallowed with ARC
Is it possible to fix this error?
The whole header file can be found here: http://puu.sh/sTrH
You need to change the initialization of pointer to this:
void *pointer(ivar == NULL ? NULL : reinterpret_cast<char *>((__bridge void *)self) + ivar_getOffset(ivar));
Go to the target of your project and select the Build Phases tab. Open the Compile Sources section and find that header file. Add the compiler flag "-fno-objc-arc" minus the quotes. This will cause that file to be compiled regularly and should fix your issue assuming the code works in a non arc environment.