Code::Blocks SDL2.0 "undefined reference to 'IMG_Load'" - c++

I'm trying to use c++, SDL, and SDL-image to make a game; I'm using Code::Blocks as my IDE, and I'm having some trouble.
I've downloaded the latest mingw SDL development files, and the latest mingw SDL-image development files.
I've placed all the SDL-image stuff into the same directory as the SDL stuff (merging the one into the other).
I've added the x86_64-w64-mingw32/include and x86_64-w64-mingw32/lib directories to my Code::Blocks search directories.
My linker settings are as follows:
-lmingw32
-lSDL2main
-lSDL2
-lSDL2_image
-lopengl32
-lglu32
(Obviously I'm using openGL as well, but I don't think that's involved here).
I have the correct #includes as far as I can tell:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_opengl.h>
But I'm still getting the error: undefined reference to 'IMG_Load'
Here's the build log:
obj\Release\gfx_Texture.o:gfx_Texture.cpp:(.text+0xd7): undefined reference to `IMG_Load'
obj\Release\gfx_Texture.o:gfx_Texture.cpp:(.text+0x1dd): undefined reference to `SDL_FreeSurface'
obj\Release\gfx_Texture.o:gfx_Texture.cpp:(.text+0x1f4): undefined reference to `SDL_FreeSurface'
g:/program files(x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: obj\Release\gfx_Texture.o: bad reloc address 0x4 in section `.text.startup'
g:/program files (x86)/codeblocks/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
And the actual code:
SDL_Surface* image = IMG_Load(file);
Where file is a char*.
I understand that there have been some changes to the way SDL_Surfaces work in SDL 2.0 as compared to SDL 1.2 (I'm trying to migrate, both from 1.2 to 2.0 and from MVSC to C::B), so could that have something to do with it? Am I using IMG_Load wrong?
Your help is much appreciated, and I'll supply any missing info on request if it'll clarify anything.
One more thought: The latest version of SDL is 2.0.1, yet the latest version of SDL_image is 2.0.0. Do I need those versions to match?

This happen When the program doesn't linker correctly at the executed time
for example:
asas#asas:~/SDL$ g++ -Wall SDL_program.cpp -lSDL2
/tmp/cc1eC6CK.o: InfunctionLTexture::loadFromFile(std::__cxx11::basic_string<char,std::char traits<char>, std::allocator<char> >)':
SDL_program.cpp:(.text+0x7d): **undefined reference toIMG_Load'**
As you can see above miss linker -lSDL2_image .
Is possible too, that you have SDL1.2 and SDL2 at the same root and again Code blocks hasn't linked correctly

Well don't I feel dumb. When I was working in MVC the x86 prefix meant 32-bit; for some reason, it means 64-bit in C::B, and the i686 prefix means 32-bit.

I encountered such an error, and all I did was check if my linker texts are correctly written check if you have typed "ISDL2_image" instead of "lSDL2_image"
(write small 'L' then SDL2 not cap 'I' then SDL2)for SDL2 projects.
for SDL1 just change the '2's into '1's. đź‘Ť

Related

Is linking libraries not as simple as specifying the path of the library and the path of the header files?

Context:
cygwin-gcc as my terminal and compiler
Trying to use OpenGL for my program and it requires GLEW extension to make it work. I chose to dynamically link my program to the required libraries.
Downloaded, make installe'd, the glew libraries and header files.
Put .dll files in usr/local/lib folder including the opengl32.dll which is originally from the system32 folder to test but still not working.
Put header files in /usr/local/include folder
Problem:
Getting underfined references to functions.
Input Command:
gcc -o TestGameBoy TestGameBoy.c -I/usr/local/include/GL -L/usr/local/lib -lglew32 -lglu32 -lopengl32
Edit: -opengl32 changed to -lopengl32 but just a typo.
Linking output error:
/tmp/cc0Qp34q.o: In function `initWindowComponents':
/cygdrive/d/DMG_Emulator/BGWindow.c:35: undefined reference to `glewExperimental'
/cygdrive/d/DMG_Emulator/BGWindow.c:36: undefined reference to `glewInit'
/cygdrive/d/DMG_Emulator/BGWindow.c:48: undefined reference to `glGenTextures'
/cygdrive/d/DMG_Emulator/BGWindow.c:50: undefined reference to `glBindTexture'
/cygdrive/d/DMG_Emulator/BGWindow.c:54: undefined reference to `glTexImage2D'
collect2: error: ld returned 1 exit status
Note: I didnt included the SDL part since it is already working. I managed to follow its argument list but somehow not working for glew.
Id like to believe that this is because the linker doesn't actually load the library into the ram. Soo it is unable to fill those necessary data for assembly calls to function's memory space which needs a defined memory address? I dont know Im mostly theorizing myself after I watch a video probably realted to my problem. But this does makes sense for me though somehow I couldnt make it work.
Never(!!!) touch anything that resides in system32 or SysWow64! These are operating system resources and messing around with those will cause a lot of headache in the long run.
As for using (modern) OpenGL with Cygwin… that's kind of a PITA. You have this XWin/GLX wrapper sitting in between, so that programs written for X11 can be run inside the Cygwin X11 server. This in turn means that inside Cygwin you're dealing with *nix-ish OpenGL library names, i.e. -lGL -lGLX -l… (probably expects X libraries as well). Which also means that you can not talk directly to the native OpenGL implementation in a pure Cygwin environment.
There's (might be) an error in your linker invocation. It's generally -l… yet you have -opengl32 there (dropped the l). However the linker should complain about every miswritten option or libraries specified, but it can not find.
I strongly suggest using MinGW/MSys2 instead of Cygwin which gives you truly native Windows API environment and makes things lots of easier. MSys2 uses the Pacman package manager (of Arch Linux) and is an all around pleasant to work with environment. It gives you all the *nix-ish dev tools (GCC / Clang, Shells, etc.) yet is native Win32.
opengl32.dll is a shared lib, but not the import lib needed by the compiler. opengl32.dll also does not match the expected the shared library name in cygwin that is cyg*.dll
Install cygwin libGLEW-devel package. It will provide the correct GLEW import lib and the headers
The content is listed on
https://cygwin.com/packages/x86_64/libGLEW-devel/libGLEW-devel-1.13.0-1
2016-07-21 22:36 1018809 usr/include/GL/glew.h
2016-07-21 22:36 73140 usr/include/GL/glxew.h
2016-07-21 22:36 0 usr/lib/
2016-07-21 22:36 2106644 usr/lib/libGLEW.dll.a
2016-07-21 22:36 1667262 usr/lib/libGLEWmx.dll.a
2016-07-21 22:36 0 usr/lib/pkgconfig/
2016-07-21 22:36 222 usr/lib/pkgconfig/glew.pc
2016-07-21 22:36 233 usr/lib/pkgconfig/glewmx.pc
There is also a glew package OpenGL Extension Wrangler demos that you can use to test the proper installation.

vtk autoinit linker error

I have a very confusing (at least for me :) ) problem with linking to vtk-libraries. On one desktop pc, my code compiles without any error. I have an identical installation on another pc, but there, I get linker errors.
On both pc's are installed: itk 4.8, vtk 6.3 and cmake 3.3. Both are debian-systems (8.1 - jessie).
[ 96%] Linking CXX executable project
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolume_AutoInit::~vtkRenderingVolume_AutoInit()':
/usr/local/include/vtk-6.3/vtkRenderingVolumeModule.h:44: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Destruct()'
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolumeOpenGL_ModuleInit::~vtkRenderingVolumeOpenGL_ModuleInit()':
project/main.cpp:4: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Destruct()'
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolumeOpenGL_ModuleInit':
project/main.cpp:4: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Construct()'
CMakeFiles/project.dir/main.cpp.o: In function vtkRenderingVolume_AutoInit':
/usr/local/include/vtk-6.3/vtkRenderingVolumeModule.h:44: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Construct()'
libcommon.a(datareader.cpp.o): In function vtkRenderingVolume_AutoInit':
/usr/local/include/vtk-6.3/vtkRenderingVolumeModule.h:44: undefined reference tovtkRenderingVolumeOpenGL_AutoInit_Construct()'
collect2: error: ld returned 1 exit status
CMakeFiles/project.dir/build.make:326: recipe for target 'project' failed
I tried to fix those errors with adding
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);
to the file where I am using the vtk libraries, but it didn't help.
Does anyone know what I'm doing wrong? I am wondering, why it works on one of those pcs but not on the other one.
thank you very much!
Update: I fixed my problem by switching the position of
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
in the CMakeLists to the end. However, does anyone know, why it makes such a huge difference for different pcs?
As long as the VTK_USE_FILE is found, your code should compile correctly. In your other PC, VTK_USE_FILE was already found from a previous configuration and sits in a CMake cache entry, so that is why it compiles fine.
If your are using Nightly,
Your should put these in front of your including any vtk headers.
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingFreeType);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL);
and link all the library you made from vtk source

