在Windows PowerShell中设置GCC的别名

时间:2014-10-06 17:00:29

标签: c powershell gcc alias c99

我试图建立一个" gcc99" Windows PowerShell中的别名等于" gcc -std = C99 -pedantic -Wall"。我们的想法是使用更少的击键来确保GCC以c99模式运行。 (我已尽力使以下帖子中的指南适应Windows PowerShell: Setting std=c99 flag in GCC

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

有什么建议吗?

(我不确定为什么我的字幕不会显示在下面的图片中。我使用自动创建的图片格式,例如,第一张图片:&#34 ; 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

1 个答案:

答案 0 :(得分:6)

PowerShell中的别名与Unix shell中的别名不同。您只能为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你的用户)。