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

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;
}

Thank you kindly for any help

>Solution :

Your CmakeLists.txt file is missing

C language parameter in the project

project(MyProject C CXX)

https://cmake.org/cmake/help/latest/command/project.html

Also please take a look at mixing c and c++

Leave a Reply