Writing a GUI (C++) for a chat app (C) - how?

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
 
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.
 
expl said:
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.
 
caesius said:
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.
 
No, C is NOT always C++ dammit!

expl said:
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.
expl said:
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
 
caesius said:
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
 
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!
 
Brandybuck said:
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
 
Brandybuck said:
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:

Code:
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?
 
caesius said:
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:
Code:
#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:
Code:
#include <iostream.h>

[red]extern "C"
{[/red]
  void foo(int arg);
  int feep(void);
[red]}[/red]

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++.
 
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.
 
Thanks fonz, very helpful post, I had actually found that link but your compiler summary with the flags cleared things up (about linking etc)
 
Back
Top