I keep getting "Undefined reference to WinMain#16" in Qt Creator

I'm trying to use SDL 2.0 in Qt Creator 3.1, using the Qt Build Suite (QBS) in Windows 7.
I've put the links in my .qbs to locate the SDL folders and I think everything works ok, because in my main.cpp it's recognizing the SDL functions, trying to autocomplete them.
The problem is, whenever I try to compile, I get the 'undefined reference to WinMain#16'
I'm using MinGW and set in the Compiler preferences the linker flags to: -lmingw32 -lSDL2main -lSDL2 -mwindows
The output is:
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/libmingw32.a(main.o): In function `main':
e:\p\giaw\src\pkg\mingwrt-4.0.3-1-mingw32-src\bld/../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/main.c:91: undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
And my .qbs is:
import qbs 1.0
CppApplication {
name: "testeSDL2"
cpp.includePaths: [".",
"C:/SDL2-2.0.3/i686-w64-mingw32/include"]
cpp.libraryPaths: [
"C:/SDL2-2.0.3/i686-w64-mingw32/lib"]
cpp.dynamicLibraries: "SDL2"
files: [
"main.cpp"
]
qbsSearchPaths: "C:/CppLibs/"
}
I've been searching a lot and only find answers for common Qt projects using the .pro but not the .qbs... And there's a section in the SDL site that says it's a linker problem, but I set them in the preferences window, my guess is that I also need to set them somehow in the .qbs file, but I don't have a clue on how to do so. Can anyone help?
As I recall, SDL defines a macro main that expands to some SDL-specific function name.
Find that macro definition and you also find the macro symbol you need to disable that thing.
Oh well maybe this should have been a comment not an answer, but when/if you find that (sorry I don't have time to check those sources) then you can mark this as “the solution”, he he. :-)
Addendum: since the OP kindly posted the SDL code, I've looked at it and found this comment:
“If you provide your own WinMain(), you may define SDL_MAIN_HANDLED”
… before including the SDL headers.
Then the macro called main is not defined.

Libusb undefined reference to

I'm trying to set up libusb API on my OS. I downloaded libusb api on libusb.org. I followed the standard installation procedure:
cd into directory
./configure
make
make check //without errors
make install
Then I launched Eclipse C/C++ and copied some code from the tutorial found on the internet. But when trying to build it I got following output:
main.cpp:(.text+0x19): undefined reference to `libusb_init'
main.cpp:(.text+0x76): undefined reference to `libusb_set_debug'
main.cpp:(.text+0x8a): undefined reference to `libusb_get_device_list'
main.cpp:(.text+0x136): undefined reference to `libusb_free_device_list'
main.cpp:(.text+0x142): undefined reference to `libusb_exit'
/tmp/ccOWJGwe.o: In function `printdev(libusb_device*)':
main.cpp:(.text+0x162): undefined reference to `libusb_get_device_descriptor'
main.cpp:(.text+0x28a): undefined reference to `libusb_get_config_descriptor'
main.cpp:(.text+0x4d4): undefined reference to `libusb_free_config_descriptor'
collect2: ld returned 1 exit status
I have libusb.so in /lib and also I have usb.h in /usr/local/include and the link for the .so and libusb.a in /usr/local/lib.
Also the #include inside the code is correct.
I know that problem is in linker but I, kind of, cannot make it work :)
I'm using Fedora 15 operating system and gcc 4.6.0 20110603 (Red Hat 4.6.0-10) version compiler.
So what could I do to resolve these undefined references? Thanks very much for help :)
I did face the same problem. But I was able to solve it by adding '-lusb-1.0' to the linker.
e.g : g++ myfile.cpp -lusb-1.0
you have to set the library linker flag for compilation in the linker,
you can get a full list in the console by executing
pkg-config --list-all
These are the libraries which you have installed on your system and you have to link against the ones you want to use.
so in your example it is libusb so you do
pkg-config --libs libusb
there should be the output
-lusb
or
-lusb-1.0
This gives you the flag you have to pass to the linker. e.g.
g++ myfile.cpp -lusb[-1.0]
Then you edit the configuration of the project and search for the linkerflags, there should be a textfield for that somewhere in the buildoptions. i'm not quite shure where to find it but googling for it suggested:
Project -> Properties -> C/C++
Build -> Miscellaneous -> flags
After you found it, just add the linker flag in the textfield and you should be fine.
EDIT
since my answer is the accepted one, I also added the other flag that seems to work for a lot of people.
What is your linker command line? You need to have -lusb in the linking command; just having the header included won't work.
I don't use Eclipse C/C++ but I am pretty sure the reason is the same that I faced some while ago when setting up a C project in Netbeans.
It's not enough to have the #include in your code and the library at the right location - you also have to tell Eclipse where to look for them and how to use them. This turorial shows you how to set it up in Eclipse.

