0

I'm new to Qt6. In Qt6.4.3, I'm writing a simple console app in c++ to load a .jpg file into a QImage object. On my Ubuntu 6.22.04 machine, I'm getting the following error: "/main.cpp:40: undefined reference to `QImage::load(QString const&, char const*)'" I have the following in my includes:

    #include <QtGui/QImage>

I declare the following as 3 consecutive lines:

    QString filename="<fullpath>.JPG";
    QImage img;
    img.load(filename, "JPG");

I'm using cmake, but don't know whether that's relevant. Using Qt Creator 9.0.2. This approach used to work well in Qt5. I know someone's gonna call me an idiot or a lazy idiot or tell me to RTFM, so go ahead. Get it out of your system, but then please give me some useful advice.

Tried the statements above, attempted a build, and got the error shown above. I was expecting a QImage object returned stuffed with the .jpeg image I requested. My fullpath looks similar to "/mnt//Project/testme/blahblah.JPG". The path is correct with correct case and slashes. As I said this worked in Qt5.15. Figure I'm missing something stupid, but after spending all day searching the web, I haven't found the answer.

REK
  • 1
  • "undefined reference to" is a linking error not a compiling error. The problem is going to be something with how your CMakeLists file is doing the linking. – jwezorek Mar 24 '23 at 22:00
  • Show the cmakelists.txt – eyllanesc Mar 24 '23 at 22:38
  • Thank you, jwezorek! I thought cmake might be the issue. Coming from 5.15 I've always been a qmake boy. Your suggestion was golden, and I really appreciate it! Here are the MakeLists edits that did it for me: find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core Gui) find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Core Gui) add_executable(untitled37 main.cpp ) target_link_libraries(untitled37 Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Gui) – REK Mar 25 '23 at 00:36
  • Please create a [MCVE], which I think in this case should include CMakeLists.txt (unless you are using default created by Qt Creator new project wizard), and main.cpp. – hyde Mar 25 '23 at 11:07

1 Answers1

2

The problem is most likely in your CMakeLists.txt. You are missing the Qt Gui module, which is needed for QImage.

Below is working code, tested on Windows and Qt 6.4.


CMakeLists.txt:

cmake_minimum_required(VERSION 3.16)

project(untitled VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.2 COMPONENTS Gui REQUIRED)

qt_add_executable(appuntitled
    main.cpp
)

target_link_libraries(appuntitled
    PRIVATE Qt6::Gui)

main.cpp

#include <QtGui/QImage>
#include <QDebug>
int main(int argc, char *argv[])
{
    QImage img;
    qDebug() << img.size();
    img.load("C:/Qt/Tools/QtCreator/share/qtcreator/qbs/share/qbs/examples/compiled-qml/cheese.jpg");
    qDebug() << img.size();

    return img.isNull() ? EXIT_FAILURE : EXIT_SUCCESS ;
}

Application Output shown by Qt Creator:

13:32:10: Starting C:\Users\hyde\Qt\build-untitled-Desktop_Qt_6_4_2_MinGW_64_bit-Debug\appuntitled.exe...
QSize(0, 0)
QSize(94, 94)
13:32:10: C:\Users\hyde\Qt\build-untitled-Desktop_Qt_6_4_2_MinGW_64_bit-Debug\appuntitled.exe exited with code 0
hyde
  • 60,639
  • 21
  • 115
  • 176