Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Mixing C and C++ in a Qt project. undefined reference to function()
Forum Updated to NodeBB v4.3 + New Features

Mixing C and C++ in a Qt project. undefined reference to function()

Scheduled Pinned Locked Moved Solved Qt 6
10 Posts 3 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KazuOfficial
    wrote on 14 Jun 2022, 08:37 last edited by
    #1

    New question because the previous one was poorly constructed.

    I'm trying to create a Qt project that will use C code for logic-related stuff and C++ for UI.

    I've added Core.c and Core.h to add_executable() in CMakeLists.txt and included Core.h in MainWindow.cpp using extern:

    extern "C" {
        #include "Core/Core.h"
    }
    

    I'm calling the say_hello() function in the constructor in MainWindow.cpp but can't get it to compile.

    Here's the compile output:

    09:24:03: Running steps for project Betacraft...
    09:24:03: Starting: "C:\Qt\Tools\CMake_64\bin\cmake.exe" --build C:/Programming/build-BetaCraft-Launcher-Java-Desktop_Qt_6_2_4_MinGW_64_bit-Debug --target clean
    [1/2 14.6/sec] Cleaning additional files...
    [2/2 25.2/sec] Cleaning all built files...
    Cleaning... 5 files.
    09:24:03: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited normally.
    09:24:03: Starting: "C:\Qt\Tools\CMake_64\bin\cmake.exe" --build C:/Programming/build-BetaCraft-Launcher-Java-Desktop_Qt_6_2_4_MinGW_64_bit-Debug --target all
    [1/7 0.8/sec] Automatic MOC and UIC for target Betacraft
    [2/7 1.5/sec] Automatic RCC for assets.qrc
    [3/7 1.7/sec] Building CXX object CMakeFiles/Betacraft.dir/Betacraft_autogen/EWIEGA46WW/qrc_assets.cpp.obj
    [4/7 0.8/sec] Building CXX object CMakeFiles/Betacraft.dir/main.cpp.obj
    [5/7 0.9/sec] Building CXX object CMakeFiles/Betacraft.dir/Betacraft_autogen/mocs_compilation.cpp.obj
    [6/7 0.5/sec] Building CXX object CMakeFiles/Betacraft.dir/MainWindow.cpp.obj
    [7/7 0.6/sec] Linking CXX executable Betacraft.exe
    FAILED: Betacraft.exe 
    cmd.exe /C "cd . && C:\Qt\Tools\mingw1120_64\bin\g++.exe -g  CMakeFiles/Betacraft.dir/Betacraft_autogen/mocs_compilation.cpp.obj CMakeFiles/Betacraft.dir/MainWindow.cpp.obj CMakeFiles/Betacraft.dir/main.cpp.obj CMakeFiles/Betacraft.dir/Betacraft_autogen/EWIEGA46WW/qrc_assets.cpp.obj -o Betacraft.exe -Wl,--out-implib,libBetacraft.dll.a -Wl,--major-image-version,0,--minor-image-version,0  C:/Qt/6.2.4/mingw_64/lib/libQt6Widgets.a  C:/Qt/6.2.4/mingw_64/lib/libQt6Network.a  C:/Qt/6.2.4/mingw_64/lib/libQt6Gui.a  -ld3d11  -ldxgi  -ldxguid  C:/Qt/6.2.4/mingw_64/lib/libQt6Core.a  -lmpr  -luserenv  -lws2_32  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
    C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/Betacraft.dir/MainWindow.cpp.obj: in function `MainWindow::MainWindow(QWidget*)':
    C:/Programming/betacraft-qt/MainWindow.cpp:22: undefined reference to `say_hello'
    collect2.exe: error: ld returned 1 exit status
    ninja: build stopped: subcommand failed.
    09:24:16: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited with code 1.
    Error while building/deploying project Betacraft (kit: Desktop Qt 6.2.4 MinGW 64-bit)
    When executing step "Build"
    09:24:16: Elapsed time: 00:13.
    

    My project's file structure:

    │   .gitignore
    │   assets.qrc
    │   CMakeLists.txt
    │   CMakeLists.txt.user
    │   LICENSE
    │   main.cpp
    │   MainWindow.cpp
    │   mainwindow.h
    │   README.md
    │
    ├───assets
    │       dirt.png
    │       favicon.png
    │       logo.png
    │       stone.png
    │
    └───Core
            Core.c
            Core.h
    

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.16)
    
    project(Betacraft VERSION 1.0.0 LANGUAGES CXX)
    
    set(CMAKE_CXX_STANDARD 17)
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    set(CMAKE_AUTOUIC ON)
    
    find_package(Qt6 REQUIRED COMPONENTS Widgets Network)
    
    add_executable(Betacraft
        MainWindow.cpp MainWindow.h
        main.cpp
        assets.qrc
        Core/Core.c Core/Core.h
    )
    
    target_link_libraries(Betacraft PRIVATE
        Qt6::Widgets
        Qt6::Network
    )
    

    Core.c

    #include <stdio.h>
    #include "Core.h"
    
    void say_hello()
    {
        printf("%s\n", "hello");
    }
    

    Core.h

    #ifndef CORE_H
    #define CORE_H
    
    void say_hello();
    
    #endif
    

    MainWindow.cpp

    #include <QtWidgets>
    #include "MainWindow.h"
    
    extern "C" {
        #include "Core/Core.h"
    }
    
    // Constructor for main window
    MainWindow::MainWindow(QWidget *parent) :
        QWidget(parent)
    {
        _changelog = new QTextEdit;
        _changelog->setReadOnly(true);
        _changelog->setStyleSheet(QString("QTextEdit { background-image: url(':/Assets/stone.png'); border: 0; color: white; font-size: 15px; padding-left: 10px; }"));
    
        say_hello();
    
        //Set mainLayout properties
        QGridLayout *mainLayout = new QGridLayout;
        mainLayout->setSpacing(0);
        mainLayout->setContentsMargins(0, 0, 0, 0);
    
        // Add widgets
        mainLayout->addWidget(_changelog);
    
        setLayout(mainLayout);
    
        //Set window properties
        setWindowTitle("Betacraft");
        resize(800, 480);
        setMinimumSize(800, 480);
    }
    
    //Destructor
    MainWindow::~MainWindow()
    {
        delete _changelog;
    }
    
    J 1 Reply Last reply 14 Jun 2022, 08:45
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 14 Jun 2022, 08:39 last edited by
      #2

      As you can still see in your compile output - Core.c is not compiled and linked so the function can not be found... nothing changed since your last post in the other thread.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • K Offline
        K Offline
        KazuOfficial
        wrote on 14 Jun 2022, 08:44 last edited by
        #3

        Turns out my CMakeLists.txt was missing the C language parameter in the project.

        project(MyProject C CXX)
        
        C J 2 Replies Last reply 14 Jun 2022, 08:45
        0
        • K KazuOfficial
          14 Jun 2022, 08:37

          New question because the previous one was poorly constructed.

          I'm trying to create a Qt project that will use C code for logic-related stuff and C++ for UI.

          I've added Core.c and Core.h to add_executable() in CMakeLists.txt and included Core.h in MainWindow.cpp using extern:

          extern "C" {
              #include "Core/Core.h"
          }
          

          I'm calling the say_hello() function in the constructor in MainWindow.cpp but can't get it to compile.

          Here's the compile output:

          09:24:03: Running steps for project Betacraft...
          09:24:03: Starting: "C:\Qt\Tools\CMake_64\bin\cmake.exe" --build C:/Programming/build-BetaCraft-Launcher-Java-Desktop_Qt_6_2_4_MinGW_64_bit-Debug --target clean
          [1/2 14.6/sec] Cleaning additional files...
          [2/2 25.2/sec] Cleaning all built files...
          Cleaning... 5 files.
          09:24:03: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited normally.
          09:24:03: Starting: "C:\Qt\Tools\CMake_64\bin\cmake.exe" --build C:/Programming/build-BetaCraft-Launcher-Java-Desktop_Qt_6_2_4_MinGW_64_bit-Debug --target all
          [1/7 0.8/sec] Automatic MOC and UIC for target Betacraft
          [2/7 1.5/sec] Automatic RCC for assets.qrc
          [3/7 1.7/sec] Building CXX object CMakeFiles/Betacraft.dir/Betacraft_autogen/EWIEGA46WW/qrc_assets.cpp.obj
          [4/7 0.8/sec] Building CXX object CMakeFiles/Betacraft.dir/main.cpp.obj
          [5/7 0.9/sec] Building CXX object CMakeFiles/Betacraft.dir/Betacraft_autogen/mocs_compilation.cpp.obj
          [6/7 0.5/sec] Building CXX object CMakeFiles/Betacraft.dir/MainWindow.cpp.obj
          [7/7 0.6/sec] Linking CXX executable Betacraft.exe
          FAILED: Betacraft.exe 
          cmd.exe /C "cd . && C:\Qt\Tools\mingw1120_64\bin\g++.exe -g  CMakeFiles/Betacraft.dir/Betacraft_autogen/mocs_compilation.cpp.obj CMakeFiles/Betacraft.dir/MainWindow.cpp.obj CMakeFiles/Betacraft.dir/main.cpp.obj CMakeFiles/Betacraft.dir/Betacraft_autogen/EWIEGA46WW/qrc_assets.cpp.obj -o Betacraft.exe -Wl,--out-implib,libBetacraft.dll.a -Wl,--major-image-version,0,--minor-image-version,0  C:/Qt/6.2.4/mingw_64/lib/libQt6Widgets.a  C:/Qt/6.2.4/mingw_64/lib/libQt6Network.a  C:/Qt/6.2.4/mingw_64/lib/libQt6Gui.a  -ld3d11  -ldxgi  -ldxguid  C:/Qt/6.2.4/mingw_64/lib/libQt6Core.a  -lmpr  -luserenv  -lws2_32  -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
          C:/Qt/Tools/mingw1120_64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/Betacraft.dir/MainWindow.cpp.obj: in function `MainWindow::MainWindow(QWidget*)':
          C:/Programming/betacraft-qt/MainWindow.cpp:22: undefined reference to `say_hello'
          collect2.exe: error: ld returned 1 exit status
          ninja: build stopped: subcommand failed.
          09:24:16: The process "C:\Qt\Tools\CMake_64\bin\cmake.exe" exited with code 1.
          Error while building/deploying project Betacraft (kit: Desktop Qt 6.2.4 MinGW 64-bit)
          When executing step "Build"
          09:24:16: Elapsed time: 00:13.
          

          My project's file structure:

          │   .gitignore
          │   assets.qrc
          │   CMakeLists.txt
          │   CMakeLists.txt.user
          │   LICENSE
          │   main.cpp
          │   MainWindow.cpp
          │   mainwindow.h
          │   README.md
          │
          ├───assets
          │       dirt.png
          │       favicon.png
          │       logo.png
          │       stone.png
          │
          └───Core
                  Core.c
                  Core.h
          

          CMakeLists.txt

          cmake_minimum_required(VERSION 3.16)
          
          project(Betacraft VERSION 1.0.0 LANGUAGES CXX)
          
          set(CMAKE_CXX_STANDARD 17)
          set(CMAKE_CXX_STANDARD_REQUIRED ON)
          
          set(CMAKE_AUTOMOC ON)
          set(CMAKE_AUTORCC ON)
          set(CMAKE_AUTOUIC ON)
          
          find_package(Qt6 REQUIRED COMPONENTS Widgets Network)
          
          add_executable(Betacraft
              MainWindow.cpp MainWindow.h
              main.cpp
              assets.qrc
              Core/Core.c Core/Core.h
          )
          
          target_link_libraries(Betacraft PRIVATE
              Qt6::Widgets
              Qt6::Network
          )
          

          Core.c

          #include <stdio.h>
          #include "Core.h"
          
          void say_hello()
          {
              printf("%s\n", "hello");
          }
          

          Core.h

          #ifndef CORE_H
          #define CORE_H
          
          void say_hello();
          
          #endif
          

          MainWindow.cpp

          #include <QtWidgets>
          #include "MainWindow.h"
          
          extern "C" {
              #include "Core/Core.h"
          }
          
          // Constructor for main window
          MainWindow::MainWindow(QWidget *parent) :
              QWidget(parent)
          {
              _changelog = new QTextEdit;
              _changelog->setReadOnly(true);
              _changelog->setStyleSheet(QString("QTextEdit { background-image: url(':/Assets/stone.png'); border: 0; color: white; font-size: 15px; padding-left: 10px; }"));
          
              say_hello();
          
              //Set mainLayout properties
              QGridLayout *mainLayout = new QGridLayout;
              mainLayout->setSpacing(0);
              mainLayout->setContentsMargins(0, 0, 0, 0);
          
              // Add widgets
              mainLayout->addWidget(_changelog);
          
              setLayout(mainLayout);
          
              //Set window properties
              setWindowTitle("Betacraft");
              resize(800, 480);
              setMinimumSize(800, 480);
          }
          
          //Destructor
          MainWindow::~MainWindow()
          {
              delete _changelog;
          }
          
          J Offline
          J Offline
          JonB
          wrote on 14 Jun 2022, 08:45 last edited by
          #4

          @KazuOfficial said in Mixing C and C++ in a Qt project. undefined reference to function():

          I've added Core.c and Core.h to add_executable() in CMakeLists.txt

          As @Christian-Ehrlicher says. I'm afarid I know nothing about Qt6/cmake, but whatever

          add_executable(Betacraft
              MainWindow.cpp MainWindow.h
              main.cpp
              assets.qrc
              Core/Core.c Core/Core.h
          )
          

          does it is not causing Core.c to be compiled and/or most significantly Core.obj (or Core.cpp.obj) to be included in the linking stage for your executable. Maybe because it is a .c file and not a .cpp file?? I don't know. But whatever it is that is what needs addressing.

          1 Reply Last reply
          0
          • K KazuOfficial
            14 Jun 2022, 08:44

            Turns out my CMakeLists.txt was missing the C language parameter in the project.

            project(MyProject C CXX)
            
            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 14 Jun 2022, 08:45 last edited by
            #5

            @KazuOfficial said in Mixing C and C++ in a Qt project. undefined reference to function():

            Turns out my CMakeLists.txt was missing the C language parameter in the project.

            Why do you specify them at all? No need for it - cmake knows what to do...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            J 1 Reply Last reply 14 Jun 2022, 08:47
            0
            • K KazuOfficial
              14 Jun 2022, 08:44

              Turns out my CMakeLists.txt was missing the C language parameter in the project.

              project(MyProject C CXX)
              
              J Offline
              J Offline
              JonB
              wrote on 14 Jun 2022, 08:45 last edited by
              #6

              @KazuOfficial said in Mixing C and C++ in a Qt project. undefined reference to function():

              Turns out my CMakeLists.txt was missing the C language parameter in the project.
              project(MyProject C CXX)

              Well there you are then: your post crossed with mine, I did wonder if it was because it is a .c file....

              1 Reply Last reply
              0
              • C Christian Ehrlicher
                14 Jun 2022, 08:45

                @KazuOfficial said in Mixing C and C++ in a Qt project. undefined reference to function():

                Turns out my CMakeLists.txt was missing the C language parameter in the project.

                Why do you specify them at all? No need for it - cmake knows what to do...

                J Offline
                J Offline
                JonB
                wrote on 14 Jun 2022, 08:47 last edited by
                #7

                @Christian-Ehrlicher said in Mixing C and C++ in a Qt project. undefined reference to function():

                Why do you specify them at all? No need for it - cmake knows what to do...

                In that case what does the OP need to do beyond specifying the Core/Core.c in that add_executable(...) statement?

                C 1 Reply Last reply 14 Jun 2022, 08:47
                0
                • J JonB
                  14 Jun 2022, 08:47

                  @Christian-Ehrlicher said in Mixing C and C++ in a Qt project. undefined reference to function():

                  Why do you specify them at all? No need for it - cmake knows what to do...

                  In that case what does the OP need to do beyond specifying the Core/Core.c in that add_executable(...) statement?

                  C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 14 Jun 2022, 08:47 last edited by
                  #8

                  @JonB said in Mixing C and C++ in a Qt project. undefined reference to function():

                  In that case what does the OP need to do beyond specifying the Core/Core.c in that add_executable(...) statement?

                  Nothing.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  J 1 Reply Last reply 14 Jun 2022, 08:49
                  0
                  • C Christian Ehrlicher
                    14 Jun 2022, 08:47

                    @JonB said in Mixing C and C++ in a Qt project. undefined reference to function():

                    In that case what does the OP need to do beyond specifying the Core/Core.c in that add_executable(...) statement?

                    Nothing.

                    J Offline
                    J Offline
                    JonB
                    wrote on 14 Jun 2022, 08:49 last edited by
                    #9

                    @Christian-Ehrlicher
                    LOL :) Well I don't know then why it does not include linking with Core.obj, or why the OP says putting project(MyProject C CXX) into " my CMakeLists.txt" makes it work...? :)

                    C 1 Reply Last reply 14 Jun 2022, 08:52
                    0
                    • J JonB
                      14 Jun 2022, 08:49

                      @Christian-Ehrlicher
                      LOL :) Well I don't know then why it does not include linking with Core.obj, or why the OP says putting project(MyProject C CXX) into " my CMakeLists.txt" makes it work...? :)

                      C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 14 Jun 2022, 08:52 last edited by
                      #10

                      @JonB said in Mixing C and C++ in a Qt project. undefined reference to function():

                      project(MyProject C CXX)

                      This is not needed when you don't need a special language

                      project(MyProject)

                      is enough - cmake does know how to compile a .c or .cpp file .

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      1 Reply Last reply
                      1

                      10/10

                      14 Jun 2022, 08:52

                      • Login

                      • Login or register to search.
                      10 out of 10
                      • First post
                        10/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved