LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Invoking C++ from C (https://www.linuxquestions.org/questions/programming-9/invoking-c-from-c-694338/)

jiml8 01-01-2009 08:17 PM

Invoking C++ from C
 
I'm not sure if this is a simple thing or not, but so far I can't get it to work.

I am building a gtk/glade based GUI using a mixture of C and C++. Essentially, I have to do my event handlers in C because that is what gtk expects, but I want to do the bulk of my work in C++.

So, I have a C++ framework that defines classes to open forms and define widgets and invoke gtk through the gtkmm libraries, and all that works.

When you click on a button on the resulting GUI, the handler for that function is a C routine, compiled separately, and referenced in the C++ code as an extern "C" module.

So, I want to go from that handler in C right back to C++ and I do not seem to be able to do it.

Here is a sample.

In the file "sighandlers.c" I have this, which is a function invoked by clicking on a button on my GUI.
Code:


G_MODULE_EXPORT void on_button1_clicked(GtkObject *object, gpointer user_data)
{
    connect_with_client();
}

the module connect_with_client() is also a C function, in a file named cmd_button_handlers.c, and it currently looks like this (note: not done and primitive)
Code:

void connect_with_client()
{
printf("connect with client button pushed\n");
        if(sockfd < 0)
        {
                char *thisuri = geturi();
                char *thisport = getport();
printf("uri %s port %s\n",thisuri,thisport);
                sockfd = Tcp_connect("192.168.0.10","2020");
                if(sockfd > 0)
                {
                        send(sockfd,"hello from dadsbox",18,0);
                        printf("sockfd is %d\n",sockfd);
                }
        }
}

Now, the functions geturi() and getport() are trial functions of mine that are defined in the main C++ file. I say "trial" because what they are supposed to do is fairly obvious from the names, but they are not working.

Here is the code:
Code:

char *geturi()
{
        return (char *)(ca.get_uri().c_str());
}
char *getport()
{
        return (char *)(ca.get_port().c_str());
}

As you can see, the architecture here is C++ and the intent is to obtain the desired information from a class that has that information tucked away.

Problem is that it won't link. I keep getting this message:
Code:

cmd_button_handlers.o: In function `connect_with_client':
/mnt/sde1/home/jiml/commander/cmd_button_handlers.c:17: undefined reference to `geturi'
/mnt/sde1/home/jiml/commander/cmd_button_handlers.c:18: undefined reference to `getport'

Near the top of the cmd_button_handlers.c file, I do have this prototype:
Code:

extern char *geturi();
extern char *getport();

So, this points to a general problem; moving from C back to C++ when programming a mixed environment.

When I google for this, what I come up with is lots of people trying to mix C# and C++. I haven't found anything useful.

Going from C++ to C is very straightforward. But how do I go back?

paulsm4 01-01-2009 09:17 PM

Hi -

Yes, in general you can certainly co-mingle C and C++ (call C from C++ and, with relatively few restrictions, also call C++ from C).

You're declaring your C++ entries as C-callable with "extern C{}", aren't you?

'Hope that helps .. pSM

jiml8 01-01-2009 10:14 PM

Quote:

Originally Posted by paulsm4 (Post 3393953)
Hi -

Yes, in general you can certainly co-mingle C and C++ (call C from C++ and, with relatively few restrictions, also call C++ from C).

You're declaring your C++ entries as C-callable with "extern C{}", aren't you?

'Hope that helps .. pSM

I was declaring my C functions that were being called from C++ that way, but I wasn't aware that the same declaration was used to go the other way.

Usually, I don't do this code mixing this way; since gtk is single threaded I generally spawn multiple threads and the gtk thread signals other threads to do things. The gtk thread will be C, and the other threads are whatever I deem necessary. This particular interface is so simple that I decided to do it all in one thread, but some of it has to be C.

Properly placing some extern "C" for C++ code to be called from C does in fact work. Thanks.

chakka.lokesh 01-01-2009 11:36 PM

Quote:

Originally Posted by jiml8 (Post 3393919)
Code:

cmd_button_handlers.o: In function `connect_with_client':
/mnt/sde1/home/jiml/commander/cmd_button_handlers.c:17: undefined reference to `geturi'
/mnt/sde1/home/jiml/commander/cmd_button_handlers.c:18: undefined reference to `getport'


What about the definitions of geturi and getport functions. Are they present as the member functions in the class to which ca is object?


All times are GMT -5. The time now is 07:42 PM.