Setup CMake with SFML in VS2017 - c++

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.

Related

AUTOMOC set to true makes fail cmake build

I'm at the very first day of Qt + Cmake and Conan, trying to make things work. I'm not using qmake because I'll integrate everything into a bigger project using cmake.
By following QT's tutorial, I figured out that I need to compile QT macros, and for that there's a useful AUTOMOC CMake property, as suggested here.
The point is that it's making me fail cmake builds.
My conanfile.txt:
[requires]
qt/5.15.2
[generators]
cmake
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.20)
project(qttest)
set(CMAKE_CXX_STANDARD 20)
set_target_properties(${PROJECT_NAME} PROPERTIES AUTOMOC TRUE)
set (PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
with the following output:
CMake Warning (dev) in CMakeLists.txt:
AUTOGEN: No valid Qt version found for target qttest. AUTOMOC disabled.
Consider adding:
find_package(Qt<QTVERSION> COMPONENTS Core)
to your CMakeLists.txt file.
This warning is for project developers. Use -Wno-dev to suppress it.
ouch, but adding the find doesn't make things better:
CMake Warning at CMakeLists.txt:6 (find_package):
By not providing "FindQt5.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Qt5", but
CMake did not find one.
Could not find a package configuration file provided by "Qt5" with any of
the following names:
Qt5Config.cmake
qt5-config.cmake
Actually the project compiles, Qt is there in its conan dir:
matteo#MacBook-Pro-de-matteo 96a68a791abfc7a246f2bc28aa2f6fc210be0f9f % cd ~/.conan/data/qt
matteo#MacBook-Pro-de-matteo qt % ls
5.15.2 6.2.2
matteo#MacBook-Pro-de-matteo qt %
how could I enable it, or make things easier to compile it along with cmake?
You need to tell CMake, where to find Qt.
So, as CMake suggests by itself:
find_package(Qt5 COMPONENTS Core)
for the most basic stuff, you might want to add some of the other components later.
Depending on the system you are working on and your Qt installation, you need to tell CMake where to search for the package configuration files (second error message). CMake has some default directories, where it looks for these files, but obviously, there is none. On Linux, this can be solved by installing Qt with a package manager (this will install the CMake config files to one of the Qt default locations). If you are on Windows or if you installed Qt to a different location, this can be solved by providing the path with the PREFIX_PATH-variable.
cmake -B $BUILD_DIR -S $SOURCE_DIR -DCMAKE_PREFIX_PATH=$QT_INSTALL_PATH/5.15.2/$ARCHITECTURE $OTHER_OPTIONS
(You can have different versions installed in the same installation path, that's why Qt adds an other folder with the version number. And you can have different compilers/architectures. On Windows for example, you might have a mingw73_32 and a msvc2017 folder to choose.)
As already mentioned in the comments, a project is no CMake target. CMake targets are either libraries (add_library), executables (add_executable) or custom targets (add_custom_target); the project is not. If you want to set the AUTOMOC property target wise, that's ok and even suggested by CMake, but you can also set it globally by using:
set(CMAKE_AUTOMOC ON)

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

How to use SFML libraries with Clion on Windows10

i'm trying to use SFML's library with CLion for a school project. I'm still not confident with programming and i am new to Clion.
After having download the library from SFML official site i read some tutorials, but i am a bit confused:
https://www.sfml-dev.org/tutorials/2.4/
Infact in the Getting Started section there are two voice that concern me: "Compiling with Cmake" and "..Code::Blocks (MinGW).
Well i tryed both but with no results, then i landed here.
I saw a topic that helped a bit configure SFML for clion (windows)
so i tried to follow the steps suggested.
The library is in C:\SFML-2.4.2
I copied FindSFML.cmake in SFMLProjects, located in C:\Users\Ludovico\ (the same path of the folder ClionProjects)
Then i gave it a try
cmake_minimum_required(VERSION 3.6)
project(provaSfml)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(provaSfml ${SOURCE_FILES})
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/SFMLProjects")
link_directories(C:/SFML-2.4.2/bin)
find_package(SFML REQUIRED system window graphics network audio)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(provaSfml ${SFML_LIBRARIES})
endif ()
These are the error messages
"C:\Program Files (x86)\JetBrains\CLion 2016.3.2\bin\cmake\bin\cmake.exe" - DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - MinGW Makefiles" C:\Users\Ludovico\CLionProjects\provaSfml
CMake Error at CMakeLists.txt:10 (find_package):
By not providing "FindSFML.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "SFML", but
CMake did not find one.
Could not find a package configuration file provided by "SFML" with any of
the following names:
SFMLConfig.cmake
sfml-config.cmake
Add the installation prefix of "SFML" to CMAKE_PREFIX_PATH or set
"SFML_DIR" to a directory containing one of the above files. If "SFML"
provides a separate development package or SDK, be sure it has been
installed.
I'm thinking i did some mistakes even in the organization of folder...
can someone tell me some advice in order to clean the mess i have in my head?
What can i do to fix these problems?

Missing sfml libraries for cmake

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?

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