CMake & QT5 & Conan // CMAKE_AUTOUIC not generating ui header files - qt

I've tried solution given in this thread: CMake & QT5 - QT5_WRAP_UI not generating ui header files but nothing changes.
Here is my first CMakeLists.txt (at the root of the project, which calls the second one)
cmake_minimum_required (VERSION 3.11.2)
project(babel)
include(${CMAKE_SOURCE_DIR}/build/conanbuildinfo.cmake)
conan_basic_setup()
subdirs(client)
and the second one is:
include_directories(${babel_SOURCE_DIR}/client/inc)
include_directories(${babel_SOURCE_DIR}/common)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR})
set(babel_client_SRCS
main.cpp
mainwindow.cpp
)
set(CMAKE_CXX_FLAGS "-Wall -fPIC -std=c++11")
set(CMAKE_AUTOUIC ON)
set(AUTOGEN_BUILD_DIR ${CMAKE_SOURCE_DIR/client/inc})
add_executable(babel_client ${babel_client_SRCS})
target_link_libraries(babel_client ${CONAN_LIBS})
my main.cpp got #include "ui_mainwindow.h"
and here is the error output:
[ 33%] Building CXX object client/src/CMakeFiles/babel_client.dir/main.cpp.o
/plopPath/client/src/main.cpp:1:10: fatal error: ui_mainwindow.h: No such file or directory
#include "ui_mainwindow.h"
^~~~~~~~~~~~~~~~~
compilation terminated.
gmake[2]: *** [client/src/CMakeFiles/babel_client.dir/build.make:63: client/src/CMakeFiles/babel_client.dir/main.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:104: client/src/CMakeFiles/babel_client.dir/all] Error 2
gmake: *** [Makefile:84: all] Error 2
Any Ideas? I've tried putting the set(CMAKE_AUTOUIC ON) at different places in my CMaleLists.txt but it looks like nothing changes (there is NO ui_mainwindow.h created in the project folders)
PS: I'm using the multiplatform binary manager CONAN to get Qt (using Qt without .ui files is working perfectly fine however)

As shown in the documentation http://doc.qt.io/qt-5/cmake-manual.html, you have to add the mainwindow.ui file to babel_client_SRCS so that it is then passed to add_executable

Related

An error occurred when the ASAP(Automated Slide Analysis Platform) software was running

