Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: "Undefined reference to sf::..."  (Read 11177 times)

0 Members and 1 Guest are viewing this topic.

Mooks

  • Newbie
  • *
  • Posts: 7
    • View Profile
"Undefined reference to sf::..."
« on: April 19, 2017, 06:41:15 pm »
EDIT: I've managed to get the 64 bit compiler for my configuration working properly! Details in the posts below  :D


Hi all

Apologies in advance if this question seems a bit simple.  I wouldn't resort to forums unless I felt that I exhausted the abilities of my google-ing. I made an account just to ask about these errors and I really try to avoid doing this because google has usually gotten me through any problems I have with programming.
(Also sorry if my forum etiquette is lacking)

Yesterday I installed SFML for the first time and attempted to use MinGW to compile the tutorial example from the official SFML documentation(the example specifically for MinGW and CodeBlocks):

#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

However I consistently return these errors:

C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x7c): undefined reference to `sf::String::String(char const*, std::locale const&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0xa0): undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0xda): undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x10f): undefined reference to `sf::CircleShape::CircleShape(float, unsigned int)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x11f): undefined reference to `sf::Color::Green'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x126): undefined reference to `sf::Shape::setFillColor(sf::Color const&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x136): undefined reference to `sf::Window::isOpen() const'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x154): undefined reference to `sf::Window::pollEvent(sf::Event&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x172): undefined reference to `sf::Window::close()'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x19d): undefined reference to `sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x1b6): undefined reference to `sf::RenderTarget::clear(sf::Color const&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x1ca): undefined reference to `sf::RenderStates::Default'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x1da): undefined reference to `sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x1ea): undefined reference to `sf::Window::display()'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x20e): undefined reference to `sf::RenderWindow::~RenderWindow()'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text+0x256): undefined reference to `sf::RenderWindow::~RenderWindow()'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0xa): undefined reference to `vtable for sf::CircleShape'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0x14): undefined reference to `vtable for sf::CircleShape'
C:\Users\Charles\AppData\Local\Temp\cc8dA4M1.o:why.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0x24): undefined reference to `sf::Shape::~Shape()'
collect2.exe: error: ld returned 1 exit status
[Finished in 0.4s with exit code 1]

I am using this to attempt to compile (in Sublime Text 3):

{
    "cmd": ["g++", "-o", "${file_path}/${file_base_name}.exe", "-DSFML_STATIC", "-LC:/Coding/mingw32/mingw32/lib", "-IC:/Coding/mingw32/mingw32/include", "-lsfml-window", "-lsfml-graphics", "-lsfml-system", "-static-libgcc", "-static-libstdc++", "${file}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.cpp, source.c++",
    "path": "C:/Coding/mingw32/mingw32/bin",
    "shell": true,
    "variants": [
        {
            "name": "Run",
            "cmd": ["g++", "-o", "${file_path}/${file_base_name}.exe", "-static-libgcc", "-static-libstdc++", "*.cpp", "&", "${file_path}/${file_base_name}.exe"]
        }
    ]
}

This build was something I found while googling about the setup of SFML with MinGW. I've tailored it to suit my setup and outside of any attempts to create links to SFML libraries I have not had any issues compiling any other code.

I tried a few different installations of both SFML and MinGW both in 32 and 64bit. I experimented for several hours attempting to configure this via CMake using both the downloads available from the SFML site and the Git source with various installs of MinGW as well, to no avail. These attempts with CMake usually gave me even more errors on various different configurations and setups than without it.

I've tried various things with the linking order and commands in the build path as googling this issue pretty much suggested that I either was using incompatible 32 and 64bit versions of libraries and such or I had incorrect link orders. Ive tried the different 32 and 64 bit  variations with and without CMake, nothing really seems to yield any kind of progress. I tried linking every single /extlib dependency (despite the tutorial explaining it wasn't necessary for Windows users)including any necessary libraries from the Windows SDK 8.0 just to see if that was my problem and nothing changed.

My current setup is using the SFML 2.4.2 GCC 6.1.0 MinGW (DW2) - 32-bit from the SFML site with the MinGW version that is linked below it.  The PATH to the MinGW bin is set accordingly.
What I've done at the moment is simply copied the "SFML/include" and SFML's "/lib" to the MinGW installation and they appear to still be valid within the compilation but I cant build any code using actual SFML objects and classes etc.

