Build helloworld GUI with QtCreator and CMAKE - cmake

I have build a hello world GUI with QtCreator using a qmake and .pro file.
I want to build the same hello world with QtCreator but with a CMAKE file
I followed this and was able to build the application successfully.
But when I run the program it does not show the main window GUI. In the application output it looks as following.
21:52:36: Starting /home/mavbot/qtopen3d/build-guiCMake-Desktop_Qt_5_12_0_GCC_64bit-Default/guiCMake...
Where am I missing out..please help. My CMAKE file looks as follows:
cmake_minimum_required(VERSION 3.1.0)
project(guiCMake)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
find_package(Qt5 COMPONENTS Widgets Core REQUIRED)
add_executable(guiCMake
mainwindow.ui
mainwindow.cpp
main.cpp
)
target_link_libraries(guiCMake Qt5::Core Qt5::Widgets)

Related

What is equivalent of qmake (.pro file) CONFIG -= console in CMAKE [duplicate]

This question already has answers here:
QML opens GUI window and console
(3 answers)
Closed 1 year ago.
I have build QML application and run it. But besides of GUI running console. I know how switch off console in qmake (.pro file) CONFIG -= console. But I do not understand how it works in CMake? I tried QML opens GUI window and console but it does not helped me.
CMakeLists.txt
cmake_minimum_required(VERSION 3.14)
project(APSMDClient LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# QtCreator supports the following variables for Android, which are identical to qmake Android variables.
# Check https://doc.qt.io/qt/deployment-android.html for more information.
# They need to be set before the find_package(...) calls below.
#if(ANDROID)
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
# if (ANDROID_ABI STREQUAL "armeabi-v7a")
# set(ANDROID_EXTRA_LIBS
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libcrypto.so
# ${CMAKE_CURRENT_SOURCE_DIR}/path/to/libssl.so)
# endif()
#endif()
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick Qml LinguistTools QuickControls2 REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick LinguistTools QuickControls2 REQUIRED)
set(TS_FILES APSMDClient_ru_RU.ts)
set(PROJECT_SOURCES
main.cpp
qml.qrc
${TS_FILES}
)
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(APSMDClient
${PROJECT_SOURCES}
)
qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
else()
if(ANDROID)
add_library(APSMDClient SHARED
${PROJECT_SOURCES}
)
else()
add_executable(APSMDClient
${PROJECT_SOURCES}
)
endif()
qt5_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
endif()
# Added
add_executable(APSMDClient WIN32 ${SOURCE_FILES})
target_link_libraries(APSMDClient PRIVATE Qt6::QuickControls2 Qt6::Qml)
target_compile_definitions(APSMDClient
PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(APSMDClient
PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick)
To disable the console window you can use either
add_executable(APSMDClient WIN32 ${PROJECT_SOURCES})
or
add_executable(APSMDClient ${PROJECT_SOURCES})
if (WIN32)
set_target_properties(APSMDClient PROPERTIES WIN32_EXECUTABLE TRUE)
endif()
qt_add_executable is a Qt wrapper for add_executable.

gtkmm 4.0 header build errors when compiling own project

I recently updated my gtkmm framework to version 4.0.0 and updated code to be compatible with the framework. I'm using msys2 in JetBrains CLion on Windows. However when I'm compiling project I got wall of warnings and errors in various framework headers like gtkmm, glibmm etc. I can't find errors which could be related to my code. Here is my Cmake configurations
cmake_minimum_required (VERSION 3.17.5)
set (CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS " -lstdc++fs")
project (Accounter)
find_package(Boost 1.73 COMPONENTS date_time REQUIRED)
FIND_LIBRARY (LIBZIP_LIBRARY NAMES zip)
FIND_PATH (LIBZIP_INCLUDE_DIR zip.h PATH_SUFFIXES include/zip include ) # Find header
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE(PkgConfig REQUIRED)
pkg_check_modules(GTKMM gtkmm-4.0)
include_directories( ${GTKMM_INCLUDE_DIRS} )
link_directories( ${GTKMM_LIBRARY_DIRS} )
#ADD_DEFINITIONS(${GTK_CFLAGS_OTHER})
FIND_PACKAGE_HANDLE_STANDARD_ARGS(libzip DEFAULT_MSG LIBZIP_LIBRARY LIBZIP_INCLUDE_DIR)
link_directories((Accounting))
add_subdirectory(Accounting)
link_directories(OdsFile)
add_subdirectory(OdsFile)
link_directories(GUI)
add_subdirectory(GUI)
if(Boost_FOUND)
include_directories(${Boost_INCLUDE_DIRS})
add_executable(Accounter main.cpp)
target_link_libraries(Accounter LINK_PUBLIC Accounting OdsFile GUI ${Boost_LIBRARIES} ${LIBZIP_LIBRARY} stdc++fs ${GTKMM_LIBRARIES} )
else()
message( FATAL_ERROR "Boost library is required for this library")
ENDIF().
The compile log is under this link . Do I miss something in cmake configuration? How it should be compiled? I'll provide additional info, files if it will be required. I think errors should be on my side not on framework.

SFML 2.5+ and CMake

With the removal of FindSFML.cmake in SFML 2.5, what is the preferred way of importing it?
I tried this, but it can't find SFMLConfig.cmake
cmake_minimum_required(VERSION 3.17)
project(untitled)
set(CMAKE_CXX_STANDARD 20)
find_package(SFML 2.5.1 COMPONENTS graphics REQUIRED)
add_executable(untitled "main.cpp")
target_link_libraries(untitled sfml-graphics)
The SFML directory is inside the project. I am using CMake with CLion on macOS Catalina
I'm also using clion and SFML inside a project directory and that's what I'm usually doing:
if(WIN32)
set(LIB_DIR libraries/win)
set(SHRD_EXT dll)
else()
set(LIB_DIR libraries/unix)
set(SHRD_EXT so)
endif(WIN32)
# find SFML 2.5
set(SFML_DIR ${LIB_DIR}/SFML-2.5.1/lib/cmake/SFML/)
find_package(SFML 2.5 COMPONENTS graphics window system audio network REQUIRED)```
In libraries/win and libraries/unix I have platform-specific sfml library.
I use sfml on both windows and linux depending on what device im on
My boiler plate CMakeLists.txt looks as follows:
cmake_minimum_required(VERSION 3.16)
project(AppName)
# Windows specific config
IF (WIN32)
# Include local sfml cmake config
set(SFML_DIR "C:/lib/SFML-2.5.1/lib/cmake/SFML")
# Link sfml statically (Optional)
set(SFML_STATIC_LIBRARIES TRUE)
ENDIF()
find_package(SFML 2.5.1 COMPONENTS graphics audio REQUIRED)
FILE(
GLOB
SOURCES
"src/*.cpp"
)
add_executable(AppName ${SOURCES})
target_link_libraries(AppName sfml-graphics sfml-audio)
target_include_directories(AppName
PRIVATE
"${PROJECT_BINARY_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/include"
)

cmake linker configuration for a project that uses wxWidgets

I have the following problem. I want to create a cross-platform GUI application that uses wxWidgets
I am trying to compile and run a "Hello World" example from wxWidgets web site.
I want to use cmake to build the project. On Linux everything works without problems, but on Windows I see the following linking error
MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
It looks like the linker is looking for main function for some reason (shouldn't it look for WinMain?)
The example code has wxIMPLEMENT_APP(MyApp); macro, I have also set the linker option
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /subsystem:windows"), but nothing helps, so I don't understand what is the problem. I have a feeling that I missed something simple. May be you had a similar problem before with one of your projects? Thank you for your help.
My CMakeLists.txt file is given below
cmake_minimum_required (VERSION 3.8)
project ("project")
set(CMAKE_CXX_STANDARD 17)
set(WXWIN "C:/msys64/wxWidgets")
# =========================================================================
set(BUILD_DEPS ON)
list(APPEND CMAKE_MODULE_PATH "include/libs")
include_directories("include/libs")
add_subdirectory("include/libs/glog")
if (MSVC)
set(wxWidgets_ROOT_DIR ${WXWIN})
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /subsystem:windows")
endif (MSVC)
find_package(wxWidgets COMPONENTS net gl core base)
include(${wxWidgets_USE_FILE})
set(wxWidgets_USE_UNICODE ON)
# Create shared library for the project
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(PROJECT_LINK_LIBS Tools)
set(LIBRARY_SRC include/Tools.cpp)
add_library(Tools SHARED ${LIBRARY_SRC})
link_directories("out/build")
set_target_properties(Tools PROPERTIES POSITION_INDEPENDENT_CODE 1)
# =========================================================================
# Add source to this project's executable.
add_executable (project "project.cpp" "project.h")
target_compile_features(project PUBLIC cxx_std_17)
target_link_libraries (project ${PROJECT_LINK_LIBS} ${wxWidgets_LIBRARIES})
It turns out that to make it work in need to put WIN32 into add_executable command of cmake
add_executable (project "project.cpp" "project.h") => add_executable (project WIN32 "project.cpp" "project.h")

Ogre Error In Cmake

While implementing CMake in my code I am getting an ogre error:
/usr/bin/ld: cannot find -lOGRE
My CMakeLists.txt file looks like:
#Specify the version being used aswell as the language
cmake_minimum_required(VERSION 2.6)
#Name your project here
project(eCAD)
#sets cmake to run moc when needed
set(CMAKE_AUTOMOC ON)
#find requirements of this projects
find_package(Qt5Widgets)
find_package(Qt5Core)
find_package(OGRE)
find_package(OIS)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#Sends the -std=c++11 flag to the gcc compiler
add_definitions(-std=c++11)
qt5_wrap_ui(Cmake_form_hdr resources/ui/mainwindow.ui)
#This tells CMake to main.cpp and name it eCAD
add_executable(eCAD main.cpp ${Cmake_form_hdr})
#include the subdirectory containing our libs
add_subdirectory (gui)
include_directories(gui)
#link_libraries
target_link_libraries(eCAD Qt5::Widgets Qt5::Core OGRE OIS)
I am new to this. Please help me out to solve the problem
The command find_package(OGRE) runs file FindOGRE.cmake and sets variables OGRE_INCLUDE_DIRS and OGRE_LIBRARIES. To link with the OGRE library, you should use these variables, e.g.
target_include_directories(eCAD PRIVATE ${OGRE_INCLUDE_DIRS})
target_link_libraries(eCAD ${OGRE_LIBRARIES})
This is same for all of the external libraries you use.