I want to include gtk to my new cmake project. But wen I compile, I get this error: +
In file included from /home/chhu/CLionProjects/MasterMind/main.cpp:2:
/usr/include/gtkmm-3.0/gtkmm.h:87: fatal error: glibmm.h: could not find such directory or file
This is my cmake file:
cmake_minimum_required(VERSION 3.17)
project(MasterMind)
set(CMAKE_CXX_STANDARD 14)
# Use the package PkgConfig to detect GTK+ headers/library files
FIND_PACKAGE(PkgConfig REQUIRED)
PKG_CHECK_MODULES(GTK3 REQUIRED gtk+-3.0)
# Setup CMake to use GTK+, tell the compiler where to look for headers
# and to the linker where to look for libraries
INCLUDE_DIRECTORIES(${GTK3_INCLUDE_DIRS})
LINK_DIRECTORIES(${GTK3_LIBRARY_DIRS})
# Add other flags to the compiler
ADD_DEFINITIONS(${GTK3_CFLAGS_OTHER})
add_executable(MasterMind main.cpp)
# Link the target to the GTK+ libraries
TARGET_LINK_LIBRARIES(MasterMind ${GTK3_LIBRARIES})
This is my class:
#include <gtkmm-3.0/gtkmm.h>
int main(int argc, char *argv[]) {
auto app =
Gtk::Application::create(argc, argv,
"org.gtkmm.examples.base");
Gtk::Window window;
window.set_default_size(200, 200);
return app->run(window);
}
I am developing under ubuntu 20.4. My g++ version is 9.3.0.
Gtkmm is not a part of Gtk. You should update your CMakeLists
PKG_CHECK_MODULES(GTKMM3 REQUIRED gtkmm-3.0)
INCLUDE_DIRECTORIES(${GTKMM3_INCLUDE_DIRS})
LINK_DIRECTORIES(${GTKMM3_LIBRARY_DIRS})
ADD_DEFINITIONS(${GTKMM3_CFLAGS_OTHER})
TARGET_LINK_LIBRARIES(MasterMind ${GTKMM3_LIBRARIES})
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'm trying to use sdl on ubuntu. According to this instruction(https://gist.github.com/BoredBored/3187339a99f7786c25075d4d9c80fad5) i installed sdl2, sdl image and sdl mixer. Now I have to link them while building. Example how should I do it below.
g++ myProgram.cpp -o myProgram `sdl2-config --cflags --libs` -lSDL2 -lSDL2_mixer -lSDL2_image -lSDL2_ttf
I'm using Cmake and I have no idea how to link them...
Below it's code done just for testing sdl working or not.
//MAIN
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
int main(int argc, char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
}
CMakeList below
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (sdl)
# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/main.cpp
)
# Add an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES})
# Set the directories that should be included in the build command for this target
# when running g++ these will be included as -I/directory/path/
target_include_directories(sdl
PRIVATE
${PROJECT_SOURCE_DIR}/inc
)
How can I link them in Cmake? Thanks for your time.
To link a library (shared/static) in cmake you can use the target_link_libraries command:
target_link_libraries(<target> ... <item>... ...)
According to the documentation:
<target> must have been created by a command such as add_executable() or add_library()
So first of all we need to find the SDL library, for that we will use the command:
find_package(SDL2 REQUIRED)
to make it's include directories available to you, use the command:
include_directories(${SDL2_INCLUDE_DIRS})
And finally to link SDL2, you need to do:
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARIES})
or alternatively:
target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2)
PRIVATE, means that ${PROJECT_NAME} uses SDL2 in its implementation, but SDL2 is not used in any part of ${PROJECT_NAME}'s public API. More here
Here ${PROJECT_NAME} is the <target>, and all the rest that follow are names of libraries.
Final Result
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (sdl)
find_package(SDL2 REQUIRED)
# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/main.cpp
)
# Add an executable with the above sources
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(sdl ${SDL2_LIBRARIES})
# Set the directories that should be included in the build command for this target
include_directories(SDL2Test ${SDL2_INCLUDE_DIRS})
Refs:
https://cmake.org/cmake/help/latest/command
https://cmake.org/pipermail/cmake/2016-May/063400.html
EDIT added the full CMAKE
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (sdl)
# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/main.cpp
)
target_include_directories(sdl
PRIVATE
${PROJECT_SOURCE_DIR}/inc
)
# Add an executable with the above sources
link_directories(path_to_lib)
add_executable(${PROJECT_NAME} ${SOURCES})
# Set the directories that should be included in the build command for this target
# when running g++ these will be included as -I/directory/path/
target_link_libraries((${PROJECT_NAME} SDL2 SDL2_mixer SDL2_image SDL2_ttf)
I'm attempting to compile a "Hello World!" application using Qt but on compilation I'm getting the following error: ‘class QApplication’ has no member named ‘setMainWidget’ and I'm not sure why, my source file looks like:
/****************************************************************
**
** Qt tutorial 1
**
****************************************************************/
#include <qapplication.h>
#include <qpushbutton.h>
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QPushButton hello( "Hello world!", 0 );
hello.resize( 100, 30 );
a.setMainWidget( &hello );
hello.show();
return a.exec();
}
and I created the Makefile with cmake using following CMakeLists.txt:
cmake_minimum_required(VERSION 3.1.0)
project(hello_world)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed
set(CMAKE_AUTOMOC ON)
# Create code from a list of Qt designer ui files
#set(CMAKE_AUTOUIC ON)
# Find the QtWidgets library
find_package(Qt5 COMPONENTS Widgets REQUIRED)
# Populate a CMake variable with the sources
set(hello_world_SRCS
main.cpp
)
# Tell CMake to create the helloworld executable
add_executable(hello_world WIN32 ${hello_world_SRCS})
# Use the Widgets module from Qt 5
target_link_libraries(hello_world Qt5::Widgets)
What am I doing wrong?
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