How do I translate this CMake file to QMake? - c++

Minimal example .zip
I'm trying to use SoLoud in my Qt project.
I managed to successfully compile & use it with this cmake file:
file(GLOB_RECURSE AUDIOSOURCE ${CMAKE_CURRENT_LIST_DIR}/SoLoud/src/audiosource/*.c*)
file(GLOB_RECURSE CORE ${CMAKE_CURRENT_LIST_DIR}/SoLoud/src/core/*.c*)
file(GLOB_RECURSE FILTER ${CMAKE_CURRENT_LIST_DIR}/SoLoud/src/filter/*.c*)
add_compile_definitions(WITH_MINIAUDIO)
set(SOLOUD_SRCS
SoLoud/src/backend/miniaudio/soloud_miniaudio.cpp
${CMAKE_CURRENT_LIST_DIR}/SoLoud/src/c_api/soloud_c.cpp
${AUDIOSOURCE}
${CORE}
${FILTER}
)
add_executable(output main.cpp ${SOLOUD_SRCS})
target_include_directories(output PUBLIC ${CMAKE_CURRENT_LIST_DIR}/SoLoud/include)
My main project is built with Qmake. So I'm trying to translate it...
INCLUDEPATH += SoLoud/include
DEFINES += "WITH_MINIAUDIO=\"\""
SOURCES += SoLoud/src/backend/miniaudio/soloud_miniaudio.cpp \
SoLoud/src/c_api/soloud_c.cpp \
$$files("SoLoud/src/audiosource/*.c*", true) \
$$files("SoLoud/src/core/*.c*", true) \
$$files("SoLoud/src/filter/*.c*", true)
HEADERS += $$files("SoLoud/include/*.h", true) \
This produces a wall of errors:
https://pastebin.com/pUQuysEr
Why?
BTW: i'm aware that recursive search of *.c* files is bad practice. However, that's what library's authors recommend and I gave up trying to find every single .cpp i need (there were MANY).

Related

how to convert qmake.pro file to cmake [duplicate]

I have a .pro file on my project, but now I want to port it to a CMakeLists.txt file. How can I do this?
QT += core
QT -= gui
CONFIG += c++11
TARGET = test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
QT += network
SOURCES += main.cpp \
test_interface.cpp \
motomanlibrary.cpp \
processing.cpp
SOURCES += main.cpp \
test_interface.h \
motomanlibrary.h \
processing.h
QMake: The required libraries.
QT += core
QT -= gui
QT += network
CMake: only the add is necessary. An exclude (QT -= gui) is not required.
find_package(Qt5Core REQUIRED)
find_package(Qt5Network REQUIRED)
QMake: Additional Compiler flags:
CONFIG += c++11
CMake: Extend the list of compiler flags as required.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
QMake: The source files
SOURCES += main.cpp \
test_interface.cpp \
motomanlibrary.cpp \
processing.cpp
CMake: Create a list of source files
set(SOURCES
main.cpp
test_interface.cpp
motomanlibrary.cpp
processing.cpp
)
QMake: The header to be included:
SOURCES += main.cpp \
test_interface.h \
motomanlibrary.h \
processing.h
CMake: Just show where the header files are.
include_directory(.) # or include_directory(${CMAKE_CURRENT_SOURCE_DIR})
include_directory(some/where/else)
QMake: The target to be built:
TARGET = test
CMake: Set the name of the target, add the sources, link the required libs.
add_executable(test ${SOURCES} )
qt5_use_modules(test Core Network) # This macro depends from the Qt version
# Should not be necessary
#CONFIG += console
#CONFIG -= app_bundle
#TEMPLATE = app
See further details on Convert qmake to cmake
There is a python script to convert QMake to CMake on a WIP branch of Qt Base: https://code.qt.io/cgit/qt/qtbase.git/tree/util/cmake/pro2cmake.py?h=wip/cmake
It will probably be released with Qt 6 when CMake will become the main build system.
This is what you would typically write in a simple Qt application built with cmake consuming Qt. A few gotchas:
Use automoc. This reduces the maintenance overhead significantly. It is also fast enough even for large projects so that you do not need to care about build-time slow-down.
Use versionless cmake targets so that they are compatible across Qt versions.
If you can, take advantage of the QT_DISABLE_DEPRECATED_BEFORE variable.
As a bonus, enable some usual warning, error and sanitizer detections if you aim for high quality.
You may or may not want to add a Qt 5 fallback in case anyone would like to use your software with Qt 5. This requirement may phase out slowly, but surely.
project(Application VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
find_package(Qt6 COMPONENTS Widgets)
if (NOT Qt6_FOUND)
find_package(Qt5 5.15 REQUIRED COMPONENTS Widgets)
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "(Clang|GNU)")
add_compile_options(-Wall -Wpedantic -Wextra -Werror)
add_compile_options(-fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer)
add_link_options(-fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer)
elseif (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
set_property(DIRECTORY APPEND PROPERTY COMPILE_OPTIONS "/w")
endif()
add_compile_definitions(QT_DISABLE_DEPRECATED_BEFORE=0xFFFFFF)
add_executable(Application
mainwindow.cpp
main.cpp
)
target_link_libraries(Application Qt::Widgets)

Qt - check availability of linked .lib on windows with preprocessor directives

I'm trying to use preprocessor directives in C++ for avoiding to compile code that requires a .lib, in case the library cannot be linked.
My .pro file contains:
INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"
and my directives are of the form:
#ifdef _WINSCARD_H_
// do something
#endif
or
#ifndef _WINSCARD_H_
// do something
#endif
This winscard comes with this windows sdk and I can definitely use its functionalities. The problems arise when I try to restrict the compilation based on these conditional directives.
Code compiles fine when using
INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"
in the .pro file.
Code is skipped during the compilation phase, as if the library is missing when using the above .pro configurations and the conditional directive, even though the library is available and linked:
#ifdef _WINSCARD_H_
// code that needs to be compiled only when library is present and linked.
#endif
The only change is the introduction of #ifdef _WINSCARD_H_.
It is possible to generate conditional build in qmake basing on file existence.
You can add in your .pro file something like that
exists( C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86/winscard* ) {
message( "Configuring for winscard..." )
INCLUDEPATH += "C:/Program Files/Windows Kits/8.0/Include/um"
LIBS += -L"C:/Program Files/Windows Kits/8.0/Lib/win8/um/x86" -l"winscard"
DEFINES += _WINSCARD_H_
}
The block after the built-in function exists() is parsed only when the path is found (it is possible to use asterisk to match part of filename). Here _WINSCARD_H_ is defined only if required file is found.
So, that macro can be used in source code for conditional compilation.
See qmake Test Functions Reference for details.

link boost libs to qt with msvc

I have installed qt-opensource-windows-x86-msvc2013_64_opengl-5.4.0.exe and compiled boost_1_58_0.zip with this command: b2 toolset=msvc --build-type=complete stage. It works fine with Visual Studio, but when I try use it with Qt I get this error:
:-1: error: LNK1104: cannot open file 'libboost_filesystem-vc120-mt-gd-1_58.lib'
Here is my .pro file:
TEMPLATE = app
QT += qml quick widgets
SOURCES += main.cpp \
testclass.cpp
RESOURCES += qml.qrc
INCLUDEPATH += C:\boost
LIBS += "-LC:\boost\stage\lib\libboost_filesystem-vc120-mt-gd-1_58.lib"
#Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =
# Default rules for deployment.
include(deployment.pri)
HEADERS += \
testclass.h
In the LIBS variable use just "-L" for correct library path (-L). You made a mix, specifying a file (lowercase l) while libs directory is missing.
You do not need to specify the library, boost has pragmas for that.

How do you tell a Qt app to depend on a Qt lib?

I have a codebase where there are some shared libraries. For example LIB1 does some sort of processing that APP1 needs (depends on).
The qmake *.pro files are setup like the following. There is one root *.pro that is a TEMPLATE=subdirs that lists APP1 and LIB1 as it's subdirs.
LIB1.pro:
TARGET = LIB1
TEMPLATE = lib
QT += core
QT += xml
DESTDIR = path/to/libs/directory
SOURCES += \
File1.cpp \
FileN.cpp
HEADERS += \
File1.h \
FileN.h
APP1.pro:
#------ dependencies ------#
LIBS += -Lpath/to/libs/directory
# LIB1
INCLUDEPATH += path/to/libs/directory/LIB1lib
DEPENDPATH += path/to/libs/directory/LIB1lib
LIBS += -lLIB1
HEADERS = \
FileNplus1.h \
FileM.h
SOURCES = \
FileNplus1.cpp \
FileM.cpp
The problem is that when you compile the root *.pro file LIB1 will compile but APP1 fails to compile with the error QDomElement: No such file or directory because APP1.pro doesn't have QT += xml.
There is a hack that I use but I would like to overcome this hack. The hack involves adding the following line to APP1.pro:
# add this to APP1.pro... let's it compile again
QT += xml
Is there anyway to setup your qmake *.pro files such that, APP1.pro depends on LIB1 without needing to modify APP1.pro to add the QT += xml?
(The reason for this question is let's say you have other libraries that depend on other stuff... I would like to have Qt/Qmake take care of the dependencies like for example the Qt += xml dependency.)
qmake allows you to create your own configuration features. (last paragraph)
So you can create your own feature and move your lib1-linking code to it. You can add QT+=xml here. And in app.pro you'll just write CONFIG += lib1

Corresponding CMakeList for a Qt pro file

I need to convert Qt Project to CMake , as i want to integrate it with other projects which are already in CMake .
The Qt .Pro file is as follows ,
TEMPLATE = app
INCLUDEPATH += ./lib
# Input
HEADERS += dollar/GestureTemplate.h \
dollar/PathWriter.h \
dollar/GeometricRecognizerTypes.h \
dollar/GeometricRecognizer.h \
dollar/SampleGestures.h \
lib/GestureTemplate.h \
lib/PathWriter.h \
lib/GeometricRecognizerTypes.h \
lib/GeometricRecognizer.h \
lib/SampleGestures.h \
lib/MultiStrokeGestureTemplate .h \
lib/MultiStrokeGestureTemplate .h \
lib/MultiStrokeGestureTemplate .h \
lib/MultiStrokeGestureTemplate .h \
lib/SampleMultiStrokeGestures.h \
lib/MultipleStrokeGestureTemplate.h \
lib/utils.h
SOURCES += main.cpp \
lib/GeometricRecognizer.cpp
LIBS += -L/usr/lib \
-lml \
There is only one directory Lib and in the main path there is main.cpp , all the other files are inside Lib directory .
I don't know much about cmake i have come up with the following cmake list for the above qt project.
cmake_minimum_required (VERSION 2.6)
Project(dollar)
INCLUDE_DIRECTORIES("lib")
# Make Executable
ADD_EXECUTABLE(main main.cpp)
# Link the executable to the Hello library.
TARGET_LINK_LIBRARIES(main -lml -L/usr/lib)
cmake succeeds but after that make gives me many errors ,see the error log -> http://www.text-upload.com/read,4022366863337 .What all additions i require to make in CMake List ?.
It seems that you have missed lib/GeometricRecognizer.cpp file
# Make Executable
ADD_EXECUTABLE(main main.cpp lib/GeometricRecognizer.cpp)