Mixing C and C++ in a Qt project. undefined reference to function()
-
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; }
-
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.
-
Turns out my CMakeLists.txt was missing the C language parameter in the project.
project(MyProject C CXX)
-
@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 significantlyCore.obj
(orCore.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. -
@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...
-
@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.... -
@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 thatadd_executable(...)
statement? -
@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.
-
@Christian-Ehrlicher
LOL :) Well I don't know then why it does not include linking withCore.obj
, or why the OP says puttingproject(MyProject C CXX)
into " my CMakeLists.txt" makes it work...? :) -
@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 .