make failed after successful CMake run - c++

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.

Related

How to properly link OpenCV in a C++ cross-platform library for Android and iOS?

I am developing a C++ library, including OpenCV, which will be used in a cross-platform Xamarin solution through a wrapper and the NuGet packaging system (see this guide). I configured a CMakeLists.txt file but I simply cannot get OpenCV to be correctly linked for both static (iOS) and dynamic (Android) libraries.
I tried to change the OpenCV_DIR variable, install and build OpenCV from sources and manually include the content of the OpenCV_INCLUDE_DIRS variable but nothing worked. I also noticed that the linking works when only using cv::Point. But the linking does not work when using cv::Mat, which I do not understand the reason.
The following is the CMakeLists.txt that I am using :
cmake_minimum_required (VERSION 3.2)
project (MyLib C CXX)
enable_testing()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
MESSAGE(STATUS "CMAKE_CXX_FLAGS: " ${CMAKE_CXX_FLAGS})
# Source and headers files
set(SOURCES File1.cpp File2.cpp)
set(HEADERS File1.h File2.h)
# Library
if(BUILD_SHARED_LIBS)
add_library (MyLib SHARED ${SOURCES} ${HEADERS})
target_compile_definitions(MyLib PUBLIC IS_BUILDING_SHARED)
else()
add_library (MyLib STATIC ${SOURCES} ${HEADERS})
endif()
# Dependencies
set(OpenCV_DIR /usr/local/Cellar/opencv/4.5.0_1/lib/cmake/opencv4)
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV_INCLUDE_DIRS = ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
message(STATUS "OpenCV_DIR = ${OpenCV_DIR}")
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(MyLib ${OpenCV_LIBS})
The following shows the location of OpenCV's files that are used during the build process. Everything seems alright.
-- OpenCV_INCLUDE_DIRS = /usr/local/Cellar/opencv/4.5.0_1/include/opencv4
-- OpenCV_LIBS = opencv_calib3d;opencv_core;opencv_dnn;opencv_features2d;opencv_flann;opencv_gapi;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_stitching;opencv_video;opencv_videoio;opencv_alphamat;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dnn_objdetect;opencv_dnn_superres;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hfs;opencv_img_hash;opencv_intensity_transform;opencv_line_descriptor;opencv_mcc;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_quality;opencv_rapid;opencv_reg;opencv_rgbd;opencv_saliency;opencv_sfm;opencv_shape;opencv_stereo;opencv_structured_light;opencv_superres;opencv_surface_matching;opencv_text;opencv_tracking;opencv_videostab;opencv_viz;opencv_xfeatures2d;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
-- OpenCV_DIR = /usr/local/Cellar/opencv/4.5.0_1/lib/cmake/opencv4
Android
The following is the commands that I am using to build the Android dynamic library (.so). I have installed the NDK and am building for each ABI (x86, x86_64, armeabi-v7a, arm64-v8a).
cmake ../.. -DCMAKE_TOOLCHAIN_FILE=/Users/$USER/Library/Android/sdk/ndk-bundle/build/cmake/android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=21 -DANDROID_ABI=$abi_name -DBUILD_SHARED_LIBS=ON
cmake --build . --config Release
I directly get an error when building the library which is the following.
ld: error: /usr/local/Cellar/opencv/4.5.0_1/lib/libopencv_gapi.4.5.0.dylib: unknown file type
ld: error: /usr/local/Cellar/opencv/4.5.0_1/lib/libopencv_stitching.4.5.0.dylib: unknown file type
[...]
ld: error: /usr/local/Cellar/opencv/4.5.0_1/lib/libopencv_rapid.4.5.0.dylib: unknown file type
ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
iOS
The following is the commands that I am using to build the iOS static library (.a). I am using leetal's ​cmake toolchain file from this repository.
cmake ../.. -G Xcode -DCMAKE_TOOLCHAIN_FILE=../../ios.toolchain.cmake -DPLATFORM=OS64COMBINED -DBUILD_SHARED_LIBS=OFF
cmake --build . --config Release
The compilation of the static library seems to work because no error message is printed. However, when the library is used in the final Xamarin solution, the linked library cannot be found and the following error is shown.
Native linking failed, undefined symbol: cv::Mat::deallocate(). Please verify that all the necessary frameworks have been referenced and native libraries are properly linked in. (MT5210)
Question
What am I missing in order to properly compile and link OpenCV into my C++ library ?
I am working on macOS Big Sur and uses the following tools versions:
cmake : 3.20.0-rc5
ndk : 23.0.7196353
apple clang : 12.0.0
I hope that the description of my problem is clear enough and I thank you in advance for any help.
We had same problems, including Xamarin's DllNotFoundException with message from last comment, which led me to this topic. What fixed the exception for us in the end was linking statically to OpenCV *.a libs instead of linking to the shared libopencv_java4.so file. So we now have a huge 30MB nativelib.so file for each android ABI in build output, instead of a pair of small nativelib.so and libopencv_java4.so per ABI. CMakeLists looks like this:
set( OpenCV_DIR "~/opencv/build/OpenCV-android-sdk/sdk/native/jni" )
find_package( OpenCV REQUIRED )
target_link_libraries( # Specifies the target library.
nativelib
${OpenCV_LIBS})
Another thing in our project is we use OpenCV optional modules and had to create a custom OpenCV build, which I guess ensures our native library and OpenCV are compiled against same NDK version. I suppose using the prebuilt OpenCV distribution and compiling against a different NDK version could lead to problems too otherwise.

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.

