Missing sfml libraries for cmake - c++

C++ newbie here, I'm trying to implement SFML library for CLion (Win 10).
I followed this http://www.sfml-dev.org/tutorials/2.0/compile-with-cmake.php guidelines and installed sfml using cmake gui and mingw.
Now when I try to include SMF libraries to project in CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(untitled2)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(untitled2 ${SOURCE_FILES})
set(CMAKE_MODULE_PATH "C:/Program Files (x86)/SFML/cmake/Modules"${CMAKE_MODULE_PATH})
set(SFML_STATIC_LIBRARIES TRUE)
find_package(SFML COMPONENTS graphics window system REQUIRED)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(First ${SFML_Libraries})
Howewer, i'm getting an error
CMake Error at C:/Program Files (x86)/SFML/cmake/Modules/FindSFML.cmake:355 (message):
SFML found but some of its dependencies are missing ( FreeType libjpeg)
Call Stack (most recent call first):
CMakeLists.txt:13 (find_package)
But I thought those libs are included in Windows. I am doing something wrong? Where should I place FreeType and libjpeg files if I were to download them?

You shouldn't set any local system specific things (like paths) in your CMakeLists.txt. This defeats the whole purpose of a build system.
First, I'd suggest you create a sub directory "cmake" in your project and copy SFML's FindSFML.cmake module in there.
Then update your CMAKE_MODULE_PATH accordingly:
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
This will ensure the module is found. CMake's default modules are always found, you don't have to preserve the previous value of the variable (should typically be empty anyway).
Next, when creating your actual project files, you'll just have to pass SFML_ROOT to CMake, for example:
cmake "-DSFML_ROOT=C:/Program Files (x86)/SFML" path/to/source
In a similar way, you shouldn't set SFML_STATIC_LIBRARIES in your CMakeLists.txt unless you're somehow forced to use static libraries. So I'd actually add that to the command line as well:
cmake "-DSFML_ROOT=C:/Program Files (x86)/SFML" -DSFML_STATIC_LIBRARIES=TRUE path/to/source
You only have to set these once, they're stored in your CMakeCache.txt file later.
Last but not least, if you'd like to support static linking with SFML, you'll have to link SFML's dependencies as well:
target_link_libraries(First ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
Note that all these won't fix the error message you're getting. For some reason SFML's prebuilt dependencies aren't in C:\Program Files (x86)\SFML\lib. Are you sure you've got the correct prebuilt package?

Related

C++ How to run programs in Clion when you need to include OpenGL libraries?

Hello I need to work with OpenGL and want to create my project in Clion. But Clion cannot compile and run my projects because of the libraries I need to include. I can create my own makefile and run the program in terminal, but I want to do it in the IDE. How can I make this happen?
First make sure you installed all libraries correctly using the compiler you configured in clion/cmake. Assuminf you have a fresh CMakeLists.txt like
cmake_minimum_required(VERSION 3.3.2)
project(MyGL CPP)
add_executable(demo-run main.cpp)
For linking your libraries you need two things. First tell the compiler where to find the include files and second which libraries to link. You could just hard code you local installation like
target_link_libraries(demo-run path/to/glfw.lib path/to/opengl.lib path/to/jpeg.lib ...)
target_include_directories(demo-run PRIVATE path/to/glfw/include path/to/opengl/include path/to/jpeg/include ...)
however this is not very portable and if you want to work with another compiler or on another machine your project file will fail. Instead you can use the package system of cmake
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
find_package(JPEG REQUIRED)
find_package(GLEW REQUIRED)
find_package (OpenGL REQUIRED)
find_package (GLM REQUIRED)
target_link_libraries(demo-run ${GLFW_LIBRARIES} ${GLEW_LIBRARIES} ${JPEG_LIBRARIES} ${OPENGL_LIBRARIES})
target_include_directories(demo-run PRIVATE ${GLFW_INCLUDE_DIRS} ${GLEW_INCLUDE_DIR} ${JPEG_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${GLM_INCLUDE_DIR})
The glfw part is a bit tricky and works only on linux i guess see http://www.glfw.org/docs/3.0/build.html.
This code is not tested at all and you may need to specify some enviroment variables so cmake can find the packages or provide additional find scripts like https://github.com/lighttransport/nanogi/blob/master/cmake/FindGLM.cmake.
I would recommend to use the CMake build tool which does the work generating Makefiles for you and is also directly supported by clion. When you open the directory containing a CMakeLists.txt (CMake Project File) with clion, it should be automatically be loaded and compiled (if not just hit build)
A very simple example CMake project would look like this
cmake_minimum_required (VERSION 2.8.9)
project (OpenGl-Stuff)
include_directories(src)
add_executable(your-binary src/your-code.c src/your-code.h)
target_link_libraries(your-binary opengl)
# target_link_libraries will search for libopengl on standard system paths,
# maybe the library is not called libopengl, then you have to adjust the name above
this cmake project will generate the binary for you and link it against opengl

Setup CMake with SFML in VS2017

Just like in CLion I want to use SFML with Visual Studio 2017, but I'm still learning cmake and I don't know the commands or the logic of how cmake works at all. I've just seen some posts and got this litle script.
Note: I downloaded the latest version of sfml in the link provided, I just taked the extrated directory and put alongside CMakeLists.txt in my folder
#sets up the minimum version of cmake
cmake_minimum_required(VERSION 3.9)
#how the project will be called
project (space_impact)
#set c++11 standard
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" -std=c++11)
#set source files
set (SOURCE_FILES main.cpp)
#we add the executable of the program
add_executable (space_impact ${SOURCE_FILES})
#taked from a mac-clion tutorial, doesn't work
set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/SFML/cmake-modules/")
find_package (SFML REQUIRED system window graphics network audio)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(space_impact ${SFML_LIBRARIES})
endif()
that thing gave me errors:
Error CMake Error at SFML/cmake-modules/FindSFML.cmake:355 (message):
Could NOT find SFML (missing: SFML_SYSTEM_LIBRARY SFML_WINDOW_LIBRARY
SFML_GRAPHICS_LIBRARY SFML_NETWORK_LIBRARY SFML_AUDIO_LIBRARY) SFML/cmake-modules/FindSFML.cmake
I want everything to be dynamic, but I don't know how can I do that..
So my question is what should I do for setting up correctly SFML with Cmake in Visual Studio.
I don't want the old-fashioned method from the official website
UPDATE
Here's my location....
The thing is.. the FindSFML.cmake script it's not working...
What files should I move for make it working?
Your script is perfectly fine, except three things I'd change:
Move the whole module detection before defining targets. I'm pretty sure you also have to define your include directories before.
Your if(SFML_FOUND) bracket is pretty pointless right now, because you've set SFML to be required, which means it will never get past find_package() unless it's found.
-std=c++11 is a GCC only flag (MSVC will always use the latest standard, unless specified). As such you'll have to check the compiler here or use CMAKE_CXX_STANDARD.
So the "cleaned" CMakeLists.txt could look like this:
#sets up the minimum version of cmake
cmake_minimum_required(VERSION 3.9) # personally I'd set this version as low as required; you don't have to require the cutting edge version
#how the project will be called
project (space_impact)
#set the C++ standard to be used
set (CMAKE_CXX_STANDARD 11)
#set source files
set (SOURCE_FILES main.cpp)
#look for SFML and add it
set (CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/SFML/cmake-modules/")
find_package (SFML REQUIRED system window graphics network audio)
include_directories(${SFML_INCLUDE_DIR})
#we add the executable of the program
add_executable (space_impact ${SOURCE_FILES})
target_link_libraries(space_impact ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
Note that adding SFML_DEPENDENCIES to the library list is optional, unless you're using a static version of SFML.
But what about your SFML issue? Since you don't have the SFML files installed in any directory looked into by default, you'll have to tell CMake where it's found using the CMake variable SFML_ROOT or the environment variable of the same name.
So the first time you're invoking CMake, it could look like this:
cmake -G "Visual Studio 15 2017" -DSFML_ROOT=path/to/sfml path/to/source
This is all you need to compile sfml in your cmake project.
find_package(SFML 2.5.1 COMPONENTS system graphics audio network REQUIRED)
add_executable (AwesomeProject "AwesomeProject.cpp" "AwesomeProject.h")
target_link_libraries(AwesomeProject PRIVATE sfml-audio sfml-graphics sfml-network sfml-system)
Also set SFML_DIR var to your sfml folder.

CMake cannot find freeglut on windows in Clion

I've been stuck for a while now and I can't figure out how to get freeglut working. I thought I knew what it was asking me to do, so I added that set(prefix_path) line but it didn't do anything. Am I supposed to write my own freeglut-config.cmake or what?
Note: I am using the freeglut for MinGW package from this website
CMake File:
cmake_minimum_required(VERSION 3.7)
project(HW1)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES Triangle.cpp)
set(CMAKE_PREFIX_PATH "C:/freeglut")
find_package(GLEW REQUIRED STATIC)
find_package(FREEGLUT REQUIRED)
find_package(OPENGL REQUIRED)
include_directories(${FREEGLUT_INCLUDE_DIRS} ${GLEW_INCLUDE_DIRS} ${OPENGL_INCLUDE_DIRS})
link_directories(${FREEGLUT_LIBRARY_DIRS} ${GLEW_LIBRARY_DIRS} ${OPENGL_LIBRARY_DIRS})
add_definitions(${FREEGLUT_DEFINITIONS} ${GLEW_DEFINITIONS} ${OPENGL_DEFINITIONS})
add_executable(HW1 ${SOURCE_FILES})
target_link_libraries(HW1 ${FREEGLUT_LIBRARIES} ${GLEW_LIBRARIES} ${OPENGL_LIBRARIES})
Full error:
CMake Error at CMakeLists.txt:8 (find_package):
By not providing "FindFREEGLUT.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "FREEGLUT",
but CMake did not find one.
Could not find a package configuration file provided by "FREEGLUT" with any
of the following names:
FREEGLUTConfig.cmake
freeglut-config.cmake
Add the installation prefix of "FREEGLUT" to CMAKE_PREFIX_PATH or set
"FREEGLUT_DIR" to a directory containing one of the above files. If
"FREEGLUT" provides a separate development package or SDK, be sure it has
been installed.
If your application is GLUT-compatible, that it doesn't use any extension of freeglut, then it is better to search GLUT instead of FREEGLUT:
find_package(GLUT REQUIRED)
"Find" script used by this command is already shipped into CMake distro, and it searches freeglut too.
(Note, that with that command variables for include directories and linking libraries are GLUT_INCLUDE_DIR and GLUT_LIBRARY correspondingly).
If your application requires exactly freeglut (that is, uses some of its extensions incompatible with other GLUT implementations), you need to ship your package with FindFREEGLUT.cmake script and adjust CMAKE_MODULE_PATH variable correspondingly:
# Assuming you have <source-dir>/cmake/FindFREEGLUT.cmake
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
find_package(FREEGLUT REQUIRED)
You may find existing script in the net, or write it by yourself, like here.
In any case, if you have freeglut installed into non-system location, you need to hint CMake about that. E.g., by adjusting CMAKE_PREFIX_PATH.

configure SFML for clion (windows)

i am setting up a work environment for a school project on my windows computer. We are going to make a basic game using c++ and CLion. To make a game i need to use the SFML library. I have followed a few tutorials but i cant seem to get it to work anyway.
I have:
Downloaded CLion and configured it with MinGW
Downloaded SFML and copied its "findSFML.cmake" file to a new directory in my project that i call cmake_modules.
Edited my CMakeLists.txt file so it looks like this:
cmake_minimum_required(VERSION 3.6)
project(testet)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(testet ${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(testet ${SFML_LIBRARIES})
endif()
These are the three steps that i see on every tutorial / answer. But I get the following error anyway:
"C:\Program Files (x86)\JetBrains\CLion 2016.3\bin\cmake\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Benjamin\ClionProjects\testet
CMake Error at cmake_modules/FindSFML.cmake:355 (message):
Could NOT find SFML (missing: SFML_SYSTEM_LIBRARY SFML_WINDOW_LIBRARY
SFML_GRAPHICS_LIBRARY SFML_NETWORK_LIBRARY SFML_AUDIO_LIBRARY)
Call Stack (most recent call first):
CMakeLists.txt:10 (find_package)
So it cant find the SFML? But should'nt the "findSFML.cmake" solve this? Any help is appretiated... Thanks! :D
I believe you are missing the link_directories() call. You can use it like this:
link_directories("C:/Path_To_Library")
This should help solve your issue.
I have successfully configured SFML with CLion on Ubuntu 16.04 and I think it will be same for Window user also.
My project name is SFML_TEST so change every occurrence of SFML_TEST with your project name.
Create a new Clion C++ Project.
Navigate to /path/to/CLionProjects/[Project_Name]/CMakeLists.txt
After the following statement
add_executable(SFML_TEST ${SOURCE_FILES})
Add following lines of code
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(SFML_TEST ${SFML_LIBRARIES})
endif()
Create a new directory /path/to/CLionProjects/[project_name]/cmake_modules/FindSFML.cmake
In FindSFML.cmake file paste the following line of code from the given file https://github.com/SFML/SFML/blob/master/cmake/Modules/FindSFML.cmake
Done!!!.. Happy Coding
my fix was, that I had to change the root path of SFML in the FindSFML.cmake
so just set(SFML_ROOT Z://your_project) after the block of comments and you are ready to go

How to use CMake's pkg_search_module() when compiling under Windows?

I'm programming a game which uses SDL2 and CMake.
In order to link and include SDL2 I use the following CMake code:
include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)
target_link_libraries(MYLIB SDL2)
When compiling under Linux (Fedora) this works perfectly.
But what about Windows? There I don't have standard system locations for DLL/a files and include folders. There isn't even pkg-config.
This is the error I get:
Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
checking for one of the modules 'sdl2'
CMake Error at C:/Program Files (x86)/CMake/share/cmake-3.0/Modules/FindPkgConfig.cmake:425 (message):
None of the required 'sdl2' found
Call Stack (most recent call first):
.../CMakeLists.txt:4 (pkg_search_module)
I used the VS 2013 generator.
As you may already know, pkg_search_module() relies on the pkg-config program in order to locate dependencies. As you point out, Windows doesn't come with pkg-config. You might be able to install it and get things working that way, but probably a better alternative for crossplatform builds is to use CMake's find_package() function instead. This way you can eliminate the dependency on having pkg-config installed on the developer's machine.
cmake_minimum_required(VERSION 3.1)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules")
find_package(SDL2 REQUIRED)
add_executable(sdl2demo sdl2demo.cpp)
target_include_directories(sdl2demo SYSTEM PRIVATE "${SDL2_INCLUDE_DIR}")
target_link_libraries(sdl2demo "${SDL2_LIBRARY}")
CMake doesn't come with a module for finding SDL2 so you'll need to add that to your source directory. That's why the above sets the CMAKE_MODULE_PATH; you add a cmake script that works cross platform for finding SDL2 to ${CMAKE_SOURCE_DIR}/cmake/Modules and then you can deliver that script with your source code.
The FindSDL2.cmake module I've used is: http://freerct.googlecode.com/svn/trunk/CMake/FindSDL2.cmake
This script should be able to locate SDL2 if it's installed in a standard location. If SDL2 isn't installed in a standard location or otherwise can't be located by this script, the developer has to configure the appropriate CMake variables to tell CMake the appropriate location, after which the configure and build will be able to procede normally.
cmake <source dir> -G "Visual Studio 12 2013 Win64" -DSDL2_INCLUDE_DIR=<sdl dir>/include -DSDL2MAIN_LIBRARY=<sdl dir>/lib/x64/SDL2main.lib -DSDL2_LIBRARY=<sdl dir>/lib/x64/SDL2.lib