使用 QT-Creator 编译和链接 C++ 和 C 程序 [英] Compile and link C++ and C programms together with QT-Creator
问题描述
所以我有几个 C++ 文件,基本上需要在我的程序的 C++ 部分完成后调用一个 C 函数.因此,如果有一种方法可以以某种方式神奇地启动 C 文件"(我希望你知道我的意思),我也会很高兴听到这个消息.现在到我的实际问题:我试图在主 C++ 文件中的主函数末尾调用 C 函数.我已经看到了所有这些从 C++ 代码调用 C 函数 并试图以正确的方式做每件事,我的 C 文件头文件看起来像这样:
so I have serveal C++ files and basically need to call a C function after the C++ part of my program is done. So if there is a way to just somehow magically "start the C file" (I hope you know what I mean) I would be glad to hear about that too. Now to my actual problem: I'm trying to call the C function at the end of my main function in the main C++ file. I already saw all of this Call a C function from C++ code and tried to do everything the correct way, my header file for the C file looks kind of like this:
#ifndef H_FILE
#define H_FILE
#ifdef __cplusplus
extern "C" {
#endif
void foo();
void bar();
#ifdef __cplusplus
}
#endif
然后我尝试将头文件包含在 C++ 文件中:
I then tried to include the header file in the C++ file with this:
extern "C" {
#include "folder/file.h"
}
我也尝试了一个基本的#include "folder/file.h"
I also alternatively tried a basic #include "folder/file.h"
但是,当我尝试使用 C 文件中的 foo() 函数时,出现未定义的引用错误.我的 .pro 文件看起来像这样:
But I'm getting a undefined reference error when I'm trying to use the foo() function from my C file. My .pro file looks kind of like this:
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Example
TEMPLATE = app
QMAKE_CXXFLAGS += -std=c++11
SOURCES += main.cpp\
folder/file.c \
HEADERS += main.h \
所以我想我必须添加某种标志才能正确编译它,但我到底错过了什么?感谢您的帮助!
So I guess I have to add some kind of flag for it to be compiled correctly, but what exactly am I missing? Thank you for your help!
推荐答案
需要明确运行qmake然后rebuild
you need to expressly run qmake and then rebuild
这篇关于使用 QT-Creator 编译和链接 C++ 和 C 程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!