Solved How to use QZXing with cmake Qt 6.4
-
Hi,
I'm trying to use QZXing's encode and decode functions in my project. How does this work with cmake with Qt 6.4.0?
The latest thing I've tried is:
-
Adding the qzxing directory to my project then adding the following to my base project's CMakeLists.txt
SET(QZXING_USE_ENCODER ON) SET(ENABLE_ENCODER_GENERIC ON) SET(ENABLE_ENCODER_QR_CODE ON) add_subdirectory(./qzxing/src)
Then, in one of my project files I
#include "./qzxing/src/QZXing.h"
and try to use QZXing's example in their repo:QString data = "text to be encoded"; QImage barcode = QZXing::encodeData(data, QZXing::EncoderFormat_QR_CODE, QSize(width.toInt(), height.toInt()), QZXing::EncodeErrorCorrectionLevel_H);
I don't get any errors after adding the above, but when I try to build, I get the following error:
:-1: error: error: undefined symbol: QZXing::encodeData(QString const&,QZXing::EncoderFormat, QSize,QZXing::EncodeErrorCorrectionLevel, bool, bool)
I'm assuming that the above error is because the
.cpp
files aren't included with the project, so no function definitions. I haven't been able to find the correct cmake commands to do this, though (if that is the issue here).
-
-
@gween You could build qzxing as a library and then link this library to your project using target_link_libraries.
-
@gween You could build qzxing as a library and then link this library to your project using target_link_libraries.
-
I appreciate the response @jsulm.
Does building as a library in Qt mean opening the
QZXing.pro
file in Qt 6.4 and clicking build? When I do so, I get the following error::-1: error: No rule to make target 'install'. Stop.
What file is this install target supposed to be in?
This is the.pro
file:CONFIG += \ enable_decoder_1d_barcodes \ enable_decoder_qr_code \ enable_decoder_data_matrix \ enable_decoder_aztec \ enable_decoder_pdf17 \ enable_encoder_qr_code \ #staticlib \ #qzxing_qml \ #qzxing_multimedia \ VERSION = 3.3 TARGET = QZXing TEMPLATE = lib include(QZXing-components.pri) DEFINES -= DISABLE_LIBRARY_FEATURES
The only thing QZXing mentions about building as a library is https://github.com/ftylitak/qzxing#compile-the-project-as-an-external-library.
-
@gween said in How to use QZXing with cmake Qt 6.4:
TEMPLATE = lib
It is already built as library.
To make it properly with QMake you should create a https://wiki.qt.io/SUBDIRS_-_handling_dependencies project. You then add qzxing as one subproject and your actualy project as another one. Then in your project you add qzxing as dependency (see "Defining Project Dependencies: .depends attributes" chapter in the link above). -
@jsulm the subdirs link you sent makes sense for QMake, but does this work with CMake since I'm using Qt 6.4? My current project doesn't have a
.pro
file, only aCMakeLists.txt
file. Does thesubdirs
"pattern" apply to projects that only haveCMakeLists.txt
? I.e. is there aDefining Project Dependencies: .depends attributes
type documentation for Qt 6.4 with cmake?The QZXing repo does have a
CMakeLists.txt
file, https://github.com/ftylitak/qzxing/blob/master/src/CMakeLists.txt, in itssrc
dir. Which is why I thought I'd just be able to add the repo as a subdirectory to my main project, then use some cmake function, such asadd_subdirectory()
, to reference the.h
and.cpp
files. -
@jsulm's answer is correct:
You could build qzxing as a library and then link this library to your project using target_link_libraries.
This was my complete misunderstanding of cmake and how it works.
# with qzxing/src copied to my project as QZXing add_subdirectory(./QZXing/) target_link_libraries(${PROJECT_NAME} qzxing)
My misunderstanding was from two things:
- I thought the command
add_subdirectory()
was the only thing that should have been needed - That I had to use the library name that is specified in the
QZXing/CMakeLists.txt
, which, from this commandadd_library(qzxing "" ${SOURCES})
isqzxing
.
- I thought the command
-
@gween How do you use QZing? His CMake use QT5