0

I have created a Qt Widgets Application project. In mainwindow.cpp, I want to add a C source file (mySource.c) from a library which includes some rules written in C language that is not allowed to compile with C++ compiler. One of the error which is appeared is:

error: invalid conversion from ‘const char*’ to ‘gchar* {aka char*}’ 

When I create a Plain C Application project in Qt, I can simply compile that source. So I need C compiler.

I also used the following code which does not solve the error:

extern "C" {
    #include "mySource.c"
}

When I go to the Tools -> Options -> Kits -> Kits, I see Desktop Qt 5.12.0 GCC 64bit and after clicking on it I can see the C and C++ compiler as follows:

  • C: GCC(C,X86 64bit in /usr/bin)
  • C++: GCC(C++,X86 64bit in /usr/bin)

I have searched a lot and tested different solutions but I can not solve the problem. What is the solution? Is it a specific flag that I should enable in .pro?

  • C code should be compiled with the C compiler. The `header` should be marked with `extern "C"` and the build system should do the right thing from there. – Michael Surette Feb 26 '19 at 21:58
  • You can't include a C file in a C++ file. A single file has to be compiled with a single compiler, so you can't mix C and C++ code in the same compilation unit. Your choices are to include the C file in another C file or to fix the C file so it can be compiled with a C++ compiler. (The `extern "C"` thing is to allow C code to be called from C++ code, not compiled.) – David Schwartz Feb 26 '19 at 22:01
  • @DavidSchwartz thanks, but the answer of these questions and some similar questions is to doing that: [https://stackoverflow.com/questions/34979280/compile-and-link-c-and-c-programms-together-with-qt-creator] [https://www.qtcentre.org/threads/33974-Compiling-c-files-using-C-and-QT] – morteza ali ahmadi Feb 26 '19 at 22:15
  • @MichaelSurette thanks, but the answer of these questions and some similar questions is to doing that: [https://stackoverflow.com/questions/34979280/compile-and-link-c-and-c-programms-together-with-qt-creator] [https://www.qtcentre.org/threads/33974-Compiling-c-files-using-C-and-QT] – morteza ali ahmadi Feb 26 '19 at 22:16
  • 1
    You are misunderstanding the links that you posted. Here is a better link to explain what extern "C" does: https://stackoverflow.com/questions/1041866/what-is-the-effect-of-extern-c-in-c – drescherjm Feb 26 '19 at 22:21
  • @mortezaaliahmadi Those links are about calling C code from C++ code, which is one of your options. The C code still has to be compiled by a C compiler, which means it has to be in a separate compilation unit. All that `extern "C"` does is arrange for the proper linking format to connect C++ code to C code compiled by a C compiler. The C code still has to be compiled with a C compiler or the linking won't work. – David Schwartz Feb 26 '19 at 22:23
  • Fundamentally, you have two choices. You can compile the code with a C compiler or you can compile it with a C++ compiler. If you're going to compile it with a C compiler, it has to be in a separate compilation unit from C++ code because a C compiler can't compile C++ code. If you're going to compile it with a C++ compiler, it has to be legal C++ code. There is no other option. A compilation unit has to be compiled by a single compiler. – David Schwartz Feb 26 '19 at 22:25
  • @DavidSchwartz It means that I should compile and create a C library from my source and after that I can use that in my C++ project? – morteza ali ahmadi Feb 26 '19 at 22:26
  • 1
    That is one of your two options. The other is to make the C code compile with a C++ compiler by, for example, adding casts. If you choose to compile it with a C compiler, you can use `extern "C"` in your C++ code to allow the C and C++ code to successfully link after being compiled by the two compilers. – David Schwartz Feb 26 '19 at 22:29
  • @DavidSchwartz thanks, I built my source file and now I have a lib*.so. I added this lib to my project and I use `extern "C" { #include "mySource.c"}` in mainwindow.cpp but in compiling, the mentioned error still appears. – morteza ali ahmadi Feb 26 '19 at 22:43
  • @mortezaaliahmadi Yeah, don't do that. In the `extern "C"` block, you must put C++ code that explains how to link to the C code. Don't include the actual source! (Look at some examples of how to properly use `extern "C"`.) – David Schwartz Feb 26 '19 at 22:52
  • @DavidSchwartz I used this [https://isocpp.org/wiki/faq/mixing-c-and-cpp] – morteza ali ahmadi Feb 26 '19 at 22:55
  • Yep, read the section on calling C functions from C++. – David Schwartz Feb 26 '19 at 22:55
  • @DavidSchwartz Until I don't identify the mySource.c to my project, I can't access to a function in it. How can I identify the source to mainwindow.cpp? – morteza ali ahmadi Feb 26 '19 at 23:02
  • 1
    Follow the examples. If the library provided some include files to use, use those. If not, make them yourself. If you need to call a C function like `int foo(int, double);` then put in your C++ code, `extern "C" int foo(int, double);`. – David Schwartz Feb 26 '19 at 23:28

0 Answers0