SFML community forums

Help => General => Topic started by: marmal102 on April 29, 2017, 10:22:15 am

Title: undefined references to... On Windows
Post by: marmal102 on April 29, 2017, 10:22:15 am
Hello everyone,

I have encountered a strange problem recently while trying to configure CLion with SFML on Windows.
I did it many times and it worked perfectly but now I am getting many problems such:

undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'

Whole list of that problems. I know it is the problem with linking but I don't know why.
SFML is detected by CMake, the same version of SFML works in Visual Studio.

FindSFML.cmake is original from SFML package downloaded.

Here is my CMakeLists.txt:

Code: [Select]
cmake_minimum_required(VERSION 3.7)
project(untitled)

set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
set(SFML_ROOT "D:/Development/SFML-2.4.2")
add_executable(untitled ${SOURCE_FILES})

set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules")
find_package(SFML REQUIRED COMPONENTS audio graphics window system)
if (SFML_FOUND)
    include_directories(${SFML_INCLUDE_DIR})
    target_link_libraries(untitled ${SFML_LIBRARIES})
endif()

Title: Re: undefined references to... On Windows
Post by: sjaustirni on April 29, 2017, 10:32:54 am
EDIT: This is not the cause of the error. See the post by eXpl0it3r.

You're not linking main (https://www.sfml-dev.org/tutorials/2.4/start-vc.php).

Personally, I use this setup for cross-platform linking
if(MSVC) # Hide console window on Windows
    set_target_properties(${EXECUTABLE_NAME} PROPERTIES  LINK_FLAGS "/SUBSYSTEM:WINDOWS")
    find_package(SFML 2 REQUIRED main system window graphics network audio)
else() # Linux doesn't need the special care
    find_package(SFML 2 REQUIRED system window graphics network audio)
endif(MSVC)

if(SFML_FOUND)
  include_directories(${SFML_INCLUDE_DIR})
  target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
Title: Re: undefined references to... On Windows
Post by: eXpl0it3r on April 29, 2017, 11:37:25 am
The CMake script looks fine. So my guess is that your compiler isn't matching your SFML libs.
What's your exact compiler and where did you get SFML from?

You're not linking main (https://www.sfml-dev.org/tutorials/2.4/start-vc.php).
While that is a true statement, it has nothing to do with the linker error. ;)

Also the recommended way to set the subsystem is to use the follow code:
add_executable(name WIN32 ${SOURCES})
With that code in place the check can be changed from MSVC to Windows as it will also work with MinGW.
Title: Re: undefined references to... On Windows
Post by: sjaustirni on April 29, 2017, 01:02:59 pm
You're right, I have just seen the combo "windows linking failing" and "sfml-main not linked" and assumed the wrong conclusion.

Thanks for the comment about setting the window subsystem. I will implement it once I boot Windows again. I am glad I haven't taken it out as an irrelevant piece of code as you would have not made the comment then :D
Title: Re: undefined references to... On Windows
Post by: marmal102 on April 29, 2017, 01:30:03 pm
But I wrote I am using CLion with MinGW.
GCC Version is 5.3.0.
Title: Re: undefined references to... On Windows
Post by: eXpl0it3r on April 29, 2017, 07:19:30 pm
MinGW GCC 5.3.0 can be a lot. There are many different variations of MinGW, thus you need to be more specific. Where did you download said MinGW version?
Title: Re: undefined references to... On Windows
Post by: marmal102 on April 30, 2017, 11:11:27 am
I got it from here: http://www.mingw.org/category/wiki/download
Then I downloaded Installer from: https://sourceforge.net/projects/mingw/files/Installer/
And installed gcc compiler from here.
Title: Re: undefined references to... On Windows
Post by: eXpl0it3r on April 30, 2017, 12:35:41 pm
Then you'll have to build SFML yourself as we don't provide any binaries for that MinGW version. See the links on the download page if you want to get a matching compiler.
Title: Re: undefined references to... On Windows
Post by: marmal102 on April 30, 2017, 03:40:46 pm
Yes... I really forgot about matching compiler to downloaded SFML..
Shame on me :) Thanks, works well.