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!

6
  • Did you re-run qmake in Qt Creator? (I assume that's what you're using) Commented Jan 24, 2016 at 17:57
  • Yes, that's what I'm using. I just did so but nothing changed unfortunately. Commented Jan 24, 2016 at 18:00
  • Try rebuilding the project after you run qmake. It should build properly if the code is fine. Commented Jan 24, 2016 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. Commented Jan 24, 2016 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. Commented Jan 24, 2016 at 18:17

3 Answers 3

2

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...

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

you need to expressly run qmake and then rebuild

1 Comment

Add some more details

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.