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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Mooks

Pages: [1]
1
General / Re: class variables reset value after function loop
« on: June 27, 2017, 12:40:49 am »
I've figured out a method to get the results I was after! I'll edit the original post.

Thanks again  :)

2
General / Re: class variables reset value after function loop
« on: June 27, 2017, 12:22:15 am »
Thanks for your response!

Testing the code without the resourcesSetter() in drawable_objects works! Seems I made a really basic error there. Now that cityFood isnt being reset I can incrimentin the loop. Overnight I made a few edits to the code and (hopefully) eliminated some of the errors you pointed out.

I entirely removed the drawable_objects functions (perhaps just for now) and moved the object declarations and definitions to overworldTime(), so I could test out incrimenting the cityFood of each object respectively.

I'm thinking that in order to get the behaviour I want out of my objects I need to have the objects properly scoped, and as it is putting them inside overworldTime() works great for changing the data inside the loop but the objects are destructed once the scope ends and then reconstructed when it begins again ( I think thats whats happening). Thus I am still essentially having the same issue, although it is clearer now what the problem is. If I could prevent the destructor from occuring OR chose a better place to declare and define my objects I believe I can get the results I want however all the methods I've tested dont seem to work; one way or another at some point the objects are destroyed and recreated and they loose they modified values.

This is what overworldTime() looks like now:

void overworldTime() {
   
        overworldLocations cityA;
        overworldLocations cityB;
        cityB.overworldCity({(float)windowSizeX/8, (float)windowSizeY/8}, "My Second City");
        cityA.overworldCity({(float)windowSizeX/3, (float)windowSizeY/3}, "My First City");
 
    sf::Time wholeDay = sf::seconds(1.0f);
    sf::Time timePassed = overworldClock.getElapsedTime();
   
    if(timePassed > wholeDay) {
        std::cout
                << "OVERWORLD TIME "
                << timePassed.asSeconds()
                << std::endl;

        overworldClock.restart();

        if(!timePaused) {

            if(currentDay >= 7) {
                    currentDay = 0;
                    cityA.cityFood +=1;
                    cityB.cityFood += 5;

             std::cout
                << "CFA is "
                << cityA.cityFood
                << std::endl;
             std::cout
                << "CFB is "
                << cityB.cityFood
                << std::endl;
            }

            currentDay ++;
                std::cout
                    << "Day "
                    << currentDay
                    << std::endl;
        }

    }
       
        std::string currentDayString = std::to_string(currentDay);

        text_function overworldTextObject("Day:", overworldFont, {overworldObjectPosX, overworldObjectPosY}, sf::Color::Red, 24);
        text_function overworldTextCurrentDay(currentDayString, overworldFont, overworldCurrentDayObjectPos, sf::Color::Red, 24);

};
 

As I mentioned, its pretty clear what the problem is but I do not really have ideas for solutions. I've tried declaring and defining the objects in different  places in the code, ultimately that doesnt get the results I want because declaring them outside of the scope of overworldTime() means that overworldTime() cant interact with the member variables.

Here are the full changes I've made to overworldLocations and gameTime they are mostly the same other than changing some of the global variables and chaing overworldTIme to no longer take a reference to overworldLocations::cityFood and some other suggestions you had:

#include "text_function.hpp"

class overworldLocations {

    private:

int cityFood, cityIndustry, cityPopulation;

    public:

void overworldCity(sf::Vector2f v, std::string s);        
friend class gameTime;

};

