Setting up Qt for CLion - c++

i am struggling to set up Qt5 for CLion. Somehow, I did this for VS before but failed to do this in CLion. Building and Including Qt headers are fine and CLion find qt symbols and auto-completes them but when i am using an Qt object Clion giving me this error:
C:\Users\binhb.CLion2016.1\system\cmake\generated\LBMTopoOptimization-ae159e87\ae159e87\Debug\LBMTopoOptimization.exe
Process finished with exit code -1073741515 (0xC0000135)
My CMake file looks like this:
cmake_minimum_required(VERSION 3.5)
project(LBMTopoOptimization)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# set prefix path for Qt5
set (CMAKE_PREFIX_PATH "C:\\QT\\5.7\\mingw53_32\\")
# Find QT Libraries
find_package( Qt5Core REQUIRED )
find_package( Qt5Widgets REQUIRED )
find_package( Qt5Gui REQUIRED )
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(LBMTopoOptimization ${SOURCE_FILES})
# Use the modules from Qt 5.
target_link_libraries(LBMTopoOptimization Qt5::Widgets Qt5::Core Qt5::Gui)
The error would already occur by using for example a QString:
#include <QWidget>
int main(int argc, char** argv ){
QString name = "this is a string";
return 0;
}
System specification:
Windows 10
Clion 1.3
Qt 5.7
CMake 3.6 rc3
mingw-gcc 4.9.3
I looking forward for any hints.

Try to add Qt library's installation directory (select specific Qt version and build system \bin sub-subdirectory) to PATH environment variable .
I have been struggling with similar problem. I have had installed Qt library (version 5.6 and 5.7) in C:\Qt\ directory. In my project I use version 5.6. Build system is MinGW.
After I have added C:\Qt\5.6\mingw49_32\bin, everything works fine.
The reason for this behaviour is quite simple. During the build, build system 'knows' where to search for libraries, but when app is being executed, Windows system searches for required libraries in application's directory and directories specified by PATH variable.
If it fails, application exits with -1073741515 (0xC0000135) error.

Related

make failed after successful CMake run

So, I am trying to follow this tutorial, I copied the code and CMakeLists.txt, which I later edited. Now when I run cmake everything is fine, but then when I run make it just fail with this error:
main.cpp:1:10: fatal error: QApplication: No such file or directory
1 | #include <QApplication>
| ^~~~~~~~~~~~~~
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(kparttut1)
set(QT_MIN_VERSION "5.11.0")
set(KF_MIN_VERSION "5.55.0")
find_package(ECM ${KF_MIN_VERSION} REQUIRED NO_MODULE)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH})
include(KDEInstallDirs)
include(KDECMakeSettings)
include(KDECompilerSettings NO_POLICY_SCOPE)
include(ECMInstallIcons)
include(FeatureSummary)
find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS Core Gui Widgets)
find_package(KF5 ${KF_MIN_VERSION} REQUIRED COMPONENTS
CoreAddons
Crash
DBusAddons
DocTools
I18n
XmlGui
TextEditor
Parts
)
set(kparttut1_SRCS
main.cpp
mainwindow.cpp
)
add_executable(kparttut1 main.cpp)
########### install files ###############
#install(TARGETS kparttut1 ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
#install(FILES kparttut1ui.rc
# DESTINATION ${DATA_INSTALL_KXMLGUI5DIR}/kparttut)
#feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES)
I am using latest Arch Linux with Qt version 5.15.2
The tutorial you followed has some problem, you should check the KF5 tutorial which has a correct CMake setup.
You did the find_package thing, but you forgot to link the library to your executable!
In CMake, linking to a library adds compile definitions, include directory and of course linking to the library.
Here's what to add to make it work:
target_link_libraries(kparttut1 PUBLIC
Qt5::Widgets
KF5::CoreAddons
KF5::I18n
KF5::WidgetsAddons
)
This adds all the necessary stuff to the compiler arguments so all linked libraries are found.

Runtime error when running libpng application

