Newbie question - using C
-
Hi there, I am evaluating Qt for a project. I already have the model code written in C and can invoke it under swift (MacOS, iOS) and Java (android through JNI). Can I import this code into a Qt project and use Qt as a cross platform GUI to invoke the code (instead of using swift/java/whatever)?
I am sure that this hs been answered/done so a pointer to an existing resource/answer/document would be welcome. I tried including my source and headers but some(or all) did not seem to be recognised.
Aethelnorn
-
Hi and welcome to the forums.
Yes, if you mean calling c function
from c++ code that should just work.
https://isocpp.org/wiki/faq/mixing-c-and-cppYou might to wrap includes in
extern "C" {
#include "my-C-code.h"
} -
I tried including my source and headers but some(or all) did not seem to be recognised.
It should be as @mrjj says. But you may have to show us what you tried/what you mean by "did not seem to be recognised".
-
Ok thanks both... sorry for the slow reply but I have only just seen this (I was expecting the forum to email me when a reply came in. Must have done something wrong).
Not at my home 'puter at present but this is what I did:
- create/use example project analogue clock
- show that I could run it desktop, iOS simulator, android simulator.
- use 'add directory' to add my c files to the source tree, same to add my h files to the header tree. I did not attempt to call any of my C from Qt.
- Compile now fails with '.jinclude.h' not found in one of my C source files. (jinclude.h is just one of many includes in my C code) - And I checked, it really is there.
So guess that it could be the need to include the 'extern "C"' thing, but as none of the CPP code references the C code is this really the cause? It is a long time since I coded CPP and C for real (apart from this latest endeavour) so my skills are very rusty. I am assuming that I am making a very obvious mistake due to being new to QT or rusty with C/CPP.
- Aethelnorn
-
@aethelnorn
the c files and its h are in same folder as the qt pro file ?
else you might need to add the .h files to
HEADERS
for it to see them. -
@mrjj No. I think I added the files as external directory references - I could not drag the files from the finder (MacOS) to the Qt IDE, and found the right-click menu option to add files in external folder. [I would remove these if I could, but the only way seems to be individually :-(. ]] The files appear in the source tree in QT under the directory structure that I added.
-
@aethelnorn
ok if you includes are not like
"folder/filename.h"
you need to use the HEADERS to tell where to look. -
OK, thnks, that sounds about right. This is what I have:
the errors include:
/Users/phill/dev/SmugglerMac/SmugglerMac/core/smuggler/smugbase.h:14: error: 'jinclude.h' file not found
#include "jinclude.h"
^~~~~~~~~~~~I am happy to remove the files from the tree (if I knew how) and add them directly to the project, but I am also happy for them to stay where they are too.
Where do I set HEADERS?
-
@aethelnorn
Hi
In that case
http://doc.qt.io/qt-5/qmake-variable-reference.html#headers
INCLUDEPATH
is the one you are after.
You add it to the .pro file.
Make sure to run qmake in menu after.
you can use absolute paths or relatives. -
Thanks, that helps some.
That .pro file is the centre of things isnt it?. I can add and remove files in there manually? As long as I run QMake after?
I added:
INCLUDEPATH = ../../../../../SmugglerMac/SmugglerMac/core/libjpeg ../../../../../SmugglerMac/SmugglerMac/core/smuggler/aes ../../../../../SmugglerMac/SmugglerMac/core/smuggler
and now get :
-1: error: symbol(s) not found for architecture x86_64
:-1: error: linker command failed with exit code 1 (use -v to see invocation)I am building desktop Qt 5.12.0 clang 64bit
should I have appended to the existing INCLUDEPATH somehow rather than declaring it as I did?
-
@aethelnorn
Hi
Yes you can edit .pro file manually all you want. Its the project file and
defines how to build a project.
if you have an existing INCLUDEPATH
then please do
INCLUDEPATH += ../xxxxxx
so you dont override the already set values. -
@mrjj I tried the += but it made no difference.
I deleted all my 'extra' files from .pro, and it all compiles and runs again.
So something in my C files is interfering with the make to give the linker error. That is me flummoxed, as most of the files are open source and so I do not want to modify them (even if I did understand them). It would be a shame if the project failed at this point, because I am impressed with what I have seen of Qt so far.
The only thing that I can imagine is that one of the component projects is jpeg-9c which had a qmake system of its own, and may have generated some of the header files in a way that confuses qmake in Qt? I can drop the exact same set of files into XCode swift project and they compile a treat (iOS or MacOS). I have also compiled them inside android studio and accessed them through JNI.
-
@aethelnorn
well did you include more than .h and .c files ?
Like dylib file or someting like that ?
If all the c files compiles its a good sign. -
@aethelnorn
Ok, can you check the output pane to find out what exactly fails ?
like here
https://stackoverflow.com/questions/18973042/symbols-not-found-for-architecture-x86-64-on-qtcreator-project -
@mrjj But yes, compile is a big step forward.
I have looked at the stackOverflow article, and I think you are onto something there a symbol (an array) is not being found. However the array is #defined to something else (in a #ifdef and I do not know if that evaluates true at that point). Lots of other breaks in that file which makes me surprised that it compiles. Maybe the compile fails do not prevent linking as here are lots of platform-alternates here.
jpeg-9c has a lot of macros etc to allow multiplatform and it looks as if the Qt build system is choking on them. So am I to be honest - too many levels of indirection spread across too many files.
I need to break off for now, but I will be back tomorrow after a rest. Maybe I will gain some more clarity with a new look in the morning. Thanks for your help so far - it is much appreciated.
-
@aethelnorn
the c code might have some
ifdef
..code..
endif
that should be defined for it to link.
Note that Creator should show if a define is enabled -
@aethelnorn while it in principle possible to compile everything with qmake, the question arises if the external libs should be build with their 'native' buildsystem and just be linked into your project.
At least thats the way most projects handle it, an thats also cleanest regarding licenses (GPL e.g.)
-
@aha_1980 I can see where you are coming from, but to compile the library first I have to install and understand all the cross-platform compiling technology. This is not something that attracts me. I was hoping that I could delegate that work to Qt. If I need to precompile the libraries then I have two projects (Qt and cross compile system) instead of one. That is very little improvement over my current solution (Xcode & Android projects) and requires extra learning.
If I can convert an automake project into a Qt project that might help - jpeg-9c is set up for automake. Two Qt projects would be an attractive solution. As this is a private project I will need to wait until this evening to give it some more time.
As for licensing, both the open source projects I use are very permissive, and the resultant code when finished will be open-sourced (GPL3 most likely if Qt is part of the solution).