[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

compile C programs for both sunstudio and Visual studio

happytoday

7/18/2011 3:50:00 AM

I comile C programs for bothe platforms : sunstudio using cc , visual
studio 6 . Is there a command to differeniate which platform I compile
for . That in order to use the the system command .

For example :

If (unixplatform)
system("clear");
else
system("cls")
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
25 Answers

Ben Bacarisse

7/18/2011 11:34:00 AM

0

happytoday <ehabaziz2001@gmail.com> writes:

> I comile C programs for bothe platforms : sunstudio using cc , visual
> studio 6 . Is there a command to differeniate which platform I compile
> for . That in order to use the the system command .
>
> For example :
>
> If (unixplatform)
> system("clear");
> else
> system("cls")

Every compiler defines one or more macros that can be used to determine
when you are using it. Check the documentation, but I think Sun
compilers define __sun so you can write:

#ifdef __sun
system("clear);
#else
system("cls");
#endif

Almost all "console programs" run in a window (or maybe even real
hardware) that understands ANSI control sequences. If all you want to
do is a few simple operations like clearing the screen, you could write
short functions that produce the correct sequence. For example,

void screen_clear(void) { puts("\x1b[H\x1b[2J"); }

moves the cursor to the top of the screen and the clears it.

Even if you decide to use conditional compilation, it's a good idea to
put all such system dependencies into functions that are defined away
from the rest of the code:

void screen_clear(void)
{
#ifdef __sun
system("clear);
#else
system("cls");
#endif
}

--
Ben.

Dave \Crash\ Dummy

7/18/2011 1:34:00 PM

0


"Ben Bacarisse" <ben.usenet@bsb.me.uk> schrieb im Newsbeitrag
news:0.dac296d5b2e0c6dcc860.20110718123404BST.87ei1nesb7.fsf@bsb.me.uk...
....
> Almost all "console programs" run in a window (or maybe even real
> hardware) that understands ANSI control sequences. If all you want to
> do is a few simple operations like clearing the screen, you could write
> short functions that produce the correct sequence. For example,
>
> void screen_clear(void) { puts("\x1b[H\x1b[2J"); }
>
> moves the cursor to the top of the screen and the clears it.
....
The windows console does not natively understand ANSI control sequences. You
would explicitely have to load the ANSI driver. And I was told not to use
hardcoded ANSI control sequences on Unix either.

my code for clearing the screen is the following (working with Turbo C 2.0,
Borland C++Builder 5, MSVC, Linux, SCO Unix, SINIX). clear() is defined on
Unix in curses.h . clrscr() is defined on Turbo C in conio.h , but on
Borland C++Builder clrscr() does not clear more then 44 lines (a Windows
console may have more).

#if defined(sinix) || defined(SNI) || defined(M_UNIX) ||
defined(linux)
#define Unix
#endif

#ifdef Unix
#include <termios.h>
#include <curses.h>
#else
#include <conio.h>
#endif

#ifdef __TURBOC__
#if __TURBOC__ <= 0x0200
#define clear clrscr
#else
#define ReplaceConio
#include <windows.h>
#endif
#endif

#ifdef MSDOS
#define clear() _clearscreen(_GCLEARSCREEN)
#endif

#ifdef _MSC_VER
#define ReplaceConio
#include <windows.h>
#include <wincon.h>
#endif

#ifdef ReplaceConio
void clear(void)
{
COORD coordScreen;
DWORD cCharsWritten,
dwConSize;
HANDLE hConsole;
CONSOLE_SCREEN_BUFFER_INFO csbi;

coordScreen.X = 0;
coordScreen.Y = 0;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, ' ', dwConSize,
coordScreen, &cCharsWritten);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConSize, coordScreen,
&cCharsWritten);
SetConsoleCursorPosition(
GetStdHandle(STD_OUTPUT_HANDLE), coordScreen);
}
#endif

Ben Bacarisse

7/18/2011 1:53:00 PM

0

"Heinrich Wolf" <invalid@invalid.invalid> writes:

> "Ben Bacarisse" <ben.usenet@bsb.me.uk> schrieb im Newsbeitrag
> news:0.dac296d5b2e0c6dcc860.20110718123404BST.87ei1nesb7.fsf@bsb.me.uk...
> ...
>> Almost all "console programs" run in a window (or maybe even real
>> hardware) that understands ANSI control sequences. If all you want to
>> do is a few simple operations like clearing the screen, you could write
>> short functions that produce the correct sequence. For example,
>>
>> void screen_clear(void) { puts("\x1b[H\x1b[2J"); }
>>
>> moves the cursor to the top of the screen and the clears it.
> ...
> The windows console does not natively understand ANSI control
> sequences. You would explicitely have to load the ANSI driver.

You are quite right -- I'd forgotten that.

> And I
> was told not to use hardcoded ANSI control sequences on Unix either.

Also true (and very much so when I started out) but there are ever fewer
cases where simple ANSI sequences won't work. It's just possible that
with a generous reading of "almost all" and a few corrupt statisticians
in my employ I could still justify my original statement, but I should
give it up!

As your code shows, there is no simple portable solution.

<snip>
--
Ben.

Virchanza

7/18/2011 2:32:00 PM

0

On Jul 18, 2:53 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:

> As your code shows, there is no simple portable solution.

I went all-out with a program of a mine and used the cross-platform
"ncurses" library:

http://en.wikipedia.org/wi...

Ben Bacarisse

7/18/2011 2:39:00 PM

0

Virchanza <virtual@lavabit.com> writes:

> On Jul 18, 2:53 pm, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>
>> As your code shows, there is no simple portable solution.
>
> I went all-out with a program of a mine and used the cross-platform
> "ncurses" library:
>
> http://en.wikipedia.org/wi...

How much functionality does ncurses give you on a Windows console when
there is no ANSI driver loaded?

--
Ben.

Kenneth Brody

7/18/2011 6:06:00 PM

0

On 7/18/2011 7:34 AM, Ben Bacarisse wrote:
[...]
> Almost all "console programs" run in a window (or maybe even real
> hardware) that understands ANSI control sequences. If all you want to
> do is a few simple operations like clearing the screen, you could write
> short functions that produce the correct sequence. For example,
>
> void screen_clear(void) { puts("\x1b[H\x1b[2J"); }
>
> moves the cursor to the top of the screen and the clears it.
[...]

That only works on a terminal, or within a terminal/console window, that
understands ANSI control sequences. This has not been the case on Windows
for many years, and is not necessarily true on many terminal emulators. The
above program gives this output in a Windows console window:

�[H�[2J

I don't know how that will be shown after NNTP gets through it it, but it's:

(left arrow)(left bracket)(capital H)(left arrow)(left bracket)
(two)(capital J)

That's because sending ESC to stdout generates a little left-arrow character.

--
Kenneth Brody

Kenneth Brody

7/18/2011 6:09:00 PM

0

On 7/18/2011 10:38 AM, Ben Bacarisse wrote:
> Virchanza<virtual@lavabit.com> writes:
>
>> On Jul 18, 2:53 pm, Ben Bacarisse<ben.use...@bsb.me.uk> wrote:
>>
>>> As your code shows, there is no simple portable solution.
>>
>> I went all-out with a program of a mine and used the cross-platform
>> "ncurses" library:
>>
>> http://en.wikipedia.org/wi...
>
> How much functionality does ncurses give you on a Windows console when
> there is no ANSI driver loaded?

I can't say, not having touched ncurses for years. However, I can tell you
that there is no need for any ANSI driver, as there is an entire Windows
Console API available to handle such things.

--
Kenneth Brody

Keith Thompson

7/18/2011 6:12:00 PM

0

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
[..]
> Almost all "console programs" run in a window (or maybe even real
> hardware) that understands ANSI control sequences. If all you want to
> do is a few simple operations like clearing the screen, you could write
> short functions that produce the correct sequence. For example,
>
> void screen_clear(void) { puts("\x1b[H\x1b[2J"); }
>
> moves the cursor to the top of the screen and the clears it.

And then moves the cursor down one line. puts() prints a '\n'
after printing its argument string.

Better:

fputs("\x1b[H\x1b[2J", stdout);
fflush(stdout);

Of course that's still non-portable, for various reasons that have
been discussed downthread.

But I suggest that the OP should ask himself whether he really
should be clearing the screen in the first place.

For a text editor or some other program that needs to take control
of the entire screen, clearing the screen is certainly a sensible
thing to do, but such a program is probably going to be using curses
or some similar package anyway.

For a program that just reads from stdin (assumed to be a keyboard)
and writes to stdout (assumed to be a screen of some sort),
clearing the screen can erase information that might be of some
value to the user. Consider carefully whether your desire to have
a clean-looking display outweighs your user's desire to keep his
own information on the screen. (Of course, if you're going to be
the only user of the program, do whatever you like.)

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Ben Bacarisse

7/18/2011 6:18:00 PM

0

Kenneth Brody <kenbrody@spamcop.net> writes:

> On 7/18/2011 10:38 AM, Ben Bacarisse wrote:
>> Virchanza<virtual@lavabit.com> writes:
>>
>>> On Jul 18, 2:53 pm, Ben Bacarisse<ben.use...@bsb.me.uk> wrote:
>>>
>>>> As your code shows, there is no simple portable solution.
>>>
>>> I went all-out with a program of a mine and used the cross-platform
>>> "ncurses" library:
>>>
>>> http://en.wikipedia.org/wi...
>>
>> How much functionality does ncurses give you on a Windows console when
>> there is no ANSI driver loaded?
>
> I can't say, not having touched ncurses for years. However, I can
> tell you that there is no need for any ANSI driver, as there is an
> entire Windows Console API available to handle such things.

Yes. I suppose I was asking, rather indirectly, does ncurses use it?

--
Ben.

Geoff

7/18/2011 11:07:00 PM

0

On Sun, 17 Jul 2011 22:49:38 -0500 (CDT), happytoday
<ehabaziz2001@gmail.com> wrote:

>I comile C programs for bothe platforms : sunstudio using cc , visual
>studio 6 . Is there a command to differeniate which platform I compile
>for . That in order to use the the system command .
>
>For example :
>
>If (unixplatform)
>system("clear");
>else
>system("cls")

The Microsoft predefined macros _WIN32 and _MSC_VER come to mind,
recently _WIN64.

http://msdn.microsoft.com/en-us/librar...(v=VS.60).aspx

http://msdn.microsoft.com/en-us/librar...

I suppose you could set unixplatform true by default and set it false
depending on the return from a conditionally-compiled GetVersionEx
call.

http://support.microsoft.com...
--
comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.