Clear screen in terminal?

Feb 19, 2008 at 10:21pm
I always see threads (not here) about clearing the screen with
system(cls);
but that is only for windows.
What about us *nix people?
Is there something like that I can use with the terminal so it will clear?

Thanks.
Feb 19, 2008 at 10:38pm
The unix command is usually "clear". (Go ahead and try typing it at the shell prompt.)

That said, the advice is usually the Wrong Thing. Unless you are a student taking a simple university course, don't ever system( "anything" ) just to accomplish some simple goal like pause or cls. It is a huge resource hog, it is slow, and it is a huge, gaping security hole in your program.

Check out this thread I responded to not too long ago for an example of how to clear the terminal screen in Unix/Linux:

http://www.cplusplus.com/forum/beginner/387/

Hope this helps.
Last edited on Feb 21, 2008 at 12:50am
Feb 19, 2008 at 10:56pm
Thanks a lot dude.

I am using Linux so I know all the basic (some advance) commands.
Oh and I'm not going to a university, I'm only in 9th grade.
And I knew to never use system(anything) because it only applies to windows. But thanks for the extra reasons.
Last edited on Feb 19, 2008 at 10:59pm
Feb 19, 2008 at 11:04pm
When you said "This requires the terminfo/curses libraries. Link with -lterminfo or -lcurses (whichever works)."

What exactly am I supposed to download and were do I put it?
Feb 20, 2008 at 1:47am
It is a standard library that gets tucked away in the /include and /lib directories just like all other libraries, so your compiler can find and use it without any grief for you.

If you are on *nix, chances are that it is already installed. If not, just "apt-get ncurses", or ask your administrator why your system is in the dark ages and if you could get it in the modern age.

If you are on Windows, google "pdcurses", and follow the instructions in the archive. Link with -lpdcurses.

On some systems the linking is a bit weird. Years ago the Sun OS at my school required me to include </usr/local/include/ncurses.h> (nonstandard!) and link with -lncurses. Weird stuff. (And annoying, because it is nonstandard and inhibited making portable programs without playing with the preprocessor... alas.)

Hope this helps.
Last edited on Feb 20, 2008 at 1:48am
Feb 20, 2008 at 2:07am
Heres what I get when I try to compile like this: "$ gcc project.cpp clearscreen.cpp -lcurses"

1
2
3
4
5
6
7
8
9
10
11
clearscreen.cpp: In function ‘bool console_initialize_terminfo()’:
clearscreen.cpp:13: error: ‘STDOUT_FILENO’ was not declared in this scope
clearscreen.cpp: In function ‘void console::clear(bool)’:
clearscreen.cpp:28: error: default argument given for parameter 1 of ‘void console::clear(bool)’
clearscreen.hpp:9: error: after previous specification in ‘void console::clear(bool)’
clearscreen.cpp: In function ‘bool console_initialize_terminfo()’:
clearscreen.cpp:13: error: ‘NULL’ was not declared in this scope
clearscreen.cpp:13: error: ‘STDOUT_FILENO’ was not declared in this scope
clearscreen.cpp: In function ‘void console::clear(bool)’:
clearscreen.cpp:28: error: default argument given for parameter 1 of ‘void console::clear(bool)’
clearscreen.hpp:9: error: after previous specification in ‘void console::clear(bool)’


Heres what I get when I compile with Geany:
1
2
3
4
5
6
clearscreen.cpp: In function ‘bool console_initialize_terminfo()’:
clearscreen.cpp:13: error: ‘STDOUT_FILENO’ was not declared in this scope
clearscreen.cpp: In function ‘void console::clear(bool)’:
clearscreen.cpp:28: error: default argument given for parameter 1 of ‘void console::clear(bool)’
clearscreen.hpp:9: error: after previous specification in ‘void console::clear(bool)’
Compilation failed.