I am trying to compile openGL file including "png.h" header file,
I got the following error:
Open GL version 2.1 ATI-3.2.24
libpng warning: Application built with libpng-1.4.12 but running with 1.6.37
error: png_create_read_struct returned 0.
Failed to read image texture from ../images/ceramic.png
My Cmakelists.txt file :
cmake_minimum_required (VERSION 3.13)
project (teapot)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_package(PNG REQUIRED)
include_directories(${PNG_INCLUDE_DIR})
add_executable(teapot teapot.cpp)
target_link_libraries(teapot ${OPENGL_gl_LIBRARY} ${GLUT_LIBRARIES} ${PNG_LIBRARIES} )
set(CMAKE_CXX_FLAGS "-I ${CMAKE_OSX_SYSROOT} ${CMAKE_CXX_FLAGS} -std=c++11")
if (APPLE)
set (CMAKE_CXX_FLAGS "-Wno-deprecated-declarations ${CMAKE_CXX_FLAGS}")
endif ()
set_target_properties(teapot PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
cmake . works fine, but when I execute ./teapot the above error occurred. Thanks for any help!
You've probably only have development libraries installed for libpng-1.4.12 and not for libpng-1.6.37 or some other "non-standard" installation.
But the message seems pretty clear you linked against an older version but have a newer version in the runtime path of loadable libraries.
find_package(PNG REQUIRED) is only going to search the "standard" locations via find_library().
You may also want to update your CMakeLists.txt file to use target_link_libraries( ... PNG::PNG). This is simpler than trying to use the PNG variables that get set; which is missing PNG_DEFINITIONS when compiling your project. Refer to the CMake manual buildsystem section about library targets.
You can inspect the variables by using the message() command to print out their values. Some of them will also be stored in CMakeCache.txt.
If libpng isn't in the standard locations then you have to use target_link_libraries( ... /path/to/lib) and target_include_directories( ... /path/to/lib/headers), etcetera to handle it.

Qt Creator Error: cannot find -lopencv_imgcodecs

I have installed opencv, qt, qt creator, cmake on ubuntu 15.10 through VMware on windows.
The opencv is installed in this directory: /home/majidalaeinia/opencv/
The project repository is cloned in this directory: /home/majidalaeinia/Desktop/imgwarp-opencv/
I want to run the project through its CMakeLists.txt in qt creator and when I press Build now on qt creator, I get the following errors:
error: cannot find -lopencv_imgcodecs
error: collect2: error: ld returned 1 exit status
Where is the problem and how can I solve it?
# Majid Alaeinia, from the CMakeLists.txt file you posted it is not specified how CMAKE should find the libraries requested from your project. Also there are no target_link_libraries declared so CMAKE does not know where to link them. Hopefully the following small example template should be helpful for your project:
cmake_minimum_required (VERSION 3.1)
project(yourProject)
find_package( OpenCV REQUIRED )
find_package( Qt5 REQUIRED COMPONENTS Sql )
### this is for c++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
### QT stuff if you want a GUI
set(CMAKE_AUTOMOC ON) # autogenerate qt gui features
set(CMAKE_AUTORCC ON) # used for QT resource Files (if you need)
## Additional operation...
# From here you are specifically linking all OpenCV libraries and executables
### Add executables
add_executable(yourExecutable main/main.cpp ui/res/res.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (yourProject example Qt5::Widgets ${OpenCV_LIBS} Qt5::Sql)
### Add Library
add_library(yourProject_lib SHARED ${SRCS} ${UI_HDRS})
target_link_libraries (yourProject_lib example Qt5::Widgets ${OpenCV_LIBS})
# Majid Alaeinia,I uploaded the repository and went through the code. if you go inside the demo folder and you change the present CMakeLists.txt file with the one I provided below it should compile (It does compile on mine with the provided changes):
project(demo)
cmake_minimum_required(VERSION 2.6)
find_package(Qt5 REQUIRED COMPONENTS Widgets Core)
FIND_PACKAGE( OpenCV REQUIRED )
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/lib ${CMAKE_CURRENT_SOURCE_DIR})
set(demo_SRCS main.cpp projfile.cpp deformwin.cpp myimage.cpp singlephotoview.cpp pointspaint.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#qt5_automoc(${demo_SRCS})
QT5_WRAP_CPP(QOBJ_CPP ${demo_SRCS})
qt5_wrap_ui(helloworld_FORMS_HEADERS deformwin.ui)
add_executable(demo ${demo_SRCS} ${helloworld_FORMS_HEADERS})
target_link_libraries(demo ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} imgwarp-lib opencv_core opencv_imgproc opencv_imgcodecs)
The code in the repository is an old code and still carries Qt4 as main wrappers. I think you probably have Qt5 installed on your computer and in fact the code I provided it will work for Qt5. Use it as a guideline for the other CMakeLists.txt file present inside src folder and change accordingly.
CMake will compile but because it was used Qt4 you need to figure out the most important modules to add, for example the new standard for including QtGui/QApplication is usually substituted by QtWidgets/QApplication
I also wanted to leave my previous answer in case you need a starting point or a initial template. I hope this clarifies a bit more and can get you move forward for your project.

Getting SDL2 to work on Clion, Windows 10 using MinGW

I cannot for the life of me get SDL2 to work with my programming group's project.
I'm using
-Clion 1.2.1
-SDL 2.0.3
-MinGW 5.0
The compiler starts yelling at me from within the Graphics.h file which includes SDL like so:
#include <SDL.h>
#include <SDL_image.h>
with the error being:
fatal error: SDL.h: No such file or directory
compilation terminated.
I tried including with
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
which still yielded the same error.
I downloaded SDL from the MinGW 32/64-bit download of development libraries from: https://www.libsdl.org/download-2.0.php. I also linked the respective ..\SDL2\x86_64-w64-mingw32\include and lib to path in system->advanced system settings->...
Still nothing.
cmake_minimum_required(VERSION 3.3)
project(ClionProjects)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lsdl2")
set(SOURCE_FILES
Kod/Graphics/Graphics.cc
Kod/Graphics/Graphics.h
Kod/Game/Game.cc
Kod/Game/Game.h
Kod/Gameboard/Gameboard.cc
Kod/Gameboard/Gameboard.h
Kod/Meeple/Meeple.cc
Kod/Meeple/Meeple.h
Kod/Player/Player.cc
Kod/Player/Player.h
Kod/Resource/Resource.cc
Kod/Resource/Resource.h
Kod/Tile/Tile.cc
Kod/Tile/Tile.h
Kod/Carcassonne.cc)
add_executable(ClionProjects ${SOURCE_FILES} Kod/Carcassonne.cc)
target_link_libraries(ClionProjects SDL2main SDL2 SDL2_image)
is the cmakefile I have in Clion. I've been bashing my head bloody trying to get this to work and none of the previous Stackoverflow questions managed to solve my issue.
Personally I uses MSYS2 in my project. The MINGW64 vesion of SDL2 and the other library were installed, as well as the other toolchains like gcc, cmake, and pkg-config (toolchains are MINGW64 version as well).
Then I point my CLion toolchain configuration to the c:\msys2\mingw64, given that I install the msys2 at c:\msys2.
The FindPkgConfig CMake module is used to locate the SDL2 library and include files location.
Please find the example below (I also use FreeType2 and harfbuzz as well). The important part is the FindPkgConfig part (pkg_check_modules) and include/link part.
Make sure to reload the configuration after you have modified the cmake file.
cmake_minimum_required (VERSION 2.6)
project (pgengine)
# The version number.
set (Tutorial_VERSION_MAJOR 0)
set (Tutorial_VERSION_MINOR 1)
add_definitions(-std=c++11)
aux_source_directory(src SRC_LIST)
aux_source_directory(src/game SRC_LIST)
aux_source_directory(src/graphics SRC_LIST)
aux_source_directory(src/sound SRC_LIST)
aux_source_directory(src/system SRC_LIST)
aux_source_directory(src/visual_novel SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
INCLUDE(FindPkgConfig)
pkg_check_modules(FREETYPE2 REQUIRED freetype2)
pkg_check_modules(HARFBUZZ REQUIRED harfbuzz)
pkg_check_modules(SDL2 REQUIRED sdl2)
pkg_check_modules(SDL2_IMAGE REQUIRED SDL2_image)
pkg_check_modules(SDL2_MIXER REQUIRED SDL2_mixer)
include_directories(${SDL2_INCLUDE_DIRS}
${SDL2_IMAGE_INCLUDE_DIRS}
${SDL2_MIXER_INCLUDE_DIRS}
${FREETYPE2_INCLUDE_DIRS}
${HARFBUZZ_INCLUDE_DIRS})
link_directories (${SDL2_LIBRARY_DIRS}
${SDL2_IMAGE_LIBRARY_DIRS}
${SDL2_MIXER_LIBRARY_DIRS}
${FREETYPE2_LIBRARY_DIRS}
${HARFBUZZ_LIBRARY_DIRS})
target_link_libraries (pgengine ${SDL2_LIBRARIES}
${SDL2_IMAGE_LIBRARIES}
${SDL2_MIXER_LIBRARIES}
${FREETYPE2_LIBRARIES}
${HARFBUZZ_LIBRARIES})
This has been tested on Windows (MSYS2) and Linux.

Cannot find QCursor in Qt CMake Project

I'm trying to build an application on Linux with Qt where I can set the Cursor position. The project is managed with CMake.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.4)
project(Project)
add_definitions(-std=gnu++14 -std=c++14 -Wall -Wextra)
set(CMAKE_PREFIX_PATH "/home/elmewo/Libraries/Qt/5.3/gcc_64")
set(CMAKE_AUTOMOC ON)
find_package(Qt5Core REQUIRED)
find_package(Qt5Quick REQUIRED)
find_package(Qt5Gui REQUIRED)
include_directories(${CMAKE_SOURCE_DIR}/src)
set(SOURCE_FILES src/main.cpp)
add_executable(Project ${SOURCE_FILES})
qt5_use_modules(Project Core Quick Gui)
The packages are found by CMake. But when I try to
#include <QCursor>
my compiler says
fatal error: QCursor: file or directory not found
I was able to compile another basic QGuiApplication on the same machine.
The QCursor file is situated in ${CMAKE_PREFIX_PATH}/include/QtGui.
Am I missing something?
It seems that you are depending on 2.8.4, so at least you either need to change your build rules based on this or you will need to upgrade the dependency to at least cmake version 2.8.9:
Using Qt 5 with CMake older than 2.8.9
If using CMake older than 2.8.9, the qt5_use_modules macro is not available. Attempting to use it will result in an error.
To use Qt 5 with versions of CMake older than 2.8.9, it is necessary to use the target_link_libraries, include_directories, and add_definitions commands, and to manually specify moc requirements with either qt5_generate_moc or qt5_wrap_cpp:
Therefore, please add these if you stick with old cmake:
# Add the include directories for the Qt 5 Widgets module to
# the compile lines.
include_directories(${Qt5Core_INCLUDE_DIRS} ${Qt5Gui_INCLUDE_DIRS} ${Qt5Quick_INCLUDE_DIRS})
#Link the helloworld executable to the Qt 5 widgets library.
target_link_libraries(helloworld Qt5::Core Qt5::Gui Qt5::Quick)