Setting alias for GCC in Windows PowerShell

2019-01-12 11:31发布

问题:

I'm trying to set up a "gcc99" alias in Windows PowerShell which is equal to "gcc -std=C99 -pedantic -Wall". The idea is to use fewer keystrokes to ensure that GCC is running in c99 mode. (I've tried my best to adapt the guidelines in the following post to Windows PowerShell: Setting std=c99 flag in GCC)

When I try to compile after setting up such an alias (exhibit 1 below), I receive an error. As reference, I don't receive this error if I use the extended command to compile (exhibit 2 below). As a test, I tried setting gc99 as an alias for "gcc" (no additional values) and it worked fine (exhibit 3 below). Please ignore the many warnings which I have yet to address in code :)

Any suggestions?

(I'm not sure why my captions aren't showing up for the images below. I'm using the auto-created format for pictures, e.g., for the first image: "Caption" followed by "1: link" on the next line.)

回答1:

Aliases in PowerShell are different from aliases in Unix shells. You can only alias the name of a cmdlet, function or program, not include parameters. Quoting from Get-Help Set-Alias:

NAME
    Set-Alias

SYNOPSIS
    Creates or changes an alias (alternate name) for a cmdlet or other command
    element in the current Windows PowerShell session.


SYNTAX
    Set-Alias [-Name]  [-Value]  [-Description ] [-Force]
    [-Option ] [-PassThru] [-Scope ] [-Confirm]
    [-WhatIf] []


DESCRIPTION
    The Set-Alias cmdlet creates or changes an alias (alternate name) for a
    cmdlet or for a command element, such as a function, a script, a file,
    or other executable. You can also use Set-Alias to reassign a current alias
    to a new command, or to change any of the properties of an alias, such as
    its description. Unless you add the alias to the Windows PowerShell profile,
    the changes to an alias are lost when you exit the session or close Windows
    PowerShell.

What you can do to run an external program with a default set of arguments is to define that default set as an array and run the program like this:

$CARGS = '-std=C99', '-pedantic', '-Wall'
gcc $CARGS -more arguments here ...

As @ChrisN suggested in the comments below, if you want the variable $CARGS pre-defined in all your PowerShell instances, you can add it to a custom PowerShell profile (e.g. %UserProfile%\Documents\Windows­PowerShell\profile.ps1 for your user).