3

I have an embedded IoT application that I developed for a customer 1.5 years ago. The application is quite large and written in C. It continues to work fine.

The customer asked me add an HDMI touch display along with some complex graphics. I selected Qt as the graphics framework. Qt is great, however, it is written in C++. Not wanting to re-write the C code to C++, I refactored the C code to collection of callable methods or services.

Yes, I know C should compile and execute properly in C++. However, anyone who has done that must first develop the C code with the assumptions that it will be compiled and executed in a C++ environment. Otherwise you get lots of compile errors and a lot of rewriting.

I created all of the C++ classes and C services. I first compile the C code using gcc and create c-object.o file. I used the following in my c-code header file:

#ifdef __cplusplus
extern "C" {
#endif

I first compile the C-code and generate the c-object.o. As crazy as it seems, I use Qt creator to generate the .pro files, qmake to generate the Makefile and make to compile and link.

I get all kinds of link errors because the Makefile has no reference to the C-code. However, make outputs all of its g++ commands to the display. I simply copy and paste to notepad the last make g++ command that links all of the cpp-object.o files -- the one that creates all of the link "object reference not found" errors. I then copy and paste to notepad the c-object.o files names and library names in the g++ command. I re-run the modified g++ command and everything works. chorusclient.o is the c-object.

g++ -Wl,-O1 -o AvEdgeDemo chorusclient.o main.o login.o hud.o /
sqliteservices.o driveraccess.o maintenanceaccess.o /
fleetmanagementaccess.o iotdataaccess.o keyboard.o /
moc_login.o moc_hud.o moc_driveraccess.o /
moc_maintenanceaccess.o moc_fleetmanagementaccess.o /
moc_iotdataaccess.o moc_keyboard.o    /
-L/usr/lib/arm-linux-gnueabihf -lQtSql -lQtGui /
-lQtCore -lpthread -lssl -lcrypto -ljson-c

Is there a way that I can get qmake or make to recognize the C-code and properly compile and link? The cutting, pasting, and re-linking is awkward to say the least!

1 Answer 1

3

I have no idea why are you doing all of this. QMake handles C code just fine. In a qmake project (or a cmake project!), just add the .c and .h files to the project. That's all.

I mean - do this:

# AvEdgeDemo.pro    

template = app
qt = widgets
sources = \
  chorusclient.c \
  sqliteservices.c \
  main.cpp \
  login.cpp \
  ...

headers = \
  chorusclient.h \
  sqliteservices.h \
  login.h \
  ...

However, anyone who has done that must first develop the C code with the assumptions that it will be compiled and executed in a C++ environment. Otherwise you get lots of compile errors and a lot of rewriting.

This applies to the interface only, i.e. to the headers. If your headers are proper clean C, they will be clean C++ as well. Otherwise - you had yourself a mess, and C++ only forced you to clean it up.

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

3 Comments

I agree, a very strange way to get it compile correctly. Getting stuff to work is the mother of all invention! I did include the c-code.h file in the Qt class that makes the call to the c-code services. For some reason QT creator or qmake is not picking up the "#include "chorusclient.h". Like I said, I can cut and paste g++ command lines and get it to compile and link correctly. No magic, just the correct object files and necessary library names. Is there something I need to do special in Qt to get qmake to recognize the c-code? I am not that familiar with qmake.
a very strange way to get it compile correctly STOP. Forget all that you did up to this point. Open up the .pro file for the Qt project. Add the .c and .h files to the SOURCES and HEADERS. You're done. Please, don't complicate it any more than that. See the edit.
Worked great. Although not obvious in my problem statement, I had to include the libraries in the .pro file as well, INCLUDEPATH += -L/usr/include \ LIBS += -lssl \ LIBS += -lcrypto \ LIBS += -ljson-c.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.