3

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!

Community
  • 1
  • 1
user3839833
  • 111
  • 1
  • 2
  • 9
  • Did you re-run qmake in Qt Creator? (I assume that's what you're using) – owacoder Jan 24 '16 at 17:57
  • Yes, that's what I'm using. I just did so but nothing changed unfortunately. – user3839833 Jan 24 '16 at 18:00
  • Try rebuilding the project after you run qmake. It should build properly if the code is fine. – owacoder Jan 24 '16 at 18:01
  • Well I guess there is something wrong then. Is the .pro alright? Nothing I might have forgotten? Clicking Strg+Left Mouse Button also transports me to the line of the method in the file.h for the c file. – user3839833 Jan 24 '16 at 18:09
  • You should make sure there are definitions for `foo()` and `bar()` in your `.c` file. You could also try adding the C header file to your `.pro` file, and see what happens. – owacoder Jan 24 '16 at 18:17
  • The definitions are there and I just tried to include the c file from a dummy c file with the header and it worked. I can also include the c file itself in the c++ file but that gives me some multiple definition errors (no wonder, I guess it just gets compiled 2 times). So where could I look to solve my problem :(? – user3839833 Jan 24 '16 at 19:26

3 Answers3

1

A bit late, but I got the same problem with Qt 6.x and a Qt Widgets Application with a CMake project file.

I followed the rules, but it did't work until I manually added C into the project tag of the CMakeLists.txt file:

project(CExternDemo VERSION 0.1 LANGUAGES CXX C)

Hope this helps someone...

Johann Horvat
  • 1,285
  • 1
  • 14
  • 18
0

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

JamesWebbTelescopeAlien
  • 3,547
  • 2
  • 30
  • 51
0

you need to expressly run qmake and then rebuild

obelix
  • 1