在Qt项目中混合使用C和c++.对function()的未定义引用



我正在尝试创建一个Qt项目,将使用C代码的逻辑相关的东西和c++的UI。

我在CMakeLists.txt中添加了Core.c和Core.h到add_executable(),并使用extern将Core.h包含在MainWindow.cpp中。:

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

我在MainWindow.cpp的构造函数中调用say_hello()函数,但无法使其编译。

下面是编译输出:

09:24:03: Running steps for project Betacraft...
09:24:03: Starting: "C:QtToolsCMake_64bincmake.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:QtToolsCMake_64bincmake.exe" exited normally.
09:24:03: Starting: "C:QtToolsCMake_64bincmake.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:QtToolsmingw1120_64bing++.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:QtToolsCMake_64bincmake.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.

我的项目文件结构:

│   .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("%sn", "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;
}

谢谢你的帮助

您的CmakeLists.txt文件缺失

项目中的C语言参数

project(MyProject C CXX)

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

也请看看混合c和c++

相关内容

  • 没有找到相关文章

最新更新