With the enthusiastic help of SO netizens, I configured all the environments required for ASAP (Automated Slide Analysis Platform)
The necessary libraries including Boost, Openslide, Qt, etc. have been successfully linked, but the following error occurred
====================[ Build | ASAP | Debug ]====================================
"D:\_Jason\Clion\CLion 2021.2.2\bin\cmake\win\bin\cmake.exe" --build D:\_Jason\C++_hospital\orignal\CLion1.9\ASAP-1.9\cmake-build-debug --target ASAP -- -j 9
[ 25%] Automatic MOC and UIC for target ASAP
[ 25%] Built target ASAP_autogen
[ 50%] Linking CXX executable ASAP.exe
CMakeFiles\ASAP.dir/objects.a(main.cpp.obj): In function `main':
D:/_Jason/C++_hospital/orignal/CLion1.9/ASAP-1.9/ASAP/main.cpp:6: undefined reference to `__imp__ZN12QApplicationC1ERiPPci'
D:/_Jason/C++_hospital/orignal/CLion1.9/ASAP-1.9/ASAP/main.cpp:7: undefined reference to `__imp__ZN4ASAPC1EP7QWidget'
D:/_Jason/C++_hospital/orignal/CLion1.9/ASAP-1.9/ASAP/main.cpp:8: undefined reference to `__imp__ZN7QWidget4showEv'
D:/_Jason/C++_hospital/orignal/CLion1.9/ASAP-1.9/ASAP/main.cpp:10: undefined reference to `__imp__ZN12QApplication4execEv'
D:/_Jason/C++_hospital/orignal/CLion1.9/ASAP-1.9/ASAP/main.cpp:7: undefined reference to `__imp__ZN4ASAPD1Ev'
D:/_Jason/C++_hospital/orignal/CLion1.9/ASAP-1.9/ASAP/main.cpp:6: undefined reference to `__imp__ZN12QApplicationD1Ev'
D:/_Jason/C++_hospital/orignal/CLion1.9/ASAP-1.9/ASAP/main.cpp:7: undefined reference to `__imp__ZN4ASAPD1Ev'
D:/_Jason/C++_hospital/orignal/CLion1.9/ASAP-1.9/ASAP/main.cpp:6: undefined reference to `__imp__ZN12QApplicationD1Ev'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\ASAP.dir\build.make:111: ASAP.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:163: CMakeFiles/ASAP.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:170: CMakeFiles/ASAP.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:168: ASAP] Error 2
It can be seen from "__imp__ZN12QApplicationD1Ev" that this is a Qt problem. The solution they gave in other forums is to add the statement QT += gui widgets core to the .pro file under this project.However, there is no .pro file in the ASAP source code, so I tried to use the qmake -project command to generate the .pro file. It still doesn't work after adding QT += gui widgets core

Qt. Copying file error while trying to run the project. How to fix a mistake and why is it happening?

I’m trying to install and create the first Qt project. I am using CLion with CMake and Qt 6.
I set up CMake:
cmake_minimum_required(VERSION 3.17)
project(QSnake)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(QT_VERSION 6)
set(REQUIRED_LIBS Core Gui Widgets)
set(REQUIRED_LIBS_QUALIFIED Qt6::Core Qt6::Gui Qt6::Widgets)
set(CMAKE_PREFIX_PATH D:/Qt/6.0.2/mingw81_64/lib/cmake)
add_executable(${PROJECT_NAME} main.cpp)
if(NOT CMAKE_PREFIX_PATH)
message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it "
"(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)")
endif()
find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})
if(WIN32)
set(DEBUG_SUFFIX)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
set(DEBUG_SUFFIX "d")
endif()
set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
if(NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
if(NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
endif()
endif()
if(EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
endif()
foreach(QT_LIB ${REQUIRED_LIBS})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endforeach(QT_LIB)
endif()
Actually, this is how it was generated when the CLion project was created. I only set the path: D:/Qt/6.0.2/mingw81_64/lib/cmake
Just in case, I will also add the main.c code. It is also generated by CLion.
#include <QApplication>
#include <QPushButton>
int
main(int argc,
char *argv[])
{
QApplication a(argc, argv);
QPushButton button("Hello world!", nullptr);
button.resize(200, 100);
button.show();
return QApplication::exec();
}
I don’t see any problems with CMake, but when I try to run the project there is an error:
Error copying file "D:/Qt/6.0.2/mingw81_64/lib/cmake/../../bin/Qt6Cored.dll" to "D:/QSnake/cmake-build-debug".
mingw32-make[3]: *** [CMakeFiles\QSnake.dir\build.make:126: QSnake.exe] Error 1
mingw32-make[3]: *** Deleting file 'QSnake.exe'
mingw32-make[2]: *** [CMakeFiles\Makefile2:96: CMakeFiles/QSnake.dir/all] Error 2
mingw32-make[1]: *** [CMakeFiles\Makefile2:103: CMakeFiles/QSnake.dir/rule] Error 2
mingw32-make: *** [Makefile:137: QSnake] Error 2
What is this error and how do I fix it? I understand that there is no problem with build - only a copying error. And should copied dll be called "Cored" and not "Core"?
Thank you for your attention! If you need any additional information, please let me know - I will try to clarify the question.
Problem solved.
Whether it is useful to anyone, but the CMake automatically generated by CLion contains rows that cause an error in debug mode (adds 'd' suffix to all dll files):
if (CMAKE_BUILD_TYPE MATCHES "Debug")
set(DEBUG_SUFFIX "d")
endif()
Deleting those lines saved me :)
The real answer to this is that you are using a CMake profile defined as "debug", while you have not installed a debug version of Qt.
To solve, and not patch, if you don't want to download the debug DLLs, then create a CMake profile for "release".
I had the same problem and none of the answers here solved the problem.
Here is how I could solved it for myself:
TL;DR:
set prefix-path when creating project
copy missing Qt6Cored.dll into bin-folder
When creating the new project in Clion be sure to set the correct prefix-path:
You can see in your error message that /bin/Qt6Cored.dll gets added to the prefix path automatically, so you don't have to select the bin folder when setting the prefix-path.
After this I still got an error message (but path was displayed correctly without any "/../..").
I then realized that there was no Qt6Cored.dll in my bin-folder:
So I simply copied the missing file from C:\Qt\6.1.3\msvc2019_64\bin into C:\Qt\6.1.3\mingw81_64\bin
And then it worked!
If you don't have the msvc2019_64 folder you can install it by modifying Qt via app-settings:

Cmake (Clion) cant link QT5 UiTools

I have a cmake file for various dummy Qt5 applications.
cmake_minimum_required(VERSION 3.15)
project(Qt_demo)
set(CMAKE_CXX_STANDARD 17)
# Tell cmake where Qt is located
set(Qt5_DIR "~/Code/Cpp/Qt/5.12.6/gcc_64/lib/cmake/Qt5")
#enable mocking compiler
set(CMAKE_AUTOMOC ON)
#enable ui compiler
set(CMAKE_AUTOUIC ON)
#enable resources
set(CMAKE_AUTORCC ON)
# Tell cmake to find the modules Qt5Core and Qt5widgets
find_package(Qt5 COMPONENTS Core Widgets UiTools REQUIRED)
get_target_property(QtUiTools_location Qt5::UiTools LOCATION)
message("${QtUiTools_location}")
add_executable(screenshot_demo screenshot/screenshot_main.cpp screenshot/screenshot.cpp)
# Link the library to the executable
target_link_libraries(screenshot_demo Qt5::Core Qt5::Widgets)
qt5_add_resources(RC_SRC "application/application.qrc")
add_executable(application_demo application/main.cpp application/mainwindow.cpp ${RC_SRC})
qt5_use_modules(application_demo Core Widgets)
qt5_add_resources(CALC_RC_SRC "calculator_builder/calculatorbuilder.qrc")
add_executable(calculator_builder_demo calculator_builder/main.cpp calculator_builder/calculatorbuilder.cpp ${CALC_RC_SRC})
target_link_libraries(calculator_builder_demo Qt5::UiTools Qt5::Core Qt5::Widgets )
Furthermore I added the Qt5 desinger as an external tool in clion as per setup
The first two projects compile and run just fine. However in the last project (calculator builder) I am trying to use UITools which is a Qt5 Addon. When I hit the compile button I get the following error:
[ 28%] Linking CXX executable calculator_builder_demo
/home/marc/Code/Cpp/Qt/5.12.6/gcc_64/lib/libQt5UiTools.a(properties.o): In function `QFormInternal::variantToDomProperty(QFormInternal::QAbstractFormBuilder*, QMetaObject const*, QString const&, QVariant const&)':
/home/qt/work/qt/qttools/src/designer/src/uitools/../lib/uilib/properties.cpp:594: undefined reference to `QString::arg(QLatin1String, int, QChar) const'
/home/marc/Code/Cpp/Qt/5.12.6/gcc_64/lib/libQt5UiTools.a(formbuilderextra.o): In function `QFormInternal::QFormBuilderExtra::readUi(QIODevice*)':
/home/qt/work/qt/qttools/src/designer/src/uitools/../lib/uilib/formbuilderextra.cpp:145: undefined reference to `QVersionNumber::fromString(QStringView, int*)'
/home/qt/work/qt/qttools/src/designer/src/uitools/../lib/uilib/formbuilderextra.cpp:150: undefined reference to `QString::arg(QStringView, int, QChar) const'
collect2: error: ld returned 1 exit status
CMakeFiles/calculator_builder_demo.dir/build.make:137: recipe for target 'calculator_builder_demo' failed
make[3]: *** [calculator_builder_demo] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/calculator_builder_demo.dir/all' failed
make[2]: *** [CMakeFiles/calculator_builder_demo.dir/all] Error 2
CMakeFiles/Makefile2:89: recipe for target 'CMakeFiles/calculator_builder_demo.dir/rule' failed
make[1]: *** [CMakeFiles/calculator_builder_demo.dir/rule] Error 2
Makefile:118: recipe for target 'calculator_builder_demo' failed
make: *** [calculator_builder_demo] Error 2
I am totally lost. Any help would be greatly appreciated

