(SFML, Code::Blocks) "undefined reference to (...)" error upon Texture.importFromFile(...) - c++

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.

Related

Why cannot I compile my code with g+ in my terminal [duplicate]

I just started using MinGW for Windows. When trying to create executable using
g++ a.cpp -o a.exe -std=c++14
for the code below:
#include <string>
using namespace std;
int main()
{
string x = to_string(123);
return 0;
}
I'm getting following error:
C:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../libmingwex.a(vsnprintf.o):(.text+0x0): multiple definition of vsnprintf
C:\Users\..\Local\Temp\cc4sJDvK.o:c:/mingw/include/stdio.h:426: first defined here
collect2.exe: error: ld returned 1 exit status
What is the root cause for this error and how can I make it go away? While I can easily find a replacement for to_string() function I'm not sure what is causing this error to occur in the first place.
Installing MinGW packages mingw32-libmingwex-* will link an appropriate version of vsnprintf and avoid the linker error.
This issue, i.e. multiple definition of vsnprintf, still exists in MinGW as December 2019.
After investigating a lot, I found the solution in the official mailing list.
It's a bug in mingwrt-5.2.2. Downgrading to the mingwrt-5.2.1 version solves that issue.
To do that, just input the following command:
mingw-get upgrade mingwrt=5.2.1
Then restart the MinGW shell.
Read the full story here.
Note: MinGW-w64 and MinGW are separate projects, so the accepted solution is not so helpful to me, as I want to keep MinGW and not to move to MinGW-w64.
I solved this issue using MinGW w64 compiler
download mingw-w64-install.exe
setup to Version: 6.3.0, Architecture: i686, Threads: posix, Exception: dwarf and Build revision: 2.
I hope this will be of some help.
There are multiple definitions of vsnprintf in both stdio.h and libmingwex.a. I am able to work this around by adding #define __USE_MINGW_ANSI_STDIO 0 to the start of the code, before any includes, which disables the definition of vsnprintf in stdio.h.

SFML undefined reference

I tried to link SFML with codeblocks, but I have some errors running the code from tutorial. This is code I tried to write after I saw that the one in tutorial doesn't work:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
int main()
{
sf::Window w(sf::VideoMode(800,600),"da");
return 0;
}
This is the tutorial I followed.
After I compile the code above it gives me 4 error that say:
||=== Build: Release in SFML1 (compiler: GNU GCC Compiler) ===|
obj\Release\main.o:main.cpp:(.text.startup+0x97)||undefined reference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'|
obj\Release\main.o:main.cpp:(.text.startup+0xc4)||undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'|
obj\Release\main.o:main.cpp:(.text.startup+0xaa)||undefined reference to `_imp___ZN2sf6WindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'|
obj\Release\main.o:main.cpp:(.text.startup+0xd3)||undefined reference to `_imp___ZN2sf6WindowD1Ev'|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
You have made an error or have not followed the tutorial to the letter. This can happen. Everyone makes mistakes. Start over completely new and follow that tutorial to the letter. We cannot possibly know which mistake you made, because to do this, we would need to step through this tutorial as well and check if all the steps were followed correctly.
Please note that the latest version is 2.3 so you should follow the 2.3 tutorial.

Linker can't find declared function in Glaux library

(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.

SFML loadFromFile undefined reference

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.

'class std::map<std::basic_string<char>, Gui>' has no member named 'emplace'

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.