Using Qt5 installed via VCPKG in Visual Studio for C++ - c++

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...

Related

Very slow intellisense for CMake-configured Vulkan-project

I have a Vulkan project configured with the following CMakeLists.txt on Windows:
cmake_minimum_required(VERSION 3.20)
project(Custom_Vulkan)
set(MINGW_INCLUDE "C:\\msys64\\mingw64\\include")
find_package(Vulkan REQUIRED)
find_package(glfw3 REQUIRED HINTS "C:\\msys64\\mingw64\\lib\\cmake")
add_subdirectory(shaders)
add_executable(AppTest main.cpp)
target_link_libraries(AppTest PUBLIC Vulkan::Vulkan glfw)
include_directories(${MINGW_INCLUDE})
Extensions for CMake (CMake & CMake Tools), and C++ (C/C++, C/C++ Extension Pack, C/C++ Themes) are installed, and I have configured and built my executable using the CMake Tools-extension.
Intellisense can at times be unreasonably slow and freezes completely, resulting in 0 autocompletion suggestions for both constants from built-in libraries like M_PI from cmath, but also for the Vulkan/GLFW-APIs which have been located by CMake.
Where should I search in order to locate the dependency that stalls Intellisense?
Is it possible to monitor/profile the Intellisense-search in order to find the issue?
The CMake-tools project is fully able to detect the dependencies, and compiles fine.
The Intellisense-freezes were in this case caused by dependency-issues between a FetchContent repository dependency in CMake and the C# vscode extension pack which caused an OmniSharp server-handle to remain open.
This was a standard FetchContent-pull:
FetchContent_Declare(
imgui_repo
GIT_REPOSITORY "https://github.com/ocornut/imgui.git"
)
FetchContent_MakeAvailable(imgui_repo)
Right-clicking the extension followed by disable(workspace) solved the issue.

How can I add Vulkan to a cross platform CMake project?

In order to make a CMake project as simple and as portable as it can get I have consider to add the "whole" repositories of the libraries I need to the project.
The project structure is as follows:
MyProject/
└──CMakeLists.txt
└──src/
└──MyProject/
└── *.h & *.cpp
└── CMakeLists.txt
└──ThirdParty/
└──Vulkan-Hpp/
└──(Vulkan Files)
└──glfw/
└──(glfw Files)
└──SFML/
└──(SFML Files)
All the third party directories are the git submodules of the following repositories:
https://github.com/KhronosGroup/Vulkan-Hpp
https://github.com/SFML/SFML
https://github.com/glfw/glfw
Summarizing everything up, I'm having trouble integrating the vulkan and sfml libraries to the project.
MyProject/CMakeLists.txt is as follows:
cmake_minimum_required (VERSION 3.8 FATAL_ERROR)
project ("MyProject")
set (MyProject_VERSION_MAJOR 0)
set (MyProject_VERSION_MINOR 2)
set (MyProject_VERSION_PATCH 1)
set (CMAKE_CXX_STANDARD 17)
# Include sub-projects.
add_subdirectory ("src/MyProject")
add_subdirectory ("ThirdParty/glfw")
add_subdirectory ("ThirdParty/SFML")
add_subdirectory ("ThirdParty/Vulkan-Hpp")
MyProject/src/MyProject/CMakeLists.txt:
cmake_minimum_required (VERSION 3.8 FATAL_ERROR)
project ("MyProject")
find_package(Vulkan REQUIRED FATAL_ERROR) # error
find_package(SFML REQUIRED network audio) # error
find_package(glfw REQUIRED FATAL_ERROR) # error
# Add source to this project's executable.
add_executable (MyProject "MyProject.cpp")
target_include_directories (MyProject
PUBLIC ${GLFW_INCLUDE_DIRS}
PUBLIC ${SFML_INCLUDE_DIR}
PUBLIC ${VULKAN_INCLUDE_DIRS}
)
target_link_libraries (MyProject glfw)
target_link_libraries (MyProject ${VULKAN_LIB_LIST})
target_link_libraries (MyProject ${SFML_LIBRARIES})
How can I tweak CMake in order to use the third party libraries at my main project?
Is the project structure incorrect?
If your find_package(Vulkan REQUIRED FATAL_ERROR) line is failing, you need to make sure the Vulkan SDK is properly installed, i.e. that you have a VULKAN_SDK environment variable that points to the correct location.
Additionally, do not embed the KhronosGroup/Vulkan-Hpp repository. This repository is for building the Vulkan C++ bindings, but shouldn't be used directly. Instead you should be using the vulkan.hpp header that is bundled with your installation of the Vulkan SDK. Otherwise when people try to build your project and have a different version of the Vulkan SDK installed than is referred to by your embedded KhronosGroup/Vulkan-Hpp
More generally, you are using find_package and then later you're using add_subdirectory to try to incorporate these external projects. That's not how it works. find_package will look for a pre-existing binary of the package, while add_subdirectory isn't designed to just swallow entire existing external CMake projects.
If you want to have your project build these others from source you should investigate the use of CMake's external project functionality. However, you'll probably find this to be more of a burden than it's worth. Alternatively, install vcpkg for your target platform, and use vcpkg to build and install glfw and sfml, then tell CMake to use the vcpkg dependencies (see the vcpkg docs on how to pass the CMAKE_TOOLCHAIN_FILE to your cmake configure line.

CMake file cannot locate Qt charts module

I'm trying to start writing my Qt project inside JetBrains' Clion but I need to link some libraries in my Cmake file first. There's no problem when trying to find packages like Qt5Core, Qt5Widgets, Qt5Gui but when it come to finding Qt5Charts an error is thrown:
By not providing "FindQt5Charts.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file
provided by "Qt5Charts", but CMake did not find one.
Could not find a package configuration file provided by "Qt5Charts"
with any of the following names:
Qt5ChartsConfig.cmake
qt5charts-config.cmake
Add the installation prefix of "Qt5Charts" to CMAKE_PREFIX_PATH or
set "Qt5Charts_DIR" to a directory containing one of the above
files. If "Qt5Charts" provides a separate development package or
SDK, be sure it has been installed.
This is my CMake file right now.
All packages are installed via the Qt's Linux(ubuntu) maintanence tool.
Any ideas how to help Cmake find the Charts module ?
Using the following and see if it helps:
sudo apt install libqt5charts5-dev
Src: https://stackoverflow.com/a/46765025
Typically when including Qt5 in a project I use the follow basic script for CMake, though I should note I haven't tested this on Linux.
cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)
project(<YOUR_PROJECT_NAME>)
find_package(Qt5 REQUIRED COMPONENTS Core Gui Widgets Charts)
# set your project sources and headers
set(project_sources src/blah.cpp)
set(project_headers include/headers/blah.h)
# wrap your qt based classes with qmoc
qt5_wrap_cpp(project_source_moc ${project_headers})
# add your build target
add_executable(${PROJECT_NAME} ${project_sources} ${project_headers} ${project_source_moc})
# link to Qt5
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt5::Core
Qt5::Gui
Qt5::Widgets
Qt5::Charts)
# require C++ 14
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_14)
When configuring your project via cmake, you just need to pass in the path to you qt5 installation directory (cmake variable name is Qt5_DIR) that contains the Qt5Config.cmake file and then cmake should be able to find the rest of the components that you request.
Also double check that Qt5Charts was installed, not sure if it's installed by default.
Maybe try this?
sudo apt install libqt5charts5-dev

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

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.