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

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.

Related

CMake Submodules Do Not Resolve Project Dependencies on MSVC "Cannot open include files"

I am using the following cmake file:
cmake_minimum_required(VERSION 3.19)
project(Neon LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
option(LIBIGL_WITH_OPENGL "Use OpenGL" ON)
option(LIBIGL_WITH_OPENGL_GLFW "Use GLFW" ON)
option(LIBIGL_WITH_OPENGL_GLFW_IMGUI "Use ImGui" ON)
include(libigl)
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
add_executable(Neon src/main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}}/include>
$<INSTALL_INTERFACE:include>)
target_link_libraries(Neon PUBLIC Eigen3::Eigen igl::core igl::opengl_glfw solvers)
add_subdirectory(solvers)
I have my main executable, and I have my subdirectory "solvers" which has some other numerical routines. The CMake file for that is as follows:
project(solvers)
add_library(${PROJECT_NAME} SHARED src/LinearElastic.cpp)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src)
install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}Config
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
target_link_libraries(${PROJECT_NAME} igl::core Eigen3::Eigen)
install(DIRECTORY include/ DESTINATION "${INSTALL_INCLUDE_DIR}")
install(EXPORT ${PROJECT_NAME}Config DESTINATION share/${PROJECT_NAME}/cmake)
export(TARGETS ${PROJECT_NAME} FILE ${PROJECT_NAME}Config.cmake
Eigen and IGL are both libraries which are found from the following:
if(TARGET igl::core)
return()
endif()
include(FetchContent)
FetchContent_Declare(
libigl
GIT_REPOSITORY https://github.com/libigl/libigl.git
GIT_TAG v2.3.0
)
# Note: In libigl v3.0.0, the following will become a one-liner:
# FetchContent_MakeAvailable(libigl)
FetchContent_GetProperties(libigl)
if(NOT libigl_POPULATED)
FetchContent_Populate(libigl)
endif()
list(PREPEND CMAKE_MODULE_PATH "${libigl_SOURCE_DIR}/cmake")
include(${libigl_SOURCE_DIR}/cmake/libigl.cmake)
Building the main executable has no issues whatsoever, however, when attempting to use the dependencies from the libigl library in the submodule, I am encountering a lot of problems where the imports are unable to resolve. In particular, when attempting to use Eigen, I get "Cannot open include file" errors.
I have tried googling this issue, but none seem to cover the case when I am using a custom-written include() for a library. Ordinarily I'd just find_package in the submodules, but this doesn't seem to work correctly. I assume this is something silly, or perhaps I'm misunderstanding. Please let me know if I can improve the clarity of the question.
The particular error is here:
#ifndef NEON_LINEARELASTIC_H
#define NEON_LINEARELASTIC_H
#include <Eigen/Dense> // "Cannot include" error
class LinearElastic {
public:
Eigen::MatrixXi foobar;
auto doIt() -> void;
};
#endif//NEON_LINEARELASTIC_
I was able to solve this problem. I am typically used to a unix-based environment, so when attempting to apply the same typical thinking to windows, I encountered a few issues, namely, the way different compilers handle shared linking. To avoid rehashing what is already known, I defer to this post to allow anyone else encountering this issue to solve the problem.
I changed the CMakeLists.txt for my main executable to the following:
cmake_minimum_required(VERSION 3.19)
project(Neon LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(INSTALL_LIB_DIR lib CACHE PATH "Install directory for library code")
set(INSTALL_BIN_DIR CACHE PATH "Install directory for executables")
set(INSTALL_INCLUDE_DIR include CACHE PATH "Install directory for header files")
list(PREPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
option(LIBIGL_WITH_OPENGL "Use OpenGL" ON)
option(LIBIGL_WITH_OPENGL_GLFW "Use GLFW" ON)
option(LIBIGL_WITH_OPENGL_GLFW_IMGUI "Use ImGui" ON)
include(libigl)
if(MSVC) // This line fixed it!
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
set(BUILD_SHARED_LIBS TRUE)
endif()
foreach(p LIB BIN INCLUDE CMAKE)
set(var INSTALL_${p}_DIR)
if(NOT IS_ABSOLUTE "${${var}}")
set(${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
endif()
endforeach()
find_package(Boost REQUIRED COMPONENTS unit_test_framework)
include_directories("${PROJECT_SOURCE_DIR}"
"${PROJECT_BINARY_DIR}")
add_executable(Neon src/main.cpp)
target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}}/include>
$<INSTALL_INTERFACE:include>)
target_link_libraries(Neon PRIVATE Eigen3::Eigen igl::core igl::opengl_glfw solvers)
add_subdirectory(solvers)
By setting the windows-specific options, the app was able to build and run without issue.

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")

Build helloworld GUI with QtCreator and 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)

Include the CGAL shared libraries to deploy a CGAL program

I'm building a C++ program using CGAL, and I'm writing CMake install rules to deploy said program so that I can CPack the result and the end user doesn't have to install CGAL or any of its dependencies to use it. In order to do that, I need to include every shared library (DLLs on Windows etc) but I can't find a CMake variable that lets me do that. I searched around in the CGAL repo, but no luck. I tried using ${CGAL_LIBRARIES} but those don't give paths, and it doesn't seem like ${CGAL_LIBRARIES_DIRS} is a thing.
My current CMakeLists is based off the one generated by the dedicated CGAL script :
# Created by the script cgal_create_CMakeLists
# This is the CMake script for compiling a set of CGAL applications.
project( MeshCleaner )
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
# CGAL and its components
find_package( CGAL QUIET COMPONENTS )
if ( NOT CGAL_FOUND )
message(STATUS "This project requires the CGAL library, and will not be compiled.")
return()
endif()
# Boost and its components
find_package( Boost REQUIRED )
if ( NOT Boost_FOUND )
message(STATUS "This project requires the Boost library, and will not be compiled.")
return()
endif()
# include for local directory
include_directories( BEFORE include )
# include for local package
# Creating entries for target: meshCleaner
# ############################
add_executable( ${PROJECT_NAME} main.cpp )
add_to_cached_list( CGAL_EXECUTABLE_TARGETS ${PROJECT_NAME} )
# Link the executable to CGAL and third-party libraries
target_link_libraries(${PROJECT_NAME} ${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES} )
install(TARGETS ${PROJECT_NAME} DESTINATION . COMPONENT Libraries)
message(${CGAL_LIBRARIES} ${CGAL_3RD_PARTY_LIBRARIES})
if(WIN32)
set(CMAKE_INSTALL_OPENMP_LIBRARIES TRUE)
include(InstallRequiredSystemLibraries)
endif()
include(${PROJECT_NAME}CPack)
The simplest way to "fix" that issue is to use CGAL as header-only. Please check the manual. Here is the direct link to the installation manual of CGAL-4.14, section "Header-only with CMake Configuration".

CMake Error at CMakeLists.txt (target_link_libraries)

I have a problem with CMake. I have wrote a CMakeList.txt file. But when I run it with Cmake I got a strange error "CMake Error at CMakeLists.txt:17 (target_link_libraries):
Cannot specify link libraries for target "debug" which is not built by this
project.".
Is it possible to create a Cmake file that can build a project file for Debug and Release mode at the same time? Or is there a simple way to fix this error?
My CMakeLists.txt looks like this:
cmake_minimum_required (VERSION 2.8)
project (SimuVille)
# Import required CMake files
set (CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules")
file(GLOB_RECURSE files
"*.cpp"
)
add_executable(debug ${files})
# Find the find Modules
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
#Find any version 2.X of SFML
#See the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
#Find SfeMovie
find_package(sfeMovie REQUIRED)
if(SFEMOVIE_FOUND)
include_directories(${SFEMOVIE_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFEMOVIE_LIBRARY})
endif()
#Find Assimp
find_package(ASSIMP REQUIRED)
if(ASSIMP_FOUND)
include_directories(${ASSIMP_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${ASSIMP_LIBRARY})
endif()
#Find DevIL
find_package(DevIL REQUIRED)
if(IL_FOUND)
include_directories(${IL_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${IL_LIBRARY})
target_link_libraries(${EXECUTABLE_NAME} ${ILU_LIBRARY})
target_link_libraries(${EXECUTABLE_NAME} ${ILUT_LIBRARY})
endif()
#Find opengl libs
find_package(OpenGL REQUIRED)
include_directories(${OpenGL_INCLUDE_DIRS})
link_directories(${OpenGL_LIBRARY_DIRS})
add_definitions(${OpenGL_DEFINITIONS})
if(NOT OPENGL_FOUND)
message(ERROR " OPENGL not found!")
endif(NOT OPENGL_FOUND)
#file(GLOB_RECURSE hfiles
# "*.h"
#)
#add_executable(SimuVille ${hfiles})
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/Game)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/GameEngine)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/GameEngine/SfmlObject)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/GameEngine/Camera)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/GameEngine/OpenglObject)
LINK_DIRECTORIES(${CMAKE_SOURCE_DIR}/GameEngine/Playable)
Edit: Added new source code.
Looks like your CMakeLists.txt doesn't contain either of two lines (which depends you are creating a library or a executable)
add_library(debug <files Name>)
OR
add_executable(debug <files Name>)
If you have these lines in your file, place it before target_link_libraries ()