Undefined symbol _main when trying to build logstalgia on mac

I have been trying to build the logstalgia project (http://code.google.com/p/logstalgia/) on my Mac (10.5). Rather than having to link it to the system libraries correctly, I have built and added all of the dependencies to the project. I am new at this, but I do think I have done this correctly, mostly because I have had two of my friends who are much more experienced say so.
Adding the frameworks removed all of the compile errors, but I still get a linker error. It seems to not be able to find the main() function. I have verified I included main.cpp in the sources to be compiled (using XCode) and that there are no accidental double declarations. I have also verified that the main function is correctly declared (no missing brackets, etc).
It is as though XCode does not link in the correct order. Any help would be really appreciated, I am really excited to be down to a single error! (Hope fixing this does not open a floodgate).
Thanks,
Hamilton
PS - I can definitely provide a zip of the Xcode project if anyone is willing to look!
Checking Dependencies
Ld "/Users/hamiltont/Downloads/logstalgia-0.9.2 2/Untitled/build/Debug/Untitled" normal i386
cd "/Users/hamiltont/Downloads/logstalgia-0.9.2 2/Untitled"
setenv MACOSX_DEPLOYMENT_TARGET 10.5
/developer/usr/bin/g++-4.0 -arch i386 -isysroot /developer/SDKs/MacOSX10.5.sdk "-L/Users/hamiltont/Downloads/logstalgia-0.9.2 2/Untitled/build/Debug" -L/sw/lib "-L/Users/hamiltont/Downloads/logstalgia-0.9.2 2/Untitled/../../pcre-7.9/.libs" -L/opt/local/lib -L/sw/lib "-F/Users/hamiltont/Downloads/logstalgia-0.9.2 2/Untitled/build/Debug" -F/Users/hamiltont/Downloads/logstalgia-0.9.2 -F2/src/SDL.framework "-F/Users/hamiltont/Downloads/logstalgia-0.9.2 2/Untitled" -filelist "/Users/hamiltont/Downloads/logstalgia-0.9.2 2/Untitled/build/Untitled.build/Debug/Untitled.build/Objects-normal/i386/Untitled.LinkFileList" -mmacosx-version-min=10.5 -framework OpenGL -lpcre -lSDL -lSDL_image-1.2.0 -prebind -o "/Users/hamiltont/Downloads/logstalgia-0.9.2 2/Untitled/build/Debug/Untitled"
Undefined symbols:
"_main", referenced from:
start in crt1.10.5.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I got this error to go away. If I understand, essentially SDL re-names the main function, so that it can do some stuff, then run your application, then clean up. Turns out that if you are building in Xcode, you must use ObjectiveC to compile your application.
In Xcode, telling the linker to try and use SDL_main(), rather than just main() does not work (for some technical reasons that are a bit beyond me). So, you include a few Objective C files. In Oc, you get the benefit of being able to say explicitely what the name of your main class is. Hence, the Objective C files you include seem to do nothing more than let Xcode know to look for SDL_main().
In summary, this really had nothing to do with Logstalgia, but was entirely a problem with getting SDL to link correctly in Xcode. This link is talking about this problem exactly. The SDLMain.h and SDLMain.m are Objective C files. If you can't find them, try googleing "Setting up SDL templates in Xcode." I installed the templates in Xcode, used one of them to create an empty project that would compile, link, and run (and promptly do nothing!) and then I added the project files I wanted to the pre-configured project.
Thanks!
Double-check the link file list to make sure main.cpp's object file is present there:
/Users/hamiltont/Downloads/logstalgia-0.9.2 2/Untitled/build/Untitled.build/Debug/Untitled.build/Objects-normal/i386/Untitled.LinkFileList
You might also want to preprocess the main.cpp to make sure main isn't getting inadvertently renamed (via a rogue macro) or omitted (via a rogue #if).