Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2004-04

Conditional compilation ELSE-IF?

9 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alan Mead

I have the following code in a function:

      {$IFDEF LINUX}
      Slash := '/';
      {$ENDIF}
      {$IFDEF WIN32}
      Slash := '\';
      {$ENDIF}

Obviously, FPC supports many other platforms. Is there a better way to handle this? Also, will this work if I ever cross-compile? Is there a way to detect this at run-time rather than compile time?

-Alan

__________________________________
Do you Yahoo!?
Yahoo! Small Business $15K Web Design Giveaway
http://promotions.yahoo.com/design_giveaway/

Peter Vreman
to Alan Mead
Show quoted text 12 lines
I have the following code in a function: {$IFDEF LINUX} Slash := '/'; {$ENDIF} {$IFDEF WIN32} Slash := '\'; {$ENDIF} Obviously, FPC supports many other platforms. Is there a better way to handle this? Also, will this work if I ever cross-compile? Is there a way to detect this at run-time rather than compile time?
{$if defined(linux)}
  Slash:='/'
{$elseif defined(win32)}
  Slash:='\'
{$elseif defined(macos)}
  Slash:=':'
{$else}
  {$error Unsupported target}
{$endif}

Better is to use PathSeparator from the System unit

Michalis Kamburelis
to Alan Mead: I have the following code in a function: {$IFDEF LINUX} Slash := '/'; {$ENDIF} {$IFDEF…
Alan Mead wrote 11 lines
I have the following code in a function: {$IFDEF LINUX} Slash := '/'; {$ENDIF} {$IFDEF WIN32} Slash := '\'; {$ENDIF} Obviously, FPC supports many other platforms. Is there a better way to handle this?

Use DirectorySeparator constant from System unit or PathDelim from SysUtils unit. PathDelim is also Delphi-compatible.

Also, will this work if I ever cross-compile?

Those are just constants defined in some units. So they will always correspond to the target OS you are compiling to. So everything will work when you cross-compile.

Show quoted text 5 lines
Is there a way to detect this at run-time rather than compile time? -Alan

Maybe I don't understand this question, but there's just no need to detect value for DirectorySeparator at run-time when this is already known at compile-time for all OSes.

Michalis.

Marco van de Voort
to Alan Mead: I have the following code in a function: {$IFDEF LINUX} Slash := '/'; {$ENDIF} {$IFDEF…
I have the following code in a function: {$IFDEF LINUX}

Should be {$ifdef unix}

Show quoted text 8 lines
Slash := '/'; {$ENDIF} {$IFDEF WIN32} Slash := '\'; {$ENDIF} Obviously, FPC supports many other platforms. Is there a better way to handle this?

See other answers, but for the case it is not alraedy predefined, yet it works, also for crosscompile. However you should hardly ever use {$if(n)def linux}, _always_ unix.

The only exeception are really 100% linux only things which are relatively rare.

there a way to detect this at run-time rather than compile time?

Yes, but not OS independant. Each OS has its own api for such things :-)

Alan Mead
to Peter Vreman: {$if defined(linux)} Slash:='/' {$elseif defined(win32)} Slash:='\' {$elseif…
--- Peter Vreman wrote 11 lines
{$if defined(linux)} Slash:='/' {$elseif defined(win32)} Slash:='\' {$elseif defined(macos)} Slash:=':' {$else} {$error Unsupported target} {$endif} Better is to use PathSeparator from the System unit

Thanks to everyone who responded.

I just re-checked the docs (which are marked as current for 1.9; I'm using the 1.0.10 build) and they don't list an $ELSEIF which is consistent with the warning I get when I compile your code:

verify.pas(50,2) Warning: Illegal compiler directive $ELSEIF

But your pointer to the system unit was very helpful; I'll use system.DirectorySeparator, as you suggested.

Thanks again.

-Alan

__________________________________
Do you Yahoo!?
Yahoo! Small Business $15K Web Design Giveaway
http://promotions.yahoo.com/design_giveaway/

Peter Vreman
to Alan Mead
Show quoted text 23 lines
--- Peter Vreman wrote: > {$if defined(linux)} > Slash:='/' > {$elseif defined(win32)} > Slash:='\' > {$elseif defined(macos)} > Slash:=':' > {$else} > {$error Unsupported target} > {$endif} > > Better is to use PathSeparator from the System unit Thanks to everyone who responded. I just re-checked the docs (which are marked as current for 1.9; I'm using the 1.0.10 build) and they don't list an $ELSEIF which is consistent with the warning I get when I compile your code: verify.pas(50,2) Warning: Illegal compiler directive $ELSEIF But your pointer to the system unit was very helpful; I'll use system.DirectorySeparator, as you suggested.

It is only available in the current 1.9.3 development version.

Tomas Hajny
to Peter Vreman

From: "Peter Vreman"
To:
Subject: Re: [fpc-pascal]Conditional compilation ELSE-IF? Date sent: Thu, 8 Apr 2004 17:30:10 +0200

.
.

Show quoted text 6 lines
> But your pointer to the system unit was very helpful; > I'll use > system.DirectorySeparator, as you suggested. It is only available in the current 1.9.3 development version.

No, it's already available in 1.0.10, although not documented - just try it.

Tomas

Peter Vreman
to Tomas Hajny
Show quoted text 9 lines
> > But your pointer to the system unit was very helpful; > > I'll use > > system.DirectorySeparator, as you suggested. > > It is only available in the current 1.9.3 development > version. No, it's already available in 1.0.10, although not documented - just try it.

But $ELSEIF is only available in 1.9.3

Olle Raab
to Michalis Kamburelis: Use DirectorySeparator constant from System unit or PathDelim from SysUtils unit.…

04-04-08 15.49, skrev Michalis Kamburelis följande:

Show quoted text 10 lines
Alan Mead wrote: > Is > there a way to detect this at run-time rather than compile time? > > -Alan > Maybe I don't understand this question, but there's just no need to detect value for DirectorySeparator at run-time when this is already known at compile-time for all OSes.

It might if you construct some cross platform tool, e g fpc itself can create link scripts (with -st option) which is to be run on target platform. In that script paths intended for the target filesystem is recorded.

But there is no support for this in the rtl, it is handled internally in the compiler.

Olle