Jump to content

Using c++


Sting

Recommended Posts

Your main page shows that I can use c++ to build my application.  Since the previous version was in c++ I thought I'd see if I can figure it out.

I have a widget in my local widgets directory and I can use it from main with no problem.  I just include the header and it works well.

I create a c++ class GThermo to attempt to encapsulate the widget in a class.  From a base class with nothing really defined if I include the header for my widget I get the error:

../gcomp/GThermo.cpp:29:1: error: expected ‘}’ at end of input
 }

without the included header it compiles as one would expect.

Is there something unique to this environment that has to be done to make this work.  

Link to comment
Share on other sites

There is not enough information in your post above for us to make any sort of determination as to what is wrong.

Ugfx is c++ compatible. That is, the public api header files can be included in c++ source files and the ugfx api can be used from c++. The ugfx source itself (the .c files) still need to be compiled using a c compiler. Make automatically uses the correct compiler for source code based on the file extension.

 

Link to comment
Share on other sites

I hope you don't misunderstand me here.  I trust that µGFX works well with c++, just not for me.  Something is wrong with my environment and with these questions and answers, maybe I can figure it out.  There isn't a whole lot of information to give here.  I create a c++ class and add

#include "gfx.h" in the header for the class and the class will not compile.  I get the message posted above.

Today I added -std=c++11 for the c++ compiler and I can now add that include and it works.  I can also add widgets that are built in the src tree.  I tried the label widget and it works well.   All I have to add is the gfx include and all of the symbols are available.  Because my widgets are not built in the gfx tree then they are not available including gfx.h so i include the header for the widget, which is where the issue presents itself.

to get the widget to compile I have to include gfx.h and gwin_class.h in the header for the widget.  When I do this I get the same message as above.

Link to comment
Share on other sites

It is quite possible that gwin_class.h is not c++ compatible as it is an internal ugfx implementation header file.

If so, it wouldn't be hard to make it c++ compatible. You just need to add the appropriate extern "C" block around the api functions.

 

Link to comment
Share on other sites

gwin_class.h is in the widget that is written in c.  

In the header of my c++ file if I include "gfx.h" and no other references ot anything gfx related.  The class compiles.  The include gwin_class.h is in the widget.

If I include the header file for local widget in the header for the class I get the error in previous post.  

I moved the c++ files to be built first and the error is the same.

I built a prototype that used the label widget in my class and it works perfectly with just the inclusion of gfx.h in the header of the class file.  This is true when the class is first in the list of files to be built or last.  All of the widget entry points are available.  

I have no experience using include with c files I will sort through how gfx.h works to see if I can figure out how to build to get my local widgets included.

Link to comment
Share on other sites

Have a look at the oscilloscope demo under demos/audio.

It creates a simple custom widget from outside the normal ugfx gwin directory. That will hopefully give you some hints.

It is virtually impossible for us to be able to diagnose any further without access to your project and source files. All we can do is provide some hints on where to look.

Link to comment
Share on other sites

Thanks, I looked at the oscilloscope demo.  I can create widgets outside of the source tree.  I link to them in main and everything works well.  I am trying to figure out how to encapsulate a widget into a c++ class.  At this point I think my problem is, I haven't looked at the underlying code enough I just want it to work :), and I am using a class variable for the GWidgetInit and the GHandle.  All of the code works correctly except the widget won't draw.  My next attempt will be to allocate these items in fixed memory and pass them into the class to be used.

Is there a demo someplace that uses c++ with widgets?

Link to comment
Share on other sites

I got it to work, however I don't think it is too pretty :)

in the top of main I create the GHandle as

GHandle        ghThermo;

// pass in address of the handle to the constructor 

GThermo* gThermo = new GThermo(&ghThermo, 230, 140);

// The constructor
GThermo::GThermo(GHandle* ghThermo, int x, int y)
{
    ghandle = ghThermo;
    this->x = x;
    this->y = y;
	// Fixed width widget, any number will do
    this->width = 0;
    this->height = 0;

    wi.g.x = x;
    wi.g.y = y;
    wi.g.width = width;
    wi.g.height = height;
    *ghandle= gwinThermoCreate(0, &wi);
    gwinThermoSetBorder(*ghandle, TRUE);
}

This is not how it will be in implementation, it is just to see what needed to be done to get it to work.

Anyone with a better idea I would certainly appreciate it.  

Link to comment
Share on other sites

There is no need to pass that GHandle around. Just make that GHandle a private member of your wrapper class. The reason we need those handles is because there are no objects in C. The handle is the object. When you wrap a C++ class around it, the object/instance of that class "becomes" the handle.

This is how we are doing it (I stripped the unrelated parts out):

wrapper.h
wrapper.cpp

Link to comment
Share on other sites

  • 1 year later...

Hello,

I'm sorry that I dig out that old topic, but since my question is related to it I didnt want to open a new topic. It is not possible to integrate C++ sources into my project which use C++ header files like <iostream> and std:: functions, right? Because I get a lot of errors when I try that, or is this just a problem of the GNU gcc compiler?

Thanks in advance.

Link to comment
Share on other sites

You can mix C and C++ without any problems. You just have to make sure that you use the C++ compiler to compile C++ sources and the C compiler to compile C sources. That seems to be the most common mistake.

Other than that, you can of course not include C++ code (eg. #include <iostream> or #include <functional>) in code that gets compiled by the C compiler.

Link to comment
Share on other sites

Thank you for the fast answer, that is good to know. So that means I just need to change my Makefile in order that the .cpp files get compiled by the C++ compiler, right now I am using the example Makefile for the Linux-SDL board which uses the gmake script and so far I thought it is detecting from the ending of the files wether to use the gcc or g++. 

I am not really familar with setting up a Makefile, so if someone has an advice for me, that would be great. :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...