LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-01-2009, 08:17 PM   #1
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
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?

Last edited by jiml8; 01-01-2009 at 08:25 PM.
 
Old 01-01-2009, 09:17 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 01-01-2009, 10:14 PM   #3
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Original Poster
Rep: Reputation: 116Reputation: 116
Quote:
Originally Posted by paulsm4 View Post
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.
 
Old 01-01-2009, 11:36 PM   #4
chakka.lokesh
Member
 
Registered: Mar 2008
Distribution: Ubuntu
Posts: 270

Rep: Reputation: 33
Quote:
Originally Posted by jiml8 View Post
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
segmentation fault when invoking new George2 Programming 15 05-15-2006 03:14 PM
Invoking the X Windows System in OpenBSD halfpower *BSD 4 07-31-2005 04:30 AM
Problem invoking user mago Linux - General 4 03-29-2005 04:25 PM
Invoking other processes velan Debian 2 06-04-2004 09:39 AM
Invoking bootloader radhapriya Programming 1 03-27-2003 07:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration