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

Author Topic: Trouble linking libraries with Cmake  (Read 7176 times)

0 Members and 1 Guest are viewing this topic.

jakobnator

  • Newbie
  • *
  • Posts: 8
    • View Profile
Trouble linking libraries with Cmake
« on: November 21, 2016, 11:21:56 pm »
I am having issues trying to get SFML to work using Cmake. I have no experience with cmake, but for the past 24 hours have been trying to follow a scatter of different tutorials for different systems and forum posts but no clear setup on how to get an already compiled version of SFML to work with Clion. What I do know is that I need to setup FindSFML.cmake file inside the project directory along with a Cmakelist.txt . I think the problem is somewhere in my Cmakelist.txt because the FindSFML.cmake is just cut and paste from GitHub. I already tried the github tutorial but it was a complete mess of other problems with trying to build SFML from source. Here is what I have done so far that I can remember.


(Using Windows 10 64bit, Clion, and cmake)
1.Download latest SFML version (64-bit Windows GCC MinGW)
2.Create a new Clion project with a directory looking like this:https://i.imgur.com/K0BwyHM.png
3.Have this basic main.cpp
#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;
}
 

4. With this Cmakelist.txt

Code: [Select]
cmake_minimum_required(VERSION 3.2)
project(SFMLdemo)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SFML_ROOT "C:/SFML-2.4.1")
set(SOURCE_FILES main.cpp)
add_executable(SFMLdemo ${SOURCE_FILES})

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

The compiler is giving me (what I believe) can't find SFML error of:
Code: [Select]
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:5: undefined reference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:5: undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:5: undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:6: undefined reference to `_imp___ZN2sf11CircleShapeC1Efj'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:7: undefined reference to `_imp___ZN2sf5Color5GreenE'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:7: undefined reference to `_imp___ZN2sf5Shape12setFillColorERKNS_5ColorE'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:9: undefined reference to `_imp___ZNK2sf6Window6isOpenEv'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:12: undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:15: undefined reference to `_imp___ZN2sf6Window5closeEv'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:18: undefined reference to `_imp___ZN2sf5ColorC1Ehhhh'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:18: undefined reference to `_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:19: undefined reference to `_imp___ZN2sf12RenderStates7DefaultE'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:19: undefined reference to `_imp___ZN2sf12RenderTarget4drawERKNS_8DrawableERKNS_12RenderStatesE'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:20: undefined reference to `_imp___ZN2sf6Window7displayEv'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:5: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
C:/Users/Jakob/ClionProjects/SFMLdemo/main.cpp:5: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
CMakeFiles\SFMLdemo.dir/objects.a(main.cpp.obj): In function `ZN2sf11CircleShapeD1Ev':
C:/SFML-2.4.1/include/SFML/Graphics/CircleShape.hpp:41: undefined reference to `_imp___ZTVN2sf11CircleShapeE'
C:/SFML-2.4.1/include/SFML/Graphics/CircleShape.hpp:41: undefined reference to `_imp___ZTVN2sf11CircleShapeE'
C:/SFML-2.4.1/include/SFML/Graphics/CircleShape.hpp:41: undefined reference to `_imp___ZN2sf5ShapeD2Ev'

My apologizes I'm sure you guys get a lot of people on this forum having trouble getting SFML started. I really didn't want to post this but I am very lost with Cmake and the only tutorial online I can find seemed vague about the cmakelist.txt part.

Thank you for your time,
-jakobnator
« Last Edit: November 21, 2016, 11:23:33 pm by jakobnator »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Trouble linking libraries with Cmake
« Reply #1 on: November 22, 2016, 01:49:34 am »
Your order in the find_package macro is wrong. It needs to be audio, network, graphics, window, system.

I usually just copy my CMake code together from other CMake files or old ones of mine. Feel free to take a look at my projects (might not be the cleanest/best): https://github.com/eXpl0it3r/game-off-2016
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jakobnator

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Trouble linking libraries with Cmake
« Reply #2 on: November 22, 2016, 04:09:03 am »
Thank you for your help! I tried switching the order of the find_package() and now it just says
Code: [Select]
Error:Could NOT find SFML (missing: SFML_WINDOWS_LIBRARY)(not when compiling just when its rechecking the cmakelist file)

When I tried modifying your Cmakelist.txt I got a bunch of undefined reference errors ( still can't locate SFML?)
Code: [Select]
CMakeFiles\SFMLdemo.dir/objects.a(main.cpp.obj):main.cpp:(.text.startup+0x84): undefined reference to `sf::String::String(char const*, std::locale const&)'
Here is what I tried to do with your code, not very familiar with cmake syntax so it might be incorrect.

Code: [Select]
cmake_minimum_required(VERSION 3.2)
project(SFMLdemo)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")

# Set default build type explicitly to Release
if (NOT CMAKE_BUILD_TYPE)
    set (CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build type." FORCE)
endif (NOT CMAKE_BUILD_TYPE)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/)

if(NOT SFML_ROOT)
    set(SFML_ROOT "C:/SFML-2.4.1")
endif()

# Find SFML
set(SFML_STATIC_LIBRARIES TRUE)
find_package(SFML REQUIRED COMPONENTS audio graphics window system)

if(NOT SFML_FOUND)
    message(FATAL_ERROR "SFML couldn't be located!")
endif()


include_directories(${SFML_INCLUDE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

set(SOURCE_FILES main.cpp)

add_executable(SFMLdemo ${SOURCE_FILES})

target_link_libraries(SFMLdemo ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})

# Install executable
install(TARGETS SFMLdemo
        RUNTIME DESTINATION .
        )

# Install game data
install(DIRECTORY bin/res
        DESTINATION .
        )

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Trouble linking libraries with Cmake
« Reply #3 on: November 22, 2016, 08:59:39 am »
I generally link SFML static, as such you need to make sure that you have the static SFML libs. If you haven't built SFML yourself with the SFML_STATIC_STD_LIB option set, then you should remove the line that adds -static to CXX_FLAGS.
« Last Edit: November 23, 2016, 09:36:42 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jakobnator

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Trouble linking libraries with Cmake
« Reply #4 on: November 23, 2016, 04:17:21 am »
Ok I went back and built SFML myself with the static options now my Cmake file is saying that it can't find the dependencies...

Code: [Select]
Error:SFML found but some of its dependencies are missing ( FreeType libjpeg OpenAL Ogg Vorbis VorbisFile VorbisEnc FLAC)
Is there a flag in cmake I am missing? Or is there a variable in cmakelist that I need to find the external dependencies?

EDIT: Nevermind I found it I had to point to the /install/ directory of my SFML folder based on the tutorial.

MAN was that a pain, I got it working now but I know very little on the in betweens of programming with cmake....

For future people trying to get Clion to work with SFML let me make it as cut and dry as possible.....

1. Download SFML source and put it in a folder.
2. Follow THIS:https://github.com/SFML/SFML/wiki/Tutorial:-Build-SFML-with-a-MinGW-Compiler
Cmake should look like this before generating makefiles->https://i.imgur.com/EHetsLR.png
3. Create a new Clion project and inside the project folder make another folder named cmake_modules and inside that create a file named FindSFML.cmake.
4. Inside FindSFML.cmake copy and paste the code for it found on github (The file is FindSFML.cmake)
5.Use the generic green circle code in the other tutorials for your main.cpp just to see if it's working.
6.Create another file inside your project folder named CMakeList.txt and post this code inside it
Code: [Select]
cmake_minimum_required(VERSION 3.2)
project(SFMLdemo)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -static")

# Set default build type explicitly to Release
if (NOT CMAKE_BUILD_TYPE)
    set (CMAKE_BUILD_TYPE "Debug" CACHE STRING "Choose the type of build type." FORCE)
endif (NOT CMAKE_BUILD_TYPE)

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake_modules/)

if(NOT SFML_ROOT)
    set(SFML_ROOT "C:/SFML-2.4.1-build/install")
endif()

# Find SFML
set(SFML_STATIC_LIBRARIES TRUE)
find_package(SFML REQUIRED COMPONENTS audio graphics window system)

if(NOT SFML_FOUND)
    message(FATAL_ERROR "SFML couldn't be located!")
endif()


include_directories(${SFML_INCLUDE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

set(SOURCE_FILES main.cpp)

add_executable(SFMLdemo ${SOURCE_FILES})

target_link_libraries(SFMLdemo ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})

Anywhere you see SFMLDemo replace with your project name, and change the SFML_ROOT to your SFML root directory.
Boom. done.

Looks easy after doing it but a lot can go wrong. I might never fully understand why you can't just have a text file that states where SFML is, debug or release, and static or dyanmic linking. But I guess that's what cmake is, still not sure whats going on with the hiearchy of .cmake and .txt files either.... /noob rant

Thanks for the help though!

« Last Edit: November 23, 2016, 04:37:34 am by jakobnator »