I'm learning to use SFML with the idea of making a small game, so far I've created a window and messed about with it's settings, and set up a draw loop and event handler.
The trouble started once I tried to add my first texture using the following code:
#include "SpaceNomad.ih"
void MainMenu(GameEngine &Engine)
{
sf::Texture texture;
if(!texture.loadFromFile("MenuBackGround.png"))
{
cout << "couldn't load background texture\n";
}
sf::Sprite *sprite = new sf::Sprite;
sprite->setTexture(texture);
Engine.AddEntity(sprite, 5);
}
which is outside of a different image literally the example code given on the SFML tutorial page.
When I try to compile this I get the following error:
||=== Build: Debug in space nomad (compiler: GNU GCC Compiler) ===|
obj\Debug\projects\st\take2\MainMenu.o||In function `Z8MainMenuR10GameEngine':|
D:\projects\st\take2\MainMenu.cpp|7|undefined reference to `_imp___ZN2sf7Texture12loadFromFileERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_4RectIiEE'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
I have checked that all the sfml libraries are included in the build options(I'm using code::Blocks with GCC MinGW).
https://drive.google.com/file/d/0B4qzXcgqbLZtcHJWUy1vSUhpdVk/view?usp=sharing
Other topics I've seen deal with undefined references to functions people have made themselves, but here I'm using a library.
edit: I've just tried with the basic shape from the same libary:
sf::CircleShape *shape = new sf::CircleShape(50);
// set the shape color to green
shape->setFillColor(sf::Color(100, 250, 50));
Engine.AddEntity(shape, 5);
Which compiles and displays without a problem.
You're using SFML libraries which were compiled with a different runtime or a different runtime ABI than you link your application against.
If your SFML libraries weren't compiled with the same compiler that you have, then you need to rebuild.
If you have set any special flags on your application (e.g. different ABI, C++14, etc.) you'll have to rebuild SFML with the same flags as well or remove them from your project.
Also it only happens sometimes because it doesn't affect the whole runtime libraries but parts like std::string which get used when calling the loadFromFile function.
Related
[summary]
I can not compile vulkan program including "vkGetAccelerationStructureBuildSizesKHR" or "vkCreateAccelerationStructureKHR" with errors "undefined reference to `vkGetAccelerationStructureBuildSizesKHR'".
[environment]
OS : Ubuntu 20.04
command : clang++ with option -lvulkan
vulkan version : 1.2.170
[what tried]
I guess that it is need more library.
I added "-lvulkan_radeon", but it does not make any change.
For reference, If my program does not include any new feature of ray tracing, I can build it normally.
I read that ray tracing features are officially supported from version 1.2.167, so I expect that I can build it without any additional works, but I can't.
Is there any additional library to be linked?
As with all functions provided by extensions and not being part of the core, you have to manually define and get the function pointers in your application before you can call it:
PFN_vkGetAccelerationStructureBuildSizesKHR pfnGetAccelerationStructureBuildSizesKHR;
pfnGetAccelerationStructureBuildSizesKHR = reinterpret_cast<PFN_vkGetAccelerationStructureBuildSizesKHR>(vkGetDeviceProcAddr(device, "vkGetAccelerationStructureBuildSizesKHR"));
I decided to try out what is depicted in this YouTube video (building a Tetris game with C++) (I
mention the video for reference purposes).
I have Code::Blocks 17.12. I installed SFML (GCC 4.9.2 TDM (SJLJ) - 32-bit) from their download page and since "the compilers have to match 100%" I also installed TDM 4.9.2 (32-bit) from the link also provided on their download page (not before having run into errors from overlooking this detail to some extent), and set it as my compiler in Code::Blocks.
I followed the tutorial on SFML page. I ran into a couple errors but managed to fix them (such as the one mentioned in the previous parenthetical remark... among others). Everything went fine so I moved on to the code as shown in the YouTube video. At the point where the code is
#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(320, 480), "window");
Texture t;
t.loadFromFile("images/tiles.png");
Sprite s(t);
while (window.isOpen())
{
Event e;
while (window.pollEvent(e))
{
if (e.type == Event::Closed)
window.close();
}
window.clear(Color::White);
window.display();
}
return 0;
}
(for the record) I get the error
||=== Build: Release in Tetris (compiler: GNU GCC Compiler) ===|
obj\Release\main.o:main.cpp:(.text.startup+0x24c)||undefined reference to `_imp___ZN2sf7Texture12loadFromFileERKNSt7__cxx1112basic_stringIcSt11char_traits
IcESaIcEEERKNS_4RectIiEE'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 3 second(s)) ===|
This is my problem.
An answer to a related SO question suggests something that I understand as "you have to use in C::B the same
compiler as the one which was used to compile SMFL". However I think I have ensured that.
Another related SO question I read but cannot understand anything. I am a total
noob to C/C++ and can't do much more than dilligently, thoughtlessly following
the directions as for example were provided in the tutorial in the SFML site... so if anyone can help me, I'd appreciate it -- could it be made to work or will I have to build SFML myself?
EDIT: Here are screenshots for my linker settings.
With this link you provided in the comments, it looks like your linker settings should be the following:
sfml-window-s
sfml-system-s
opengl32
freetype
jpeg
I had an issue similar to this when I was trying to use SFML, and the linker settings in the correct order was the issue.
(I know it is similar to questions previously asked, but I tried for hours and failed to successfully use advice like the one written HERE and in related threads; I'm too inexperienced and a slow learner).
I'm Using:
Code::Blocks 13.12 IDE,
GNU GCC Compiler,
MinGW's folders for holding GLUT and
other libraries + files (like GLAux.h).
Windows 7 OS
Error message:
In function `Z7LoadBMPPc':
undefined reference to `auxDIBImageLoadA#4'
=== Build failed: 1 error(s), 5 warning(s) (0 minute(s), 3 second(s)) ===
(Testing one of the proposed solutions, when I additionelly add a Glaux library to the linker's libraries, dozens of other undefined reference errors pop up.)
The above function's declaration is in the <gl\GLAux.h> header file:
AUX_RGBImageRec * APIENTRY auxDIBImageLoadA(LPCSTR);
But naturally I can't go further, and look into the Glaux library itself.
I only know how to solve (some) undefined reference problems if they refer to the code I've written. Here it is not the case.
.
Which version of the undefined reference problem am I dealing with?
What to do?
Project → Build options → Linker Settings → Add → GLAUX → OK. Also you need to specify the full link to the picture. And pucture must be BMP 24 bit.
Hi Guy's looking for some help and can't understand the problem. I'm following this tutorial https://www.binpress.com/tutorial/creating-a-city-building-game-with-sfml-part-8-gui-system/130, I'm using CodeBlocks 13.12 and SFML 2.1 but when I try to compile the code I get this
||=== Build: Debug in Test (compiler: GNU GCC Compiler) ===|
C:\Users\James\Desktop\Summer Repeats 2015\Games Dev 2\Test\src\Game_State_Start.cpp||In constructor 'GameStateStart::GameStateStart(Game*)':|
C:\Users\James\Desktop\Summer Repeats 2015\Games Dev 2\Test\src\Game_State_Start.cpp|100|error: 'class std::map<std::basic_string<char>, Gui>' has no member named 'emplace'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
This is the line of code giving me this error.
this->guiSystem.emplace("menu", Gui(sf::Vector2f(192, 32), 4, false, game->stylesheets.at("button"),
{ std::make_pair("Load Game", "load_game") }));
I have CodeBlocks set for C++11 and change SFML to SFML-2.3.1-windows-gcc-4.9.2-mingw-32-bit because I found here saying I have to use g++ 4.8.0 or higher on this page std::map emplace gcc 4.8.2, but still getting the same error, also the website give's me the code to download even though I'm trying to follow it myself , I checked the code, importing into a new project and that didn't work either, So no idea whats wrong. Any help or a point in the right direction would be most appreciated.
Normally I would post this in a comment but I don't have enough rep.
Start a new project and compile and run this code:
#include <iostream>
#include <ostream>
int main()
{
std::cout << __cplusplus << std::endl;
}
If the result printed out is 199711, then it means you're not compiling using the -std=c++11 option. If the result printed out is 201101 then it means you're using an outdated version of the standard library.
Found the solution. I had originally installed codeblocks-13.12mingw-setup, I uninstalled this and installed codeblocks-13.12mingw-setup-TDM-GCC-481. It works now. Thanks for everybody's help.
Edit: I have in the meantime figured this out and written a detailed answer below.
I just tried switching from the Express version of MSVC 10 to Eclipse CDT on Win7, and while configuring I encountered a problem with the following simple OpenGL code (which works fine in Visual Studio):
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main()
{
GLFWwindow* w;
if (!glfwInit())
return -1;
w = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!w)
{
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(w);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
return -1;
}
while (!glfwWindowShouldClose(w))
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(w);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
In Visual Studio, I include the library paths for GLFW and GLEW, and link (in addition to the stuff that VS does built-in) opengl32.lib, glew32s.lib, glfw3.lib, in that order.
Now if I do the same in Eclipse CDT, I can't get it to work. The following errors occur:
Info: Internal Builder is used for build
g++ "-LD:\\lib\\cpp\\glfw-3.0.1.bin.WIN32\\lib-mingw" "-LD:\\lib\\cpp\\glew-1.10.0binaries\\lib\\Release\\Win32" -o glfwcheck.exe main.o -lopengl32 -lglew32s -lglfw3
Warning: .drectve `/DEFAULTLIB:"LIBCMT" /DEFAULTLIB:"OLDNAMES" ' unrecognized
D:\lib\cpp\glew-1.10.0binaries\lib\Release\Win32/glew32s.lib(tmp/glew_static/Release/Win32/glew.obj):(.text[__glewInit_GL_VERSION_1_2]+0x4): undefined reference to `_imp__wglGetProcAddress#4'
D:\lib\cpp\glew-1.10.0binaries\lib\Release\Win32/glew32s.lib(tmp/glew_static/Release/Win32/glew.obj):(.text[__glewInit_GL_VERSION_1_3]+0x4): undefined reference to `_imp__wglGetProcAddress#4'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: D:\lib\cpp\glew-1.10.0binaries\lib\Release\Win32/glew32s.lib(tmp/glew_static/Release/Win32/glew.obj): bad reloc address 0x4 in section `.text[__glewInit_GL_VERSION_1_3]'
d:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation
collect2.exe: error: ld returned 1 exit status
Of course I tried changing the order of the three libraries, but that only mad it worse. (By the way: I think it's strange that I can't reproduce these errors above such that they're the only ones when I re-arrange the libs to the initial order. I stay at 20+ errors until I delete ALL the libs, build, and add them again.)
After fumbling around and looking through forum posts, I figured that maybe it's a problem with the GLEW binaries, and compiled them on my own with MinGW. Only this time, I get all the 'not found' errors. I don't know how I can link statically with my self-compiled GLEW, since there's now no glew32s.lib any longer, but only libglew32.a and libglew32mx.a. Linking dynamically with glew32 and putting the dll into my project folder didn't work either.
I feel like I'm doing something very wrong here in Eclipse, or just forget some additional libraries (although I once tried putting all the ones VS uses in there as well and it still did the exact same thing).
Can you help me out? :) Otherwise I think I'd have to stay with VS, or switch to Linux + make.
I figured it out, and for anyone ever encountering the issue I will try and make clear what exactly I did to finally be able to create OpenGL projects with the setup MinGW + GLEW + GLFW. In my case, I used Eclipse CDT as IDE, but I'll write down the resulting g++ command line so it should be easy to adapt to other IDE's.
I'll suppose MinGW and MSYS (can be chosen to be installed from within the MinGW GUI; thus no need to download separately) are installed.
Download GLFW and unzip it in your external libraries folder of choice (in my case, this is D:\external\cpp, so it would be something along the lines of D:\external\cpp\glfw, where I renamed the glfw-3.0.3.bin.WIN32 folder to simply glfw).
Download the GLEW source as a zip folder an unzip it, in my case it's in D:\external\cpp\glew. Now start MSYS, cd to the glew folder and invoke make all.
Step 3 should have created (among others) the files libglew32.a and glew32.dll inside the folder glew\lib. Now right click your Eclipse CDT C++ project, go to Properties - C/C++ General - Paths and Symbols. In the Includes tab, add the paths to the include folders of GLFW and GLEW. Again, for me this is D:\external\cpp\glew\include and analogous for GLFW. In Library Paths, do the same for the folders lib (GLEW) and lib-mingw (GLFW).
Now we have to add the libraries we want our project to be linked with. If you wish to link with GLEW dynamically, make sure to include the glew32.dll in the folder where your executable will be. In Eclipse CDT, that's usually the Debug (or Release) folder in your project structure. In the Libraries tab in the options window we opened before, add (the order is important!) glfw3, glew32, opengl32, glu32, gdi32. Now building the project should work hopefully. In case you want to link statically with GLEW, add the same libraries with the exception of glew32. Instead, in the project properties go to C/C++ Build - Settings and in the Tool Settings - MinGW C++ Linker - Miscellaneous tab add the path to libglew32.a to the Other objects field. In my case, this is D:\external\cpp\glew\lib\libglew32.a. Now in order for the static linking to work, you have to either add #define GLEW_STATIC above #include <GL/glew.h> or use the preprocessor command -DGLEW_STATIC. The GLEW homepage says that it's also possible to include the glew.c and glew.h files into your project in order to link statically, but somehow this didn't really work out for me.
These steps worked for me, and they produced command lines similar to the following (I have only one file named main.cpp and used static linking with GLEW), which could be useful if you're trying to figure this matter out without Eclipse CDT.
g++ -ID:\external\cpp\glew\include -ID:\external\cpp\glfw\include -c -o main.o main.cpp
g++ -LD:\external\cpp\glew\lib -LD:\external\cpp\glfw\lib-mingw -o minimalexample.exe main.o D:\external\cpp\glew\lib\libglew32.a -lglfw3 -lopengl32 -lglu32 -lgdi32
In the dynamic linking case, simply remove the part containing libglew32.a in the second line, and add -lglew32 between -lglfw3 and -lopengl32. As a little example source file, you could just use the code in my above question.
I hope I can help anyone with this, as I sure saw me having a lot of trouble figuring this out between tens of error messages of unresolved symbols and various other problems :-)
Update: I tried to go over this again some days ago and ran into problems with the pre-compiled GLFW binaries for Windows (I'm now using Win8.1). But you can just use CMake in combination with mingw32-make to compile it on your own. Also, GLEW seems to not be getting updates anymore, so I switched to glad instead. It's also possible to use MinGW-w64 to compile the libraries and your final project as 64-bit application.