View Full Version : Writing a GUI (C++) for a chat app (C) - how?
caesius
December 27th, 2009, 22:41
A bit of background, over the uni holidays I decided to give network programming in C a real good crack. I wrote a simple LAN chat server and now I want to write a chat client GUI and had planned on using Qt, which is C++.
Now I see the dilemma: how can I write a chat client using Qt when I need network code, which is all in C?
Obviously there must be a way around it, or apps like KMess and Konversation wouldn't exist..
Thanks
expl
December 27th, 2009, 22:57
Well there are several ways to make it work.
* C++ compiler will compile C code, so you can take your C program and add any C++ code on top of it and it will all work. Even tough its ugly and bad programming practice.
* Write your GUI as a separate library and call it from your main program.
* Ive heard there are C bindings for QT but cant tell more about it since I do not use QT.
* Use GTK instead of QT.
caesius
December 27th, 2009, 23:10
Well there are several ways to make it work.
* C++ compiler will compile C code, so you can take your C program and add any C++ code on top of it and it will all work. Even tough its ugly and bad programming practice.
* Write your GUI as a separate library and call it from your main program.
* Ive heard there are C bindings for QT but cant tell more about it since I do not use QT.
* Use GTK instead of QT.
I've already tried to compile my network code with g++, it doesn't like the void * pointers and a few other things.
Hmmm option three looks like what I want - could you please elaborate on this? Or point me somewhere that explains it? Thanks.
expl
December 27th, 2009, 23:20
I've already tried to compile my network code with g++, it doesn't like the void * pointers and a few other things.
Hmmm option three looks like what I want - could you please elaborate on this? Or point me somewhere that explains it? Thanks.
An ANSI valid C code will always compile with C++ compiler. Can you post me code that fails to compile?
A quick google search gave me information that there were C bindings for QT (called QtC) but project was dropped by maintainer.
fonz
December 27th, 2009, 23:59
C++ compiler will compile C code, so you can take your C program and add any C++ code on top of it and it will all work.
An ANSI valid C code will always compile with C++ compiler.
False.
C is not C++. C is not a strict subset of C++ (and vice versa: C++ is not a strict superset of C either). There are several constructs that are valid in (ANSI) C but not in C++.
It's usually not very difficult (sometimes even trivial) to modify C code so that it's also valid C++, but stating that C code will always compile as C++ without any effort/modification is just plain wrong.
Alphons
fonz
December 28th, 2009, 00:07
I've already tried to compile my network code with g++, it doesn't like the void * pointers and a few other things.
Void pointers require explicit casts in C++ (the reason why is beyond me, but that's the way it is). Feel free to post the other errors (plus relevant code snippets).
Alphons
Brandybuck
December 28th, 2009, 02:19
C++ can call C functions, so there is no problem there. Use Qt for you GUI, and call the C network code functions. But C++ is stronger typed than C, so you will have to do some casts back and forth.
Using Qt/C++ as a front end GUI, and a specialty C library as the back end, is a very common use pattern. Go for it!
fonz
December 28th, 2009, 04:05
C++ can call C functions, so there is no problem there.
For the sake of clarity, are you referring to the extern "C" construct? If so, yes, you are right.
Alphons
caesius
December 28th, 2009, 09:19
C++ can call C functions, so there is no problem there. Use Qt for you GUI, and call the C network code functions. But C++ is stronger typed than C, so you will have to do some casts back and forth.
Using Qt/C++ as a front end GUI, and a specialty C library as the back end, is a very common use pattern. Go for it!
Sorry for the non-specific question but how exactly can C++ call C functions? Do you literally compile it like this:
g++ -o program program.cpp c_file.c other_c_file.c
and then simply call the functions inside the C files from program.cpp?
fonz
December 28th, 2009, 13:45
Sorry for the non-specific question but how exactly can C++ call C functions?
Here's a stupid but hopefully illustrative example:
A simple C file foo.c:
#include <stdio.h>
void
foo(int arg)
{
(void)printf("C says: %d\n",arg);
}
int
feep(void)
{
return 666;
}
A simple C++ file bar.cc:
#include <iostream.h>
extern "C"
{
void foo(int arg);
int feep(void);
}
int
main(void)
{
foo(6);
int bla=feep();
cout<<"C++ sucks"<<endl;
return 0;
}
To compile:
cc -c foo.c
g++ -c bar.cc -Wno-deprecated
g++ foo.o bar.o
To summarize:
Compile (not link) your C code as C code with a C compiler.
In your C++ code, "import" the C functions you need through an extern "C" block (I recommend using header files).
Compile (not link) your C++ code as C++ code with a C++ compiler.
Link the object files with a C++ compiler[sic].
You may also find the following page handy (despite all accusations, Google is still your friend): http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html
Hope this helps,
Alphons
P.S. I used the -Wno-deprecated flag because apparently the C++ that I learned is already obsolete. You may not need this flag if you learned recent C++.
kpedersen
December 28th, 2009, 18:52
I don't know if it is an option but when I work with sockets / netcode I would write a class in c++ using either sig++ (for the receive callbacks) or using a derived Socket Adapter class to extend and recieve the callbacks that way.
caesius
December 28th, 2009, 20:31
Thanks fonz, very helpful post, I had actually found that link but your compiler summary with the flags cleared things up (about linking etc)
bigearsbilly
January 4th, 2010, 01:15
QT:
QTcpSocket
QTcpServer
look in the qt4-assistant devel/qt4-assistant under QtNetwork
vBulletin® v3.8.7, Copyright ©2000-2013, vBulletin Solutions, Inc.
0