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

Author Topic: [Solved] Problem with Linker | error LNK2001: unresolved external symbol  (Read 2209 times)

0 Members and 1 Guest are viewing this topic.

shengelenge

  • Newbie
  • *
  • Posts: 2
    • View Profile
Hey,

I am new here and want to learn SFML. I installed SFML 2.6.1 Visual C++ 15 (2017) - 32-bit and Visual Studio 2022. I have completed SFML and Visual Studio tutorial and ended with a beautiful window with a green circle! So I thought I can move to another project from this book "Beginning C++ Game Programming - Second Edition". I have finished the first chapter, run the project with the same properties as before and get this error:

Build started at 1:00 PM...
1>------ Build started: Project: SpaceInvaders, Configuration: Debug Win32 ------
1>GameScreen.obj : error LNK2001: unresolved external symbol "public: static int WorldState::WORLD_HEIGHT" (?WORLD_HEIGHT@WorldState@@2HA)
1>GameScreen.obj : error LNK2001: unresolved external symbol "public: static int WorldState::SCORE" (?SCORE@WorldState@@2HA)
1>GameUIPanel.obj : error LNK2001: unresolved external symbol "public: static int WorldState::SCORE" (?SCORE@WorldState@@2HA)
1>GameScreen.obj : error LNK2001: unresolved external symbol "public: static int WorldState::LIVES" (?LIVES@WorldState@@2HA)
1>GameUIPanel.obj : error LNK2001: unresolved external symbol "public: static int WorldState::LIVES" (?LIVES@WorldState@@2HA)
1>GameScreen.obj : error LNK2001: unresolved external symbol "public: static int WorldState::NUM_INVADERS_AT_START" (?NUM_INVADERS_AT_START@WorldState@@2HA)
1>GameScreen.obj : error LNK2001: unresolved external symbol "public: static int WorldState::NUM_INVADERS" (?NUM_INVADERS@WorldState@@2HA)
1>C:\VSProjects\SpaceInvaders\Debug\SpaceInvaders.exe : fatal error LNK1120: 5 unresolved externals
1>Done building project "SpaceInvaders.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 1:00 PM and took 00.300 seconds ==========

 

I guess it has something to do with this file:

#pragma once
class WorldState {
public:
static const int WORLD_WIDTH = 100;
static int WORLD_HEIGHT;
static int SCORE;
static int LIVES;
static int NUM_INVADERS_AT_START;
static int NUM_INVADERS;
static int WAVE_NUMBER;
};
 

I attach some useful screenshots.

Not sure if that's the correct way to do it but I copied "include" and "lib" dirs from SFML to my SolutionDir (as you can see in the screenshots)
« Last Edit: September 24, 2024, 04:43:50 pm by shengelenge »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Problem with Linker | error LNK2001: unresolved external symbol
« Reply #1 on: September 24, 2024, 03:05:09 pm »
You need to define the static variable, it's not enough to just declare it in the header.

See the section about this here: https://www.learncpp.com/cpp-tutorial/static-member-variables/#:~:text=Defining%20and%20initializing%20static%20member%20variables
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

shengelenge

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problem with Linker | error LNK2001: unresolved external symbol
« Reply #2 on: September 24, 2024, 04:41:37 pm »
Yes, I found a solution. Thank you ;)