Compile and link C++ and C programms together with QT-Creator

冷暖自知 提交于 2019-12-13 02:15:15

问题


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

I then tried to include the header file in the C++ file with this:

 extern "C" {
 #include "folder/file.h"
 }

I also alternatively tried a basic #include "folder/file.h"

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!


回答1:


You may be missing one more #endif at the end of your header file




回答2:


you need to expressly run qmake and then rebuild



来源:https://stackoverflow.com/questions/34979280/compile-and-link-c-and-c-programms-together-with-qt-creator

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!