void overworldLocations::overworldCity(sf::Vector2f cityPos, std::string cityName) {

        float citySizeX{100}, citySizeY{100};
       
        sf::RectangleShape cityShape(sf::Vector2f(citySizeX, citySizeY));
        cityShape.setPosition(cityPos);
        window.draw(cityShape);
       
        sf::Vector2f cityPosition{cityShape.getPosition()};
        float x = cityPosition.x;
        float y = cityPosition.y;
        sf::Vector2f cityPosVector{cityPosition.x + 25, cityPosition.y + 80};

        std::string foodString = std::to_string(this->cityFood), industryString = std::to_string(this->cityIndustry), populationString = std::to_string(this->cityPopulation);

        text_function textObject(cityName, cityFont, cityPosVector, sf::Color::Black, 10);

        text_function cityTextObject1("F", cityFont, {cityPosition.x, cityPosition.y - 15}, sf::Color::Green, 12);
        text_function cityResourceText1(foodString, cityFont, {cityPosition.x + 7, cityPosition.y - 15}, sf::Color::Green, 12);

        text_function cityTextObject2("I", cityFont, {cityPosition.x + 40, cityPosition.y - 15}, sf::Color::Red, 12);
        text_function cityResourceText2(industryString, cityFont, {cityPosition.x + 47, cityPosition.y - 15}, sf::Color::Red, 12);

        text_function cityTextObject3("P", cityFont, {cityPosition.x + 80, cityPosition.y - 15}, sf::Color::Yellow, 12);
        text_function cityResourceText3(populationString, cityFont, {cityPosition.x + 87, cityPosition.y - 15}, sf::Color::Yellow, 12);
       
};
 

#include "overworldLocations.hpp"
#include "drawable_objects.hpp"

class gameTime {

        private:

static int currentDay;

sf::Text overworldText, overworldCurrentDay;
float overworldObjectPosX{(float)windowSizeX/windowSizeX}, overworldObjectPosY{(float)windowSizeY - 30};
sf::Vector2f overworldCurrentDayObjectPos{(float)windowSizeX/windowSizeX + 50, (float)windowSizeY - 30};

        public:

static  bool timePaused;
int gtfoodCount;
friend class overworldLocations;


void overworldTime() {

        overworldLocations cityA;
        overworldLocations cityB;
        cityB.overworldCity({(float)windowSizeX/8, (float)windowSizeY/8}, "My Second City");
        cityA.overworldCity({(float)windowSizeX/3, (float)windowSizeY/3}, "My First City");
 
    sf::Time wholeDay = sf::seconds(1.0f);
    sf::Time timePassed = overworldClock.getElapsedTime();
   
    if(timePassed > wholeDay) {
        std::cout
                << "OVERWORLD TIME "
                << timePassed.asSeconds()
                << std::endl;

        overworldClock.restart();

        if(!timePaused) {

            if(currentDay >= 7) {
                    currentDay = 0;
                    cityA.cityFood +=1;
                cityB.cityFood += 5;

             std::cout
                << "CFA is "
                << cityA.cityFood
                << std::endl;
             std::cout
                << "CFB is "
                << cityB.cityFood
                << std::endl;
            }

            currentDay ++;
                std::cout
                    << "Day "
                    << currentDay
                    << std::endl;
        }

    }
       
        std::string currentDayString = std::to_string(currentDay);

        text_function overworldTextObject("Day:", overworldFont, {overworldObjectPosX, overworldObjectPosY}, sf::Color::Red, 24);
        text_function overworldTextCurrentDay(currentDayString, overworldFont, overworldCurrentDayObjectPos, sf::Color::Red, 24);

};

};

bool gameTime::timePaused = true;
int gameTime::currentDay{1};
 

Your advice is greatly appreciated! I'm sure I'm still doing things wrong all over the place so any corrections you have for my project are definitely taken to heart.

3
General / [SOLVED]class variables reset value after function loop
« on: June 26, 2017, 12:01:40 am »
SOLUTION: It seems I was being really rigid in my idea of how to declare and define the objects I was working with. I was declaring and defining the objects generally in the same scope. The solution I found was to declare the objects outside the scope of the function that I wanted to change the objects' value and then make calls to that objects member functions (thus defining them, I think) inside the function in question. I hope that explains it.

What it looks like:
overworldTime() previously looked like this:

 

