I'm quite new to CMake and C++ in general, so today I've tried to import an external library like SFML to my project in Jetbrain's IDE CLion, but without any luck.
After spending a whole day learning about CMake in general and making SFML work with CMake in particular, I've finally managed my project's CMake to find the SFML library files.
However, when I approached a header file of my own to include a SFML header, I encountered a problem as it didn't find any headers from the library - And by that I mean the #include directives.
Since I am a newbie, I'm quite lost here.
Here's my CMakeLists.txt file:
# Set CMake's minimum required version
cmake_minimum_required(VERSION 3.5)
set(CMAKE_VERBOSE_MAKEFILE on)
#Set CMake's project name
project(testproj)
include_directories("${PROJECT_BINARY_DIR}")
#Set CMake's flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
#Set source files
set(SOURCE_FILES Animal.cpp Animal.hpp ConstantValues.hpp Enums.hpp Mamal.hpp Mammals/Lion.cpp Mammals/Lion.hpp)
add_library(testproj SHARED ${SOURCE_FILES})
#Set CMake's module path to find the SFML Lib
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/Modules/" ${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "C:\\SFML\\SFML-2.3.2")
#Find the SFML lib
find_package(SFML 2 REQUIRED audio)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(testproj ${SFML_LIBRARIES})
endif (SFML_FOUND)
It is worth noting that I'm working on Windows and I look only for the audio module in SFML.
What am I missing?
OK So I've managed to pull to this off thanks to #Tsyvarev's help.
When I realized that the problem is related only to the #include directives and not the CMake script, I took a bit deeper look in it and found out that opposed to SFML's official documentation, the header files for each module are located under their matching directory.
So for example to include a header file from the audio module, I should do this:
#include <SFML/Audio/sound.hpp>
The key here is to look in the SFML folder first, just as you would do with boost.
Related
I know this is a daft question, but I'm a beginner in visual studio/c++/cmake. I'm looking for a quick intro on how to use Qt5 installed via vcpk using:
vcpkg install qt5-base:x64-windows
This all installed ok and I got the following:
The package qt5-base:x64-windows provides CMake targets:
find_package(Qt5Concurrent CONFIG REQUIRED)
target_link_libraries(main PRIVATE Qt5::Concurrent Qt5::ConcurrentPrivate)
etc....
I just don't know what to do next! Before using libs in VS I just did an <#include> now I'm confronted with this lot... Pref. I want some sort of explanation at newbie level please.
If I add the line (at the top of a .cpp file just as a test):
#include <QtWidgets/QApplication>
It gives: Error (active) E1696 cannot open source file "QtWidgets/QApplication"
I'm new, I thought vcpkg took all the pain out of having to add all the libs etc to the project options? What do I need to do?
If you ran vcpkg integrate install and are just using VS you can just #include <Qt5/QtWidgets/QApplication>
If you are using CMake:
find_package(Qt5 COMPONENTS Widgets Concurrent CONFIG REQUIRED) and use target_link_libraries as described by the other answers. But you probably have to switch to #include <QApplication> since cmake file add the QtWidgets folder to the include folders.
For find_package to find the vcpkg build versions you have to specify the vcpkg.cmake toolchain file as the CMAKE_TOOLCHAIN_FILE=<vcpkgroot>/scripts/buildsystems/vcpkg.cmake (must be set in the first CMake call or rather early in the CMakeLists.txt before any project() call) and maybe also VCPKG_TARGET_TRIPLET=<sometriplet> (must also be defined early before CMAKE_TOOLCHAIN_FILE is loaded) if you installed Qt5 using one of the static triplets.
vcpkg is a cross-platform C++ package manager. Unlike winget, apt, and brew, vcpkg is designed for developers. For example, it builds the binaries from the source by default.
So it help user to have libraries installed in their projects and be able to find them.
You still need to learn how CMake canonical find_package() work IMHO.
Qt provide a cmake config package and usually, you'll need to use
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(Qt5Widgets REQUIRED)
then
target_link_libraries(main PRIVATE ... Qt5::Core Qt5::Gui Qt5::Widgets)
ie rule of Thumb: you need a QtWidget/* include ? then target_link to Qt5::Widget etc...
Please note that CMake also provides (i.e. built-in) a few tools to ease Qt-related dev...
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
-> you should try to read the CMake doc...
So I'm trying to use OpenCV on windows (my ubunut is broken) and I can't configure my Clion to compile my openCV code.
first; i'm completely new to OpenCV so this question might be an easy one but I did all I could to find the problem to no avail.
I used MinGW and Clion and my codes run with no problem if I don't use OpenCv which indicates there's no problem with MinGW installation.
I downloaded OpenCv files and extracted them, then added them to System path.
after that I configured my Cmakelists.txt (my first time modifying it, so I was an amateur) like this.
After that I got this error:
Error:By not providing "FindOpenCV.cmake" in CMAKE_MODULE_PATH this
project has asked CMake to find a package configuration file provided
by "OpenCV", but CMake did not find one. Could not find a package
configuration file provided by "OpenCV" with any of the following
names: OpenCVConfig.cmake opencv-config.cmake Add the installation
prefix of "OpenCV" to CMAKE_PREFIX_PATH or set "OpenCV_DIR" to a
directory containing one of the above files. If "OpenCV" provides a
separate development package or SDK, be sure it has been installed.
I know I'm doing something wrong or missing a step but I can't figure out why. is there any guide or tutorial for my scenario or can anyone point me towards a correct direction?
thanks in advance and any help appreciated.
I managed to make those errors go away with changing my
CMakeLists.txt to:
cmake_minimum_required(VERSION 3.5)
project(something)
set(OpenCV_DIR "E:/Programs/programming/opencv/build2") find_package( OpenCV REQUIRED )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp) add_executable(something ${SOURCE_FILES})
add_executable( {SOURCE_FILES} DisplayImage ) target_link_libraries( ${OpenCV_LIBS} DisplayImage )
and building the libraries on my own by minGW and Cmake.
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
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.
I'm currently learning CMake and I'm trying to create my first test project. I'm able to get a simple project up and running in visual studio via CMake. However, I'm having trouble trying to add a library. I've read some guides and things but I keep getting errors. Basically, I'm trying to link SDL libraries (a game programming library) in my sample project. I've placed these libraries in a top level, 'ThirdParty' folder. Here is what my CmakeLists.txt file looks like in my top level directory:
cmake_minimum_required(VERSION 2.8.11)
project(Hello)
#Find necessary header files
find_path(SDL_INCLUDE_DIR SDL.h HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/include/)
#Find necessary library files
find_library(SDL_LIB_DIR SDL2 HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/lib/x86)
find_library(SDLMAIN_LIB_DIR SDLmain HINTS ${CMAKE_SOURCE_DIR}/ThirdParty/SDL2/lib/x86)
#Add/Link files to project
include_directories(${SDL_INCLUDE_DIR})
target_link_libraries(Test PUBLIC ${SDL_LIB_DIR})
target_link_libraries(Test PUBLIC ${SDLMAIN_LIB_DIR})
add_executable(Test "${CMAKE_SOURCE_DIR}/Source/Main.cpp")
I'm not 100 percent sure of the HINTS parameter, but I saw it used on another thread. Anyway, here's the error I keep getting:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.
Please set them or make sure they are set and tested correctly in the CMake files:
SDLMAIN_LIB_DIR
linked by target "Test" in directory C:/Users/Jason/Desktop/Test
What am I doing wrong and how do I properly link libraries in CMake?
In cmake, you first create the executable, and then you link it to a library
You have to understand how finding libraries and packages works in CMake. Typically the way it works is that you use find_library or find_package, and then cmake will set some variables that you can use to link to/use the library.
I'm not familiar with SDL, but by googling a little bit about it, I would say this is how it should look like:
find_file(SDL2_INCLUDE_DIR NAME SDL.h HINTS SDL2)
find_library(SDL2_LIBRARY NAME SDL2)
add_executable(MyExec main.cpp)
target_include_directories(MyExec ${SDL2_INCLUDE_DIR})
target_link_libraries(MyExec ${SDL2_LIBRARY})
That find_library will set the variables SDL2_INCLUDE_DIR and SDL2_LIBRARY, which you can use to link to SDL and add its includes to your project.