Wtf?
Not sure how to fix it (since I'm new to C++). Maybe you know?

Last edited on Feb 20, 2008 at 2:22am
Feb 20, 2008 at 3:11am
Ah, sorry. I forgot a header and made a typo.

#include <cstdio>

and fix the the clearscreen.cpp file to prototype the clear() function as:
void console::clear( bool gohome )
(since the header file already gives the default argument value).

I'll fix my post in the other thread.
Feb 20, 2008 at 8:01pm
Now I get with: $ gcc project.cpp clearscreen.cpp -lcurses:
1
2
3
4
clearscreen.cpp: In function ‘bool console_initialize_terminfo()’:
clearscreen.cpp:14: error: ‘STDOUT_FILENO’ was not declared in this scope
clearscreen.cpp: In function ‘bool console_initialize_terminfo()’:
clearscreen.cpp:14: error: ‘STDOUT_FILENO’ was not declared in this scope


And with Geany:
1
2
3
clearscreen.cpp: In function ‘bool console_initialize_terminfo()’:
clearscreen.cpp:14: error: ‘STDOUT_FILENO’ was not declared in this scope
Compilation failed.
Feb 20, 2008 at 9:11pm
Hmm...

Type the following and tell me what you get
1
2
cd /include
grep -rl STDOUT_FILENO *

(That's a lowercase 'L' for the option.)
Last edited on Feb 20, 2008 at 9:12pm
Feb 20, 2008 at 10:55pm
I did not have a /include

So I did this instead:
1
2
cd /usr/include
grep -rl STDOUT_FILENO *


I got:
unistd.h

Wait I might've did it wrong!

1
2
cd /path/to/C++/project
grep -rl STDOUT_FILENO *


I got:
clearscreen.cpp

----------------------------------------------------------------------------------------------------------------------------

Also...
Why aren't you trying this out?
After all this is your code.
Are you just making this stuff up from memory? Or do you actually use this?

Ps. I'm not trying to sound mean I just thought I would like to know.
-----------------------------------------------------------------------------------------------------------------------------
UPDATE:

I included unistd.h in clearscreen.cpp and now I get:

$ gcc project.cpp clearscreen.cpp -lcurses
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/tmp/cczPsQ6R.o: In function `console::cursorxy(int, int)':
clearscreen.cpp:(.text+0x70): multiple definition of `console::cursorxy(int, int)'
/tmp/cc8RRZFM.o:Midpoint.cpp:(.text+0x3f6): first defined here
/tmp/cczPsQ6R.o: In function `console::clear(bool)':
clearscreen.cpp:(.text+0xde): multiple definition of `console::clear(bool)'
/tmp/cc8RRZFM.o:Midpoint.cpp:(.text+0x464): first defined here
/tmp/cc8RRZFM.o: In function `__static_initialization_and_destruction_0(int, int)':
Midpoint.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
/tmp/cc8RRZFM.o: In function `__tcf_0':
Midpoint.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
/tmp/cc8RRZFM.o: In function `get_coord(double&, double&)':
Midpoint.cpp:(.text+0x83): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x88): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x96): undefined reference to `std::cin'
Midpoint.cpp:(.text+0x9b): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(double&)'
Midpoint.cpp:(.text+0xaa): undefined reference to `std::cout'
Midpoint.cpp:(.text+0xaf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0xbd): undefined reference to `std::cin'
Midpoint.cpp:(.text+0xc2): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(double&)'
/tmp/cc8RRZFM.o: In function `main':
Midpoint.cpp:(.text+0xe7): undefined reference to `std::cout'
Midpoint.cpp:(.text+0xec): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0xfa): undefined reference to `std::cin'
Midpoint.cpp:(.text+0xff): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char&)'
Midpoint.cpp:(.text+0x149): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x14e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x16f): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x174): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x1c7): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x1cc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x1dc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x1eb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
Midpoint.cpp:(.text+0x1fb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x20a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
Midpoint.cpp:(.text+0x21a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x232): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x237): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x258): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x25d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x2a8): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x2ad): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x2bd): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x2cc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
Midpoint.cpp:(.text+0x2dc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x2eb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
Midpoint.cpp:(.text+0x2fb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x310): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x315): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x32d): undefined reference to `std::cin'
Midpoint.cpp:(.text+0x332): undefined reference to `std::basic_istream<char, std::char_traits<char> >::ignore(int, int)'
Midpoint.cpp:(.text+0x339): undefined reference to `std::cin'
Midpoint.cpp:(.text+0x33e): undefined reference to `std::basic_istream<char, std::char_traits<char> >::get()'
Midpoint.cpp:(.text+0x360): undefined reference to `std::cin'
Midpoint.cpp:(.text+0x365): undefined reference to `std::basic_istream<char, std::char_traits<char> >::ignore(int, int)'
Midpoint.cpp:(.text+0x36c): undefined reference to `std::cin'
Midpoint.cpp:(.text+0x371): undefined reference to `std::basic_istream<char, std::char_traits<char> >::get()'
/tmp/cc8RRZFM.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
/tmp/cczPsQ6R.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status


Midpoint.cpp is my current project.

All I did was add "#include <unistd.h>" to clearscreen.cpp and it did all that!?

Wow.
Last edited on Feb 20, 2008 at 11:22pm
Feb 20, 2008 at 11:40pm
Yes, it was from memory. Doing this stuff on Windows is a bit different, and that's what I'm working on right now.

I just switched over to my Debian to check my results against yours (which I did before your edit --more on that in a moment). I thought that <stdio.h> declared it, but as you noted it is actually declared in <unistd.h>. Replace #include <cstdio> with:
#include <unistd.h>

I've fixed my other post (again :-/ ).

Don't feel put-upon. I've had you do it for three reasons.

1) I was logged into Windows.

2) When dealing with this stuff all bets are off. _Most_ implementations follow the basic POSIX standards pretty well, but not all, and sometimes things are shuffled around in one include file or another. If the code I supplied needs help I want to know. Likewise, if it works fine I want to know.

3) You need to learn how to figure this stuff out for yourself, and people only learn by doing. So rather than say "argh! your code doesn't work!" you can say "hey, you goofed up _here_ (at least on Joe's Linux kernel 0.9.1.2 with GCC 4.1.3) and this is what I did to fix it."

Finally, please be careful. Your comment was well-intentioned, and please believe me that I understand your point of view as you are newly diving into C++ programming and this weird esoteric stuff is frustrating, but it still comes across as very rude to complain like that. Most geeks will stop responding to you if you bite the hand that is feeding you:

The fact is I've been tweaking this stuff for a couple of weeks now and I am working primarily in Windows ATM to verify that my library routines behave exactly the same as they do in Linux. When done, I will post a tutorial in a place that will get high Google hits, so that less system("cls")s will appear in people's code and neophytes will learn something interesting about something wizardly. Don't assume that your fun compares with any professional programmer's time. The reason people post help is because they want to help, not hinder.

Now, don't feel bad. Go forth, young grasshopper, and kick some terminal butt. (Let me know if it still doesn't work.)
Feb 20, 2008 at 11:45pm
Wow, all that? It looks like something is wrong with your includes. The "multiple definition" indicates that somehow "clearscreen.cpp" is being included somewhere or "clearscreen.hpp" is being included twice somewhere...

All those things about std::something are errors in your code: it looks like it is due to a missing #include <iostream> and/or something else...
Feb 21, 2008 at 12:06am
Heres all my code in my Midpoint program. If I don't include clearscreen.cpp/hpp it will compile and build fine. Once I include them.... Dead.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Find the midpoint of 2 points

#include </usr/include/ncurses.h>
#include "clearscreen.cpp"
#include "clearscreen.hpp"
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cctype>
#include <stdio.h>
using namespace std;
	
void get_coord( double &X, double &Y )
{
    cout << "X: ";
    cin >> X;
    cout << "Y: ";
    cin >> Y;
}

int main() 
{
	char ME;
	bool init;
	double X1, Y1, X2, Y2, MX, MY, EX, EY;
	
	do 
	{
		cout << "Find the Midpoint (or Endpoint) of 2 points.\n\n"
            	"Are you trying to find the Midpoint or Endpoint?\n"
            	"[M/E] ";
		cin >> ME;
	
    	switch (ME) 
    	{	
    		case 'm': case 'M':
    		{
    			init = true;
        		cout << "\nEnter the the first coordinate:\n";
        		get_coord( X1, Y1 );
        		cout << "\nEnter the second coordinate:\n";
        		get_coord( X2, Y2 );
				MX = (X1 + X2) / 2;
				MY = (Y1 + Y2) / 2; 
				cout << "\nMidpoint is: " << "(" << MX << "," << MY << ")";
				break;
			}
	
			case 'e': case 'E':
			{
				init = true;
				cout << "\nEnter the the first coordinate:\n";
       			get_coord( X1, Y1 );
        		cout << "\nEnter the midpoint:\n";
        		get_coord( MX, MY );
				EX = (((-X1 / 2) + MX) * 2);
				EY = (((-Y1 / 2) + MY) * 2);
				cout << "\nEndpoint is: " << "(" << EX << "," << EY << ")"; 
				break;
			}
	
			default:
			{
				init = false;
				cout << "Please enter an M or E!";
				std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
				std::cin.get();
			}
		}
	} while (init == false);
	
	std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
	std::cin.get();
	return 0;
}


And heres everything (important) in the project folder:

Midpoint.cpp
clearscreen.cpp
clearscreen.hpp
readme.txt

And it still outputs the same exact long list of errors. I have no clue why

Thats it. Hope we can work out this problem. I will try my best to help.
Last edited on Feb 21, 2008 at 12:10am
Feb 21, 2008 at 12:40am
Yes, the problem is with your includes. Let me switch back to *nix again... (give me a minute or two)

What version of unix are you using?
Feb 21, 2008 at 1:01am
OK, I'm using Kubuntu (Debian), and I have a hard link: ncurses.h -> curses.h in my /usr/include directory. I assume you looked there and found the ncurses.h file. I will assume you are not on Solaris or some other goofed-up system requiring special help.

The proper way to include the curses.h is
#include <curses.h>

or, if you know that your code uses something specific to NCurses that would fail on the old, broken Curses library, you can
#include <ncurses.h>
to force compilation to fail on really old systems that have not been graced with NCurses.


Now to the problems.
First off, you _do_not_need_ to #include <curses.h> or any variation thereof. Get rid of your first #include.

Second off, _don't_ include .cpp files in anything. (There are times when you can break this rule, but if you don't know what conditions apply, then don't break it.) Get rid of your second #include.

Now compile with:
$ g++ Midpoint.cpp clearscreen.cpp -lcurses
[EDIT] Fixed typo from "gcc" to "g++". gcc is for C, and g++ is for C++... heh :-P [/edit]

I've not ever tried to compile anything with Geany, so I don't know how to make it work there.

After making these changes, your program works fine for me on my system. Let me know if you still get errors.
Last edited on Feb 21, 2008 at 3:41am
Feb 21, 2008 at 2:44am
By the way I am using Ubuntu.
And yes it's still doing it!

The output of "$ gcc Midpoint.cpp clearscreen.cpp -lcurses" is :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/tmp/ccKsQ71J.o: In function `__static_initialization_and_destruction_0(int, int)':
Midpoint.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
/tmp/ccKsQ71J.o: In function `__tcf_0':
Midpoint.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccKsQ71J.o: In function `get_coord(double&, double&)':
Midpoint.cpp:(.text+0x83): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x88): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x96): undefined reference to `std::cin'
Midpoint.cpp:(.text+0x9b): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(double&)'
Midpoint.cpp:(.text+0xaa): undefined reference to `std::cout'
Midpoint.cpp:(.text+0xaf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0xbd): undefined reference to `std::cin'
Midpoint.cpp:(.text+0xc2): undefined reference to `std::basic_istream<char, std::char_traits<char> >::operator>>(double&)'
/tmp/ccKsQ71J.o: In function `main':
Midpoint.cpp:(.text+0xe7): undefined reference to `std::cout'
Midpoint.cpp:(.text+0xec): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0xfa): undefined reference to `std::cin'
Midpoint.cpp:(.text+0xff): undefined reference to `std::basic_istream<char, std::char_traits<char> >& std::operator>><char, std::char_traits<char> >(std::basic_istream<char, std::char_traits<char> >&, char&)'
Midpoint.cpp:(.text+0x149): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x14e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x16f): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x174): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x1c7): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x1cc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x1dc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x1eb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
Midpoint.cpp:(.text+0x1fb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x20a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
Midpoint.cpp:(.text+0x21a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x232): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x237): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x258): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x25d): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x2a8): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x2ad): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x2bd): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x2cc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
Midpoint.cpp:(.text+0x2dc): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x2eb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)'
Midpoint.cpp:(.text+0x2fb): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x310): undefined reference to `std::cout'
Midpoint.cpp:(.text+0x315): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
Midpoint.cpp:(.text+0x32d): undefined reference to `std::cin'
Midpoint.cpp:(.text+0x332): undefined reference to `std::basic_istream<char, std::char_traits<char> >::ignore(int, int)'
Midpoint.cpp:(.text+0x339): undefined reference to `std::cin'
Midpoint.cpp:(.text+0x33e): undefined reference to `std::basic_istream<char, std::char_traits<char> >::get()'
Midpoint.cpp:(.text+0x360): undefined reference to `std::cin'
Midpoint.cpp:(.text+0x365): undefined reference to `std::basic_istream<char, std::char_traits<char> >::ignore(int, int)'
Midpoint.cpp:(.text+0x36c): undefined reference to `std::cin'
Midpoint.cpp:(.text+0x371): undefined reference to `std::basic_istream<char, std::char_traits<char> >::get()'
/tmp/ccKsQ71J.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
/tmp/ccoTxgQj.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status


Maybe there is something wrong with clearscreen.cpp/hpp?

Anyway here is Midpoint.cpp (the includes)
1
2
3
4
5
6
7
8
9
10
#include "clearscreen.hpp"
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cctype>
#include <stdio.h>
#include <cstdio>
#include <unistd.h>
using namespace std;


Here is clearscreen.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// clearscreen.cpp

#include <unistd.h>
#include <term.h>
#include "clearscreen.hpp"

static char *console_clearscreen    = 0;
static char *console_cursorposition = 0;

static bool console_initialize_terminfo()
{
  int result;
  if (cur_term) return true;
  setupterm( NULL, STDOUT_FILENO, &result );
  if (result > 0)
  {
    console_clearscreen    = tigetstr( "clear" );
    console_cursorposition = tigetstr( "cup"   );
  }
  return (result > 0);
}

void console::cursorxy( int col, int line )
{
  if (console_initialize_terminfo())
    putp( tparm( console_cursorposition, line, col, 0, 0, 0, 0, 0, 0, 0 ) );
}

void console::clear( bool gohome )
{
  if (console_initialize_terminfo())
  {
    putp( console_clearscreen );
    if (gohome) cursorxy( 0, 0 );
  }
}

// end clearscreen.cpp 


And here is clearscreen.hpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// clearscreen.hpp

#ifndef CLEARSCREEN_H
#define CLEARSCREEN_H

namespace console {

  void cursorxy( int col, int line );
  void clear( bool gohome = true );

  }

#endif

// end clearscreen.hpp 


I still have no clue whats wrong.
Last edited on Feb 21, 2008 at 2:51am
Feb 21, 2008 at 2:52am
(Sorry for double posting but the last message was too long.)

I even made a basic script that just displays text and it still gives me the long list of errors just like before.

EDIT:
Everything says "undefined reference to..." So is there a way we can reference it?
Last edited on Feb 21, 2008 at 3:03am
Feb 21, 2008 at 3:39am
I was thinking there had to be something wrong with your compiler. I've scoured the net some and came across the obvious answer (by accident). I told you to compile using GCC as
gcc ...

BUT both your program and my library files are C++... (Both of us should have noticed that.)

Compile with
g++ Midpoint.cpp clearscreen.cpp -lcurses

If that doesn't work, make sure you have the latest ncurses-dev package installed and try again.

And _please_ stop telling me that clearscreen.foo is wrong. I've been using this code for years on at least six different systems, and I've been scrutinizing it carefully for the past two weeks. Other than some initial typos there is _nothing_ wrong with it. What remains is compiler errors and binary link mismatches.

Alas, dinking around with C and C++ on linux can be a hard education... (one of the reasons why C and C++ haters hate C and C++). Sorry it has been so rough.
Last edited on Feb 21, 2008 at 3:39am
Feb 21, 2008 at 9:18pm
Thanks a ton man. It's too bad it was right in front of our noses the whole time.

Oh and I'm sorry for offending you about your code Duoas, I just wanted to explore all possible solutions for the problem that I had.

Keep doing what your doing Duoas. You just made C++ better for everyone.

Also...
You should make a new thread on EXACTLY how to do this.
Feb 23, 2008 at 3:21pm
Yeah, it's always the most obvious problems that give the most grief... :-P

Thanks. That's exactly what I am planning... ;-)
Topic archived. No new replies allowed.