LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-03-2005, 05:06 PM   #1
citizen_lc
LQ Newbie
 
Registered: Apr 2005
Location: Poland
Distribution: Slackware 10.1
Posts: 10

Rep: Reputation: 0
Libraries and other questions


Hello,
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>
etc.
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
 
Old 04-03-2005, 05:49 PM   #2
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 346

Rep: Reputation: 76
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.)
 
Old 04-03-2005, 06:00 PM   #3
citizen_lc
LQ Newbie
 
Registered: Apr 2005
Location: Poland
Distribution: Slackware 10.1
Posts: 10

Original Poster
Rep: Reputation: 0
Can you write it again but more simple? I'm afraid I don't understand exactly what do ypu mean...
from conio.h - getch (IMHO very useful )
 
Old 04-03-2005, 06:45 PM   #4
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
Quote:
I was trying to include iostream but than cout and others very simples functions simple doesn't work - program do nothing.
Include this:
Code:
#include <iostream>
using namespace std;
Quote:
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.

Quote:
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.
 
Old 04-03-2005, 07:41 PM   #5
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 346

Rep: Reputation: 76
* 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 2...you 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.
 
Old 04-04-2005, 09:55 AM   #6
citizen_lc
LQ Newbie
 
Registered: Apr 2005
Location: Poland
Distribution: Slackware 10.1
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
Old 04-04-2005, 04:22 PM   #7
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 346

Rep: Reputation: 76
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
http://www.faqs.org/docs/artu/
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.
 
Old 04-04-2005, 04:32 PM   #8
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 346

Rep: Reputation: 76
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.

eg.
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.
 
Old 04-04-2005, 08:56 PM   #9
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Code:
#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;
    system("pause");

    return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Libraries lyar1031 Linux - Software 0 08-31-2004 08:23 PM
g++ libraries ofada Programming 1 04-09-2004 12:47 PM
Libraries? where to get Magick211 Linux - Software 8 03-18-2004 10:53 PM
document storage/libraries - more questions bigjohn Linux - Software 0 10-15-2003 12:53 PM
3 Quick questions about libraries Stephanie Linux - General 2 01-24-2002 05:16 PM

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

All times are GMT -5. The time now is 04:40 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration