2014-10-06 86 views
2

我试图在Windows PowerShell中设置“gcc -std = C99 -pedantic-Wall”的“gcc99”别名。这个想法是使用较少的击键来确保GCC在c99模式下运行。 (我已经尽我所能在下面的帖子Windows PowerShell来适应准则:Setting std=c99 flag in GCC在Windows PowerShell中为GCC设置别名

当我尝试建立这样一个别名(下图1)后进行编译,我收到一个错误。作为参考,如果我使用扩展命令进行编译,则不会收到此错误(下图2)。作为一个测试,我尝试将gc99设置为“gcc”的别名(没有附加值),它工作正常(下面的展示3)。请忽略我尚未解决的许多警告:)

有什么建议吗?我不知道为什么我的字幕没有出现在下面的图片中,我使用了自动创建的图片格式,例如,对于第一张图片:“Caption”,后面跟着“1:链接”下一行。)

Exhibit 1: Error when trying to compile via a "gcc99" alias which is set to "gcc -std=c99 -pedantic -Wall"

Exhibit 2: It works fine when compile directly via "gcc -std=c99 -pedantic -Wall"

Exhibit 3: It works fine when compiling via a "gcc99" alias which is set to "gcc", which makes me think that I'm incorrectly setting the additional values in Exhibit 1

+3

您无法创建运行带参数的命令的别名,您需要创建一个函数。请参阅http://stackoverflow.com/questions/4166370/how-can-i-write-a-powershell-alias-with-arguments-in-the-middle – nos 2014-10-06 17:17:22

+0

可能的重复[如何使用参数编写PowerShell别名在中间?](https://stackoverflow.com/questions/4166370/how-can-i-write-a-powershell-alias-with-arguments-in-the-middle) – 2017-09-22 19:21:25

回答

6

别名在PowerShell中从在Unix的别名不同炮弹。您只能将cmdlet,函数或程序的名称别名,而不能包含参数。从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.

你可以做什么用的参数的默认设置运行外部程序是定义默认设置为一个数组,像这样运行程序:

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

由于@ChrisN在下面的评论中建议,如果您想要在所有PowerShell实例中预先定义变量$CARGS,则可以将其添加到custom PowerShell profile(例如,%UserProfile%\Documents\Windows­PowerShell\profile.ps1为您的用户)。

+0

谢谢!你知道每次打开一个新的Powershell窗口时是否需要重新加载数组(/函数/导入的模块)? – iceman 2014-10-06 17:35:20

+1

他们这样做 - 但您可以将它们添加到PowerShell配置文件,该文件可以是系统,用户或特定于shell的配置文件。 – 2014-10-06 18:22:48