Background
I'm currently using Eclipse Neon.3 and have installed the "C/C++ CMake Build Support - Experimental" package (I'm not using CMake's Eclipse generator). I have a simple program that uses Qt 5.8 which builds successfully, however, Eclipse seems unable to index Qt symbols(e.g. QCoreApplication, QDebug, etc...).
The symptoms of this are:
No code completion suggestions
#include <QtCore> and other include statements are shown as unresolved
Qt symbols such as QCoreApplication, QDebug(), and QCoreApplication.exec() are shown as not resolved.
Code
CMakeLists.txt file
cmake_minimum_required(VERSION 3.5)
project(test-program)
set(CMAKE_CXX_STANDARD 11)
# Put the CMake files for Qt5 in the Prefix path.
set(Qt5_DIR /opt/Qt/5.8/gcc_64/lib/cmake/Qt5/)
#Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
#Find the Qt5Core Library
find_package(Qt5 REQUIRED COMPONENTS Core Widgets)
set(SOURCE_FILES
src/main.cpp)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} Qt5::Core)
main.cpp (shown with eclipse annotations)
#include <QtCore> //Unresolved inclusion: <QtCore>
#include <QDebug> //Unresolved inclusion: <QDebug>
int main(int argc, char** argv){
QCoreApplication application(argc, argv);
//Type 'QCoreApplication' could not be resolved
qDebug() << "Test";
//Function 'qDebug' could not be resolved
application.exec();
//Method 'exec' could not be resolved
return 0;
}
Question
So my question is this: How can I get Eclipse to recognize Eclipse to recognize Qt symbols? Or is that just not possible at this time?
Did you enable the "CDT GCC Build Output Parser"? This is an Eclipse feature to parse the output of the build and guesses the include paths automatically. You can find it unter Project Properties->C/C++ General->Preprocessor Include Paths, Macros etc. and then under the tab Providers.
In order for this feature to work properly, a detailed build report must be generated. You can achieve this by either changing the build command under Preferences->C/C++ Build to make VERBOSE=1 or by specifying set(CMAKE_VERBOSE_MAKEFILE On) inside your CMakeLists.txt.
See also Eclipse Help - Scanner Discovery Preferences
Related
I have qt/6.4.1 as a dependancy in my project.
My conanfile.py looks like this:
class RainEditorConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
requires = "qt/6.4.1" # comma-separated list of requirements
default_options = {"qt:shared": False, "qt:opengl": "desktop", "qt:qtsvg": True}
Since these set of options are not present on the qt repository on conancenter, I build qt from source using the --build=qt flag
Now, my CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.16)
project(helloworld VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(helloworld
src/main.cpp
src/res.qrc
)
target_include_directories(helloworld PRIVATE src)
# Link to Qt
find_package(Qt6 REQUIRED COMPONENTS Widgets QWindowsIntegrationPlugin Svg QSvgPlugin)
qt_standard_project_setup()
target_link_libraries(helloworld PRIVATE Qt6::Widgets Qt6::QWindowsIntegrationPlugin Qt6::Svg Qt6::QSvgPlugin)
Now, my main.cpp tries to display an SVG resource (defined in the qrc file):
#include <QtWidgets>
#include <iostream>
#include <QtPlugin>
Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
Q_IMPORT_PLUGIN(QSvgPlugin)
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
// Create a window.
QMainWindow mainWindow;
mainWindow.setWindowTitle("Example");
mainWindow.resize(800, 600);
QLabel* l = new QLabel();
QPixmap pixmap(":/close.svg");
l->setPixmap(pixmap);
// Add label to the window.
mainWindow.setCentralWidget(l);
mainWindow.show();
return app.exec();
}
When I configure my cmake project, I get the following error:
Target "helloworld" links to:
[cmake]
[cmake] Qt6::QSvgPlugin
[cmake]
[cmake] but the target was not found.
If I remove the linking to QSvgPlugin, then it configures correctly but I get the following error while building my project:
error LNK2019: unresolved external symbol "struct QStaticPlugin const __cdecl qt_static_plugin_QSvgPlugin(void)"
Now, this symbol is defined in the plugin library qsvgd.lib which is inside the conan installation, so if I link to this manually by writing out the full path, it builds and runs correctly:
target_link_libraries(helloworld PRIVATE Qt6::Widgets Qt6::QWindowsIntegrationPlugin Qt6::Svg "C://.conan//8126c6//1//res//archdatadir//plugins//imageformats//qsvgd.lib")
If I don't link to this plugin, and remove the Q_IMPORT_PLUGIN(QSvgPlugin) line from my source code, then it configures and builds correctly but cannot display the SVG when run
Which target should I link to, to link to the correct svg plugin library? It seems that the conan qt repository's exported cmake files do not expose this library as a target.
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.
I want to be able to run my Qt applications from CLion. In order to do so, I'm using MinGW, CLion, and Qt 5.15.0. In Settings > Build, Execution, Deployment > CMake, I've set my CMake Options as the following:
-DCMAKE_PREFIX_PATH=/Qt/5.15.0/mingw81_64/lib/cmake
In Settings > Build, Execution, Deployment > Toolchains, I've set my Environment as the following:
C:\Qt\Tools\mingw810_64
The C and C++ compilers have been automatically detected so I'm not too worried about that. For the file main.cpp, I have the following:
#include <QApplication>
#include <QDebug>
int main() {
qDebug() << QT_VERSION_STR;
return 0;
}
For CMakeLists.txt, I have the following:
cmake_minimum_required(VERSION 3.3)
project(Practice)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
find_package(Qt5Widgets REQUIRED)
add_executable(Practice ${SOURCE_FILES})
target_link_libraries(Practice Qt5::Widgets)
Building this test project gives me no warnings or errors but running it does. Why is this? Here's the error code I get when it does run.
C:\Users\IvanJaramillo\CLionProjects\Practice\cmake-build-debug\Practice.exe
Process finished with exit code -1073741511 (0xC0000139)
Changing compilers from MinGW to Cygwin and changing the versions of it hasn't done the job.
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.
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)