I still consider myself a beginner at coding so I assume its very likely there's some blatant oversight that's beyond me that letting some more people look at for a few minutes would make apparent so any help will be greatly appreciated!
I know this question has been asked plenty of times before so I apologize if this seems like an unnecessary post but I'm clueless here.
« Last Edit: April 20, 2017, 05:50:33 am by Mooks »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: "Undefined reference to sf::..."
« Reply #1 on: April 19, 2017, 08:36:31 pm »
It could be the order of your inclusion. Try graphics before window.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Mooks

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: "Undefined reference to sf::..."
« Reply #2 on: April 19, 2017, 11:58:48 pm »
Thank for the response!

Unfortunately I have tested out several different orders for linking the libraries and they all yield more or less the same error.  The only difference appears to occur when I leave out linking to the SFML_STATIC, which leaves me with a very similar error, however "undefined references.." point to objects with a prefix of "__imp__", however its pretty obvious what the problem is there(if I don't include -DSFML_STATIC it tried to link to DLL's as opposed to the static libs.

It seems like I actually initially posted a slightly inccorect version of the build I'm using to compile (the mixed order for "windows and graphics" was me testing earlier and forgot to change it back.

It's supposed to look more like this:

{
    "cmd": ["g++", "-o", "${file_path}/${file_base_name}.exe", "-DSFML_STATIC", "-LC:/Coding/mingw32/mingw32/lib", "-IC:/Coding/mingw32/mingw32/include", "-lsfml-graphics", "-lsfml-window", "-lsfml-system", "-static-libgcc", "-static-libstdc++", "${file}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.cpp, source.c++",
    "path": "C:/Coding/mingw32/mingw32/bin",
    "shell": true,
    "variants": [
        {
            "name": "Run",
            "cmd": ["g++", "-o", "${file_path}/${file_base_name}.exe", "-DSFML_STATIC", "-LC:/Coding/mingw32/mingw32/lib", "-IC:/Coding/mingw32/mingw32/include", "-lsfml-graphics", "-lsfml-window", "-lsfml-system", "-static-libgcc", "-static-libstdc++", "*.cpp", "&", "${file_path}/${file_base_name}.exe"]
        }
    ]
}

Just to be sure, I do still get the exact same error in this instance:

C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x7c): undefined reference to `sf::String::String(char const*, std::locale const&)'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0xa0): undefined reference to `sf::VideoMode::VideoMode(unsigned int, unsigned int, unsigned int)'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0xda): undefined reference to `sf::RenderWindow::RenderWindow(sf::VideoMode, sf::String const&, unsigned int, sf::ContextSettings const&)'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x10f): undefined reference to `sf::CircleShape::CircleShape(float, unsigned int)'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x11f): undefined reference to `sf::Color::Green'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x126): undefined reference to `sf::Shape::setFillColor(sf::Color const&)'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x136): undefined reference to `sf::Window::isOpen() const'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x154): undefined reference to `sf::Window::pollEvent(sf::Event&)'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x172): undefined reference to `sf::Window::close()'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x19d): undefined reference to `sf::Color::Color(unsigned char, unsigned char, unsigned char, unsigned char)'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x1b6): undefined reference to `sf::RenderTarget::clear(sf::Color const&)'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x1ca): undefined reference to `sf::RenderStates::Default'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x1da): undefined reference to `sf::RenderTarget::draw(sf::Drawable const&, sf::RenderStates const&)'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x1ea): undefined reference to `sf::Window::display()'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x20e): undefined reference to `sf::RenderWindow::~RenderWindow()'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x256): undefined reference to `sf::RenderWindow::~RenderWindow()'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0xa): undefined reference to `vtable for sf::CircleShape'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0x14): undefined reference to `vtable for sf::CircleShape'
C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text$_ZN2sf11CircleShapeD1Ev[__ZN2sf11CircleShapeD1Ev]+0x24): undefined reference to `sf::Shape::~Shape()'
collect2.exe: error: ld returned 1 exit status
[Finished in 0.4s with exit code 1]

The only piece of information I left out is an addendum to the error that states some info about my system attempt to compile (build path location etc.) It is as follows:

[Finished in 0.4s with exit code 1]
[cmd: ['g++', '-o', 'B:\\Coding\\C++ projects\\Why/why.exe', '-DSFML_STATIC', '-LC:/Coding/mingw32/mingw32/lib', '-IC:/Coding/mingw32/mingw32/include', '-lsfml-graphics', '-lsfml-window', '-lsfml-system', '-static-libgcc', '-static-libstdc++', 'B:\\Coding\\C++ projects\\Why\\why.cpp']]
[dir: B:\Coding\C++ projects\Why]
[path: B:\Program Files (x86)\ImageMagick-7.0.5-Q16;C:\Program Files\ImageMagick-7.0.5-Q16;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\GnuWin32\bin;C:\Python35;C:\Program Files (x86)\GtkSharp\2.12\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit\;C:\Coding\mingw32\mingw32\bin;C:\Users\Charles\scoop\shims;C:\Users\Charles\AppData\Local\Programs\Python\Python35-32\Scripts\;C:\Users\Charles\AppData\Local\Programs\Python\Python35-32\;C:\Users\Charles\AppData\Local\Programs\Python\Python35\Scripts\;C:\Users\Charles\AppData\Local\Programs\Python\Python35\;C:\Users\Charles\AppData\Local\Programs\Python\Python35;C:\Python35\Scripts;C:\Python27;C:\Program Files\CMake\bin;C:\Coding\mingw32\mingw32\bin;]

All of that appears directly under the error in the console.
Any ideas? I am hoping theres just some glaring oversight that I failed to ensure here.

Mooks

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: "Undefined reference to sf::..."
« Reply #3 on: April 20, 2017, 12:11:22 am »
Something I noticed about the extra information after the error report:

It states above that my current working directory for the program to be compiled "B:/Coding/C++ Projects/Why" where as the libraries I am linking are clearly in a very different place and the error indicates that the undefined references are occurring in my "AppData/Local/Temp/(random string on text).cpp".
First line of error:

C:\Users\Charles\AppData\Local\Temp\ccL6ioMh.o:why.cpp:(.text+0x7c): undefined reference to `sf::String::String(char const*, std::locale const&)'  ETC.....