C/C++ undefined reference for sqlite3's functions using CLion with CMAKE

CMAKE's file has this code:
cmake_minimum_required(VERSION 3.6)
project(HelloSqliteC)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.c)
add_executable(HelloSqliteC ${SOURCE_FILES})
The file main.c has this code:
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
int main() {
sqlite3 *db;
int rc;
rc = sqlite3_open("database.db", &db);
if (rc) {
fprintf(stderr, "Can't open database: %s!\n", sqlite3_errmsg(db));
} else {
fprintf(stderr, "Opened database successfully!\n");
}
sqlite3_close(db);
return 0;
}
When I've trying compiling:
/home/marcus/ide/clion/clion-2016.3.1/bin/cmake/bin/cmake --build /home/marcus/projects/native/HelloSqliteC/cmake-build-debug --target HelloSqliteC -- -j 4
[ 50%] Building C object CMakeFiles/HelloSqliteC.dir/main.c.o
[100%] Linking C executable HelloSqliteC
CMakeFiles/HelloSqliteC.dir/main.c.o: In function `main':
/home/marcus/projects/native/HelloSqliteC/main.c:13: undefined reference to `sqlite3_open'
/home/marcus/projects/native/HelloSqliteC/main.c:16: undefined reference to `sqlite3_errmsg'
/home/marcus/projects/native/HelloSqliteC/main.c:21: undefined reference to `sqlite3_close'
collect2: error: ld returned 1 exit status
CMakeFiles/HelloSqliteC.dir/build.make:94: recipe for target 'HelloSqliteC' failed
make[3]: *** [HelloSqliteC] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/HelloSqliteC.dir/all' failed
make[2]: *** [CMakeFiles/HelloSqliteC.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/HelloSqliteC.dir/rule' failed
make[1]: *** [CMakeFiles/HelloSqliteC.dir/rule] Error 2
Makefile:118: recipe for target 'HelloSqliteC' failed
make: *** [HelloSqliteC] Error 2
I tried solve this issue using different ways, but no success.
I'm using CLion C/C++, my OS is Ubuntu 16.04 and I install sqlite3 using autoconf.
For test, I used the main.c above and compiled in command line with "-l sqlite3" using GCC and I had success, but I want use CLion.
Help me, thanks.
I also use CLion C/C++. The OS is Ubuntu 18.04.
But I want to compile the main.cpp.
The CMakelist.txt is:
cmake_minimum_required(VERSION 3.13)
project(ProjectName)
set(CMAKE_CXX_STANDARD 14)
add_executable(student main.cpp)
target_link_libraries(ProjectName LINK_PUBLIC sqlite3)
It works successfully.
I solved this issue with this target_link_libraries(HelloSqlite3 LINK_PUBLIC sqlite3) or target_link_libraries(projectName LINK_PUBLIC libraryName).
CLion uses CMake for all the building and project configuration. You have to manually modify CMakeLists.txt. In fact this is a CMake question.
This line in your CMakeLists.txt will solve your problem:
add_compile_options(-l sqlite3)
But actually CMake has a more sophisticated dependency discovery system. Read How To Find Libraries to learn this.

qt add path for 3rd party header and libraries

I am using qt and developing a desktop app that will run under win xp/vista.
I have a 3rd party library UserAgentLib (static, and shared). But I am not sure how to link in qt creator.
I have opened the *.pro file and added my library and header path.
The library is called UserAgentLib and the header file is called UserAgentLib.h
TARGET = Dialer
TEMPLATE = app
LIBS += D:\Projects\qtDialer\tools\lib\UserAgentLib
INCLUDEPATH += D:\Projects\qtDialer\tools\inc
SOURCES += main.cpp\
catdialer.cpp
HEADERS += catdialer.h
FORMS += catdialer.ui
I think it does find the header file, as I get about 100 errors for declarations in the UserAgentLib.h file. However, I don't think it is linking with the library.
Many thanks for any suggestions,
======================
I have create a very simple library in VS C++ 2008. Here is the code for the header and source file.
Header:
// mathslibrary.hpp
int add_numbers(const int a, const int b);
Source:
// mathslibrary.cpp
#include "mathslibrary.hpp"
int add_numbers(const int a, const int b)
{
return a + b;
}
I have compiled this into a library. And tested by linking with a WIN32 console application in VS 2008. The library worked as expected.
Now when I try and link with qt.
#include <QtCore/QCoreApplication>
#include <iostream>
#include "mathslibrary.hpp"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
std::cout << "add numbers 40 + 60 = " << add_numbers(40, 60) << std::endl;
return a.exec();
}
This is my qmake file:
QT -= gui
TARGET = testlibrary
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
LIBS = D:\Projects\TestLibrary\mathsLibrary\Debug\mathsLibrary.lib
INCLUDEPATH = D:\Projects\TestLibrary\mathsLibrary\
SOURCES += main.cpp
These are the errors I get when I try and build:
c:/Qt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c::-1: error: undefined reference to `WinMain#16'
:-1: error: collect2: ld returned 1 exit status
And these are the compile issues:
Running build steps for project testlibrary...
Creating gdb macros library...
Configuration unchanged, skipping QMake step.
Starting: C:/Qt/mingw/bin/mingw32-make.exe debug -w
mingw32-make: Entering directory `D:/Projects/TestQTLibrary/testlibrary'
C:/Qt/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory `D:/Projects/TestQTLibrary/testlibrary'
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-subsystem,console -mthreads -Wl -o debug\testlibrary.exe -L"c:\Qt\qt\lib"
D:\Projects\TestLibrary\mathsLibrary\Debug\mathsLibrary.lib -lQtCored4
mingw32-make[1]: Leaving directory `D:/Projects/TestQTLibrary/testlibrary'
mingw32-make: Leaving directory `D:/Projects/TestQTLibrary/testlibrary'
c:/Qt/mingw/bin/../lib/gcc/mingw32/3.4.5/../../../libmingw32.a(main.o):main.c:
(.text+0x104): undefined reference to `WinMain#16'
collect2: ld returned 1 exit status
mingw32-make[1]: * [debug\testlibrary.exe] Error 1
mingw32-make: * [debug] Error 2
Exited with code 2.
Error while building project testlibrary
When executing build step 'Make'
Many thanks for any advice,
Don't know if this changes anything, but maybe you have to define it like this:
LIBS += -LD:/Projects/qtDialer/tools/lib -lUserAgentLib
If you are getting compiler errors then your UserAgentLib.h probably didn't get included. You can test it with:
!exists( UserAgentLib.h ) {
error( "No UserAgentLib.h file found" )
}
You put the above in one of the .pro file and not the constructor.See this.
If the library didn't get linked (which is after your application has compiled well) -- then you need to tinker with your LIBS += ... line, though which appears fine on first glance.
Try this with the simple library first and then try it with the library you are actually trying to get working.
LIBS += D:\Projects\qtDialer\tools\lib\mathsLibrary.lib
In your .hpp file, add extern "C" before your function declarations:
// mathslibrary.hpp
extern "C" int add_numbers(const int a, const int b);
Rebuild the library from Visual Studio.
Now you should be able to compile your test app with Qt Creater. Then copy the corresponding dll into the directory with your new executable and give it a run.
As far as i understood, you generated a dll using MSVC and now you are trying to link it in Qt using mingw. right?
Object files and static libraries created with different compilers, or
even with significantly different releases of the same compiler, often
cannot be linked together. This issue is not specific to MinGW: many
other compilers are mutually incompatible. Build everything from
source with the same version of the same compiler if you can
chech this out : http://chadaustin.me/cppinterface.html

Resources