-   Programming (
-   -   Libraries and other questions (

citizen_lc 04-03-2005 04:06 PM

Libraries and other questions
I am writing this post because I am interesting in programming in C++ and compiling it by G++.
My first question is about libraries. I am accustomed to writing programs in Borland C++ Builder ( I was trying to use Kylix, but there are a mistakes... maybe I will write about it in next post). In BCB at the beginning of any *.cpp file is list of included libraries like this:
#include <iostream.h>
#include <conio.h>
My question: which libraries i had to include in g++?
I was trying to include iostream but than cout and others very simples functions simple doesn't work - program do nothing.
And my second problem: I hope that language C++ is the same under Linux and under Windows... F.e. cout, cin, loops works normally?
Best regards

cyent 04-03-2005 04:49 PM

I think conio is a non-standard Borland header. What are you using from it?

Umm, you are also confusing two things here, headers and libraries. #include includes headers, the functions declare in the headers may be found in objects files or libraries and that you need to specify to the linker. A library is exactly a bundle of object files.

There after (at lest for the stdc++/stl stuff, if you compile and link with g++, magic happens and all the right libraries are pulled in for you.)

citizen_lc 04-03-2005 05:00 PM

Can you write it again but more simple? :rolleyes: I'm afraid I don't understand exactly what do ypu mean...
from conio.h - getch (IMHO very useful :))

coolman0stress 04-03-2005 05:45 PM


I was trying to include iostream but than cout and others very simples functions simple doesn't work - program do nothing.
Include this:

#include <iostream>
using namespace std;


I hope that language C++ is the same under Linux and under Windows... F.e. cout, cin, loops works normally?
Will work the same. You'll only have problems with specific platform libraries.


from conio.h - getch (IMHO very useful :))
conio.h is windows only. You'll have to use/learn ncurses.h (or some other GUI lib) on Linux.

cyent 04-03-2005 06:41 PM

* conio is non-standard, doesn't exist under g++.
* Your english is a lot better than my Polish... :-) but you are missing a subtle point here. A Library in the gcc world is a bundle of .o object files, what you #include is just called a header file, not a library.
* I have a funny feeling you are mixing two problems here.
- Problem one programming in g++
- Problem two programming using Borland products.
I can help you with problem one, but question regarding problem two are probably better asked in a Borland specific forum. (Anyway here's a hint for Problem may need to add g++ -I/path/to/borland/headers/include)
* The old Borland / MS-DOS world provided us with a wonderfully simplistic view of the keypress world. The "conio" stuff hides an amazingly amount of complexity from us. In the Linux world the xterm command line interface reminds us of ye olde DOS and we fall into the trap of thinking we are talking directly to our program when we make a key press. Life is _much_ more complicated than that.

When you press a key, the kernel gets it, passes it on to X-Windows, which passes the event on to the Window Manager which decides which window has focus, passes the event onto that window. If that window is an xterm (or gnome-terminal or konsole) it buffers the keypress until an enter key is pressed and then sends the whole line to the stdin of the program. Phew.
Now for efficiency reasons the iostream stuff _also_ buffers input.
So to get the "wait for keypress" effect you have to tell _both_ the xterm _and_ the iostream stuff _not_ to buffer anything and just hand you keypresses. Hint 1: Don't use iostream. Use raw low-level I/O. The next gotcha is to tell the xterms not to buffer, you can do that via stty or the termios stuff. Hairy. The problem is Unix still has the notion that these terminals are smart terminals sitting at the end of an RS-232 line communicating to a mini-computer / mainframe!

The simplist work around for all this is change from "hit any key to continue" to "hit enter to continue" :-)

Less simple work around is to use libraries like curses or libtexttool.

Once you into that, it may be easier to go to libQt and do a full windowing app.

citizen_lc 04-04-2005 08:55 AM

I think I understand... But can you give me an simplest example of program to compile under g++ that has just to say "Sunday" and wait for pressing a key? BTW can't find any useful tutorials... Each of them is about writing applications with GUI. If you know some links please give me :)
Thanks a lot, best regards

cyent 04-04-2005 03:22 PM

Now that is an interesting thought.

Back in the bad old days of DOS I used to write programs that would ask you for this then ask your for that then ask your for the other and then do whatever.

When I came to linux I immediately hit the same problem as you. It threw me for a while.

But do you know, I have been working full time in linux for many years now, but I haven't written one such program!

You see, I have changed over entirely to the Unixish command line option way of writing programs.

If you look in "info getopt" somewhere around there is a worked example.

ESR in
puts it very nicely. Write each program to do one thing and do it well, and expect it's input and output to come from and go to another (as yet unspecified) program.

The other curiosity is I simply haven't bothered with writing GUI's for years. Command line is much more productive, and if I need the friendliness of a GUI, I write a CGI web interface!

Write it, clip it to my web server, and the app is delivered instantly to the entire world. Upgrades? No problem, upgrade the version on my web server, and the entire worlds version is upgraded instantly.

cyent 04-04-2005 03:32 PM

Ok, so that answer was a little theoretical. But to make it a bit more concrete, why do you want getch? To allow your program to pause until the reader has read the screen and is ready to go on.

But what if the user want to go back a page? What if he wants to search the output?

Why not ignore the problem and let the output just pour out.

And then pipe it to less.

myprog | less

Or if he just wants to see the last few lines to check see the final answer...

myprog | tail

Or find a string..
myprog | grep -i error

The other reason for reading from the console is to gather parameters.

But that makes it hard to script. It makes it hard to use your program inside another program.

It also forces the user to answer a bunch of questions that he probably just wants to take the default value.

lowpro2k3 04-04-2005 07:56 PM


#include <iostream>
using namespace std;

// Or:
// using std::cout;
// using std::cin;
// using std::endl;
// using std::string;  etc...

int main()
    cout << "Hi there" << endl;

    return 0;

All times are GMT -5. The time now is 03:20 PM.