Setting up Qt for CLion

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.

Can't link GLFW in qtcreator

I want to link GLFW. I already installed:
sudo apt-get install cmake make g++ libx11-dev libxi-dev libgl1-mesa-dev libglu1-mesa-dev libxrandr-dev libxext-dev
Then I create catalog with 2 sub catalogs build(cmake out source build) and libs(for external libraries).
from build catalog I run cmake with command "cmake .."
CMakeLists.txt in main catalog
cmake_minimum_required(VERSION 2.6)
project(MyProject) # project name
#version number
set(MyProject_Version_Major 1) #numer wersji glowny
set(MyProject_Version_Minor 0) #numer wydania
find_package(OpenGL REQUIRED)#when not found skip script
# add external subdirectory with another cmake file
add_subdirectory (libs)
include_directories(
libs/glfw-3.0.4/include/GLFW/
)
set(allLibs
${OPENGL_LIBRARY}
${GLFW_LIBRARIES}
)
add_executable(Manipulator main.cpp)
target_link_libraries(Manipulator ${allLibs})
CMakeLists.txt in libs catalog
GLFW
add_subdirectory (glfw-3.0.4)
include_directories(
glfw-3.0.4/include/GLFW/
)
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(OPENGL_LIBRARY
${OPENGL_LIBRARY}
${GLFW_LIBRARIES}
)
From official documentation I got information:
add_subdirectory(path/to/glfw) - done, main catalog CMakeList.txt
To be able to include the GLFW header from your code, you need to tell
the compiler where to find it.
include_directories(path/to/glfw/include) done, main catalog CMakeList.txt
Once GLFW has been added to the project, the GLFW_LIBRARIES cache
variable contains all link-time dependencies of GLFW as it is
currently configured. To link against GLFW, link against them and the
glfw target.
target_link_libraries(myapp glfw ${GLFW_LIBRARIES}) done, main catalog CMakeList.txt
Note that GLFW_LIBRARIES does not include GLU, as GLFW does not use
it. If your application needs GLU, you can add it to the list of
dependencies with the OPENGL_glu_LIBRARY cache variable, which is
implicitly created when the GLFW CMake files look for OpenGL.
target_link_libraries(myapp glfw ${OPENGL_glu_LIBRARY} done, main catalog CMakeList.txt
${GLFW_LIBRARIES})
Still don't working, got following error:
error: GLFW/glfw3.h: No such file or directory #include
I'm using QTCreator 3.0.1, Ubuntu 14.02 Ubuntu 14.04 lts.
In QTCreator I specified the main CMakeLists.txt directory, build directory (different then source)
Build directory - path to build directory main catalog/build
Working directory - path to main CMakeLists.txt directory
Run configoration: Manipulator
And still don't working. I found working examples on internet but I don't understand them( i.e. they create some directives no idea why).
Now it's works.
Main CMakeLists.txt
cmake_minimum_required(VERSION 2.6) #minimum CMake version
project(MyProject) #project name
set(MyProject_Version_Major 1) #project version
set(MyProject_Version_Minor 0)
find_package(OpenGL REQUIRED) #find opengl library - stops when not found
add_subdirectory (libs) # add another directory to project(contain glfw library)
#include glfw
include_directories(
libs/glfw-3.0.4/include
)
# create variable to store all libraries
set(allLibs
${GLFW_LIBRARIES}
)
#create Manipulator executable from main.cpp file
add_executable(Manipulator main.cpp)
#links alllibs variable with my executable
target_link_libraries(Manipulator glfw ${allLibs})
CMakeLists.txt in libs subdirectory.
### GLFW ###
add_subdirectory (glfw-3.0.4)
include_directories(
glfw-3.0.4/include/GLFW/
)
tl;dr -
I use this:
target_link_libraries(Manipulator ${allLibs})
instead of this:
target_link_libraries(Manipulator glfw ${allLibs})
Forgot glfw variable

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)