class gameTime {

void overworldTime() {
        overworldLocations cityA;   //objects are declared //
        overworldLocations cityB;
        cityB.overworldCity({(float)windowSizeX/8, (float)windowSizeY/8}, "My Second City");
        cityA.overworldCity({(float)windowSizeX/3, (float)windowSizeY/3}, "My First City");
        // ..and objects are defined right away //
// do stuff //

}; // end gameTime
 

now is looks like this:


overworldLocations cityA;   //objects are declared outside of gameTime //
overworldLocations cityB;

class gameTime {

void overworldTime() {
       
        cityB.overworldCity({(float)windowSizeX/8, (float)windowSizeY/8}, "My Second City");
        cityA.overworldCity({(float)windowSizeX/3, (float)windowSizeY/3}, "My First City");
        // ..and they are still defined in the same place //

// do stuff //

}; // end gameTime
 

/edit


This is more of a general C++ question I believe, but because I'm using SFML to work on this project I figured I'd ask around for help here before going elsewhere.

I have a set of classes and I'd like for their variables to be shared. Easy enough, however I have a function within one of the classes that is meant to incriment the value of the shared variable. In my logger I note that the incriment occurs once and then reverts to the initial value, and this process just repeats itself.

The goal is to get cityFood which is defined in class overworldLocations to be incrimented in the loop in overworldTime() which is a function of class gameTime.

Here are the two classes ( I'm sure there are errors everywhere, but everthing works aside from this one problem) overworldLocations:

#include "text_function.hpp"
#include <memory>

unsigned int cityPosX{(unsigned int)windowSizeX/3}, cityPosY{(unsigned int)windowSizeY/3};
sf::Font font, cityFont;

class overworldLocations {
       
       

public:
        static int cityFood, cityIndustry, cityPopulation;
        friend class gameTime;
       
int resourceSetter(int a, int b, int c) {

        cityFood = a;
        cityIndustry = b;
        cityPopulation = c;
       
}

int resourceGetter() {

        return cityFood, cityIndustry, cityPopulation;
}

void overworldCity(sf::Vector2f cityPos, std::string cityName) {

        float citySizeX{100}, citySizeY{100};

        sf::RectangleShape cityShape(sf::Vector2f(citySizeX, citySizeY));
        cityShape.setPosition(cityPos);
        window.draw(cityShape);
       
        sf::Vector2f cityPosition{cityShape.getPosition()};

        float x = cityPosition.x;
        float y = cityPosition.y;

        sf::Vector2f cityPosVector{cityPosition.x + 25, cityPosition.y + 80};
        // *foodPointer = cityFood;

       
       
        std::string foodString = std::to_string(cityFood), industryString = std::to_string(cityIndustry), populationString = std::to_string(cityPopulation);

        text_function textObject(cityName, cityFont, cityPosVector, sf::Color::Black, 10);

        text_function cityTextObject1("F", cityFont, {cityPosition.x, cityPosition.y - 15}, sf::Color::Green, 12);
        text_function cityResourceText1(foodString, cityFont, {cityPosition.x + 7, cityPosition.y - 15}, sf::Color::Green, 12);

        text_function cityTextObject2("I", cityFont, {cityPosition.x + 40, cityPosition.y - 15}, sf::Color::Red, 12);
        text_function cityResourceText2(industryString, cityFont, {cityPosition.x + 47, cityPosition.y - 15}, sf::Color::Red, 12);

        text_function cityTextObject3("P", cityFont, {cityPosition.x + 80, cityPosition.y - 15}, sf::Color::Yellow, 12);
        text_function cityResourceText3(populationString, cityFont, {cityPosition.x + 87, cityPosition.y - 15}, sf::Color::Yellow, 12);
       
};
};

 int overworldLocations::cityFood{10}, overworldLocations::cityIndustry{10}, overworldLocations::cityPopulation{10};

 

 and gameTime:
#include "overworldLocations.hpp"
#include "drawable_objects.hpp"

sf::Font overworldFont;
bool timePaused = true;


sf::Clock overworldClock;
sf::Text overworldText, overworldCurrentDay;
float overworldObjectPosX{(float)windowSizeX/windowSizeX}, overworldObjectPosY{(float)windowSizeY - 30};
sf::Vector2f overworldCurrentDayObjectPos{(float)windowSizeX/windowSizeX + 50, (float)windowSizeY - 30};

class gameTime : public overworldLocations {

public:

static int currentDay;

friend class overworldLocations;

void overworldTime(int& reference) {

        sf::Time wholeDay = sf::seconds(1.0f);
        sf::Time timePassed = overworldClock.getElapsedTime();
       
        if(timePassed > wholeDay) {

                std::cout
                        << "OVERWORLD TIME "
                        << timePassed.asSeconds()
                        << std::endl;

                overworldClock.restart();

                if(!timePaused) {

                        if(currentDay >= 7) {
                                currentDay = 0;
                                overworldLocations::cityFood += 1;
                        std::cout
                                << "OL CF is "
                                << overworldLocations::cityFood
                                << std::endl;
                        std::cout
                                << "GT CF is "
                                << cityFood
                                << std::endl;
                        std::cout
                                << "reference to cityFood is "
                                << reference
                                << std::endl;
                        }


                        // std::cout
                        //      << "shared_ptr & is "
                        //      << &sharedfoodPointer
                        //      << std::endl;
                        // std::cout
                        //      << "shared_ptr * is "
                        //      << *sharedfoodPointer
                        //      << std::endl;
                        // std::cout
                        //      << "shared_ptr is "
                        //      << sharedfoodPointer
                        //      << std::endl;

                        }

                         
                        currentDay ++;
                        std::cout
                                << "Day "
                                << currentDay
                                << std::endl;

        }
       
overworldLocations::cityFood += 1;
        std::string currentDayString = std::to_string(currentDay);

        text_function overworldTextObject("Day:", overworldFont, {overworldObjectPosX, overworldObjectPosY}, sf::Color::Red, 24);
        text_function overworldTextCurrentDay(currentDayString, overworldFont, overworldCurrentDayObjectPos, sf::Color::Red, 24);

};


};

int gameTime::currentDay{1};

 

and the main.cpp:
#include <SFML/System.hpp>
#include <SFML/WIndow.hpp>
#include <SFML/Graphics.hpp>
#include "include/window.hpp"
#include <iostream>
#include <thread>
#include <string>
#include "include/eventPoll.hpp"


int main() {

if (!cityFont.loadFromFile("impact.ttf")) {

                std::cout
                        << "error loading font";

                }
if (!overworldFont.loadFromFile("impact.ttf")) {

                std::cout
                        << "error loading font";

                }
if (!font.loadFromFile("impact.ttf")) {

                std::cout
                        << "error loading font";

                }
 
   
        while(true) {

                window.clear(black);

                gameTime gameClock;
                gameClock.overworldTime(overworldLocations::cityFood);

                character();

                drawable_objects();

               

                eventPoll();

                window.display();
               
                if(isClosed == true) {

                        break;
                }
       
        }

        return 0;

} //main()  

 

There is more I can put here if its neccesary but I believe my problem lies within these bits of code.

I can achieve the desired effect by making cityFood global - but alot of information suggests that is bad practice, and theres obviously something fundemental I'm missing so I'd like to learn how to get the results I want without using globals. The last thing I tried was passing by reference, as you can see in the overworldTime() function but I did not notice any change.

Any help would be appreciated, I hope I provided enough information.

EDIT: I forgot to add the code where I make instances of the overworldLocations class. I have those objects in a function called drawable_objects which is called in main :
void drawable_objects() {

        overworldLocations cityA, cityB;
        cityA.resourceSetter(10,10,10);
        cityB.resourceSetter(10,10,10);
    cityB.overworldCity({(float)windowSizeX/8, (float)windowSizeY/8}, "My Second City");
        cityA.overworldCity({(float)windowSizeX/3, (float)windowSizeY/3}, "My First City");

}

 

4
Graphics / Re: "Undefined reference to sf::..."
« 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

5
Graphics / Re: "Undefined reference to sf::..."
« 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.

6
Graphics / Re: "Undefined reference to sf::..."
« 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.

7
Graphics / "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.

Pages: [1]
anything