Compiler info after build attempt :


[cmd: ['g++', '-o', 'B:\\Coding\\C++ projects\\Why/why.exe'      ETC.....


Is there a kind of hierarchy that I am supposed to adhere to in this circumstance? To be clear I have actually attempted to change the working directory of "Why.cpp" but maybe there's a specific way which I'm meant to do this?

Totally unsure about this, I could be missing the mark completely and all of that has little or nothing to do with this error but I figured I'd be as clear about the things I've tried as possible.




EDIT: I cleared the temp folder and attempted to recompile and I returned the same error. Looking into the /Temp folder shows a folder from NVIDIA Corporation:


--NVIDIA Corporation
     ---NV_Cache
           ----5 Files (long giberish string names), 3 end with extension ".bin", and 2 with extension ".toc"


When I look at these in sublime they are blank. And in notepad++ I get the error "Can not open file "(File path)"...
I have just tried settings full permissions for users on the pc to access the temp folder and its subdirectories but that changed nothing.
« Last Edit: April 20, 2017, 01:01:02 am by Mooks »

Mooks

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: "Undefined reference to sf::..."
« Reply #4 on: April 20, 2017, 05:49:10 am »
Good news!

After doing what seemed like banging my head against a wall for hours I managed to produce the compiled exe that creates the tutorial example successfully.

As it turns out, pulling a random compile build off the web and tinkering with it with minimal knowledge was the problem. My non-working build looked like this:


{
    "cmd": ["g++", "-o", "${file_path}/${file_base_name}.exe", "-DSFML_STATIC", "-LC:/Coding/mingw32/mingw32/lib", "-IC:/Coding/mingw32/mingw32/include", "-lsfml-graphics", "-lsfml-window", "-lsfml-system", "-static-libgcc", "-static-libstdc++", "${file}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.cpp, source.c++",
    "path": "C:/Coding/mingw32/mingw32/bin",
    "shell": true,
    "variants": [
        {
            "name": "Run",
            "cmd": ["g++", "-o", "${file_path}/${file_base_name}.exe", "-DSFML_STATIC", "-LC:/Coding/mingw32/mingw32/lib", "-IC:/Coding/mingw32/mingw32/include", "-lsfml-graphics", "-lsfml-window", "-lsfml-system", "-static-libgcc", "-static-libstdc++", "*.cpp", "&", "${file_path}/${file_base_name}.exe"]
        }
    ]

While the above worked just fine for compiling code for a simple and quick console application (indeed that what I was looking for when I found this build on the web) that didn't apparently require any library linking, the order of some of the commands was incorrect.

My new build looks like this(There are a lot of edits here but I will point out the actual important change that allowed this all to work, I think):

{
    "cmd": ["g++", "${file}","C:/Coding/MinGW/mingw64/lib/libsfml-graphics.a", "C:/Coding/MinGW/mingw64/lib/libsfml-window.a", "C:/Coding/MinGW/mingw64/lib/libsfml-system.a",
    "-o", "${file_path}/${file_base_name}.exe",
    "-IC:/Coding/MinGW/mingw64/include",
    "-static-libgcc", "-static-libstdc++",],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.cpp, source.c++",
    "path": "C:/Coding/MinGW/mingw64/bin",
    "shell": true,
}

It looks a lot different but I believe most of what occurs when it compiles remains the same except you'll notice the position of "${file}" from the "cmd" line has been moved to the front of the command, right after "g++". Also you can see I specified exact pathways to all the libraries directly after that.

I figured this out from reading over the GCC command line documentations ( https://gcc.gnu.org/onlinedocs/gcc-3.0.4/gcc_3.html#SEC6 ) and discovered the process that is supposed to be occuring here;
- the command runs or looks for (im not sure which is more correct) g++
- it then see the source file "${file}"
- then the libraries corresponding to the source file follows
- "-o" is called which specifies to put the output to "${file_path}/${file_base_name}.exe" (the working directory for the source file)
- Then the preprocessor definitions follow "-I..."
- everything else is as it was after this point ( I removed the "run" variation because I didn't ever use it and it was cluttering)

So ultimately my problem came down to a not comprehending how to interact with the gcc compile commands!

You'll notice also that my MinGW directories have been renamed to "mingw64". I am now using the 64 bit versions of both MinGW and SFML.  Everything works as expected.

Now, I've done some experimenting with trying to additionally create a MinGW/SFML build using 32 bit installations. Following exactly the process which led me to finally building and running the tutorial example I was unable to reach the same results. The compiler builds the exe however at runtime a new error occurs:

 

"The procedure entry point
(gibberish string)
could not be located in the dynamic link library
C:/Coding/MinGW/mingw32/bin/sfml-graphics-2.dll"


Some tinkering with the build has yet to yield anything new. Some searching indicates that using two builds of SFML independently can cause errors like this where MinGW is pointing it resources to the wrong installation, however my assumption was that if i meticulously specified the exact locations of all the libraries and includes in the compiler it would know exactly where to look for everything.

Is this a limitation of what I'm trying to do? I'm running a 64 bit OS but I assumed it wouldn't have trouble compiling 32 bit programs. Could this be more related to how I set up MinGW? Similarly to earlier I've done many checks that my path variables have been set up properly. If it helps, my PATH looks like this:


C:\Users\Charles\scoop\shims;C:\Users\Charles\AppData\Local\Programs\Python\Python35-32\Scripts\;C:\Users\Charles\AppData\Local\Programs\Python\Python35-32\
;C:\Users\Charles\AppData\Local\Programs\Python\Python35\Scripts\;C:\Users\Charles\AppData\Local\Programs\Python\Python35\;
C:\Users\Charles\AppData\Local\Programs\Python\Python35;C:\Python35\Scripts;C:\Python27;C:\Program Files\CMake\bin;C:\Coding\MinGW\mingw32\bin;C:\Coding\MinGW\mingw64\bin;


As I mentioned my assumption was that MinGW would know where to look with all of these specifications, so any input would be appreciated as to why this might be happening.

Nonetheless like I've said I've got the 64bit build set up and running, so yay progress!  :D

 

anything