gcc unix环境变量
src="binds.c engine.c console.c"
echo $src
gcc $src
上面的批处理文件适用于 echo,但 gcc 失败。如果我输入 在命令行中使用 gcc $src
可以正常工作。所以看来 gcc 没有在批处理文件中获取环境局部变量?
使用CYGWIN。
src="binds.c engine.c console.c"
echo $src
gcc $src
The batch file above works with the echo but the gcc fails. If I typegcc $src
at the command line, that works. So it seems that gcc doesn't get environment local variables within a batch file?
Using CYGWIN.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 Cygwin,那么您就是在 Windows 上的 Unix 仿真中运行。相当不错,但仍然是一个模拟。但是,您也可能使用
bash
作为您的 shell,这很好(如果您有机会使用 Korn shell,我下面所说的同样适用,并且如果您使用/bin/sh
,这实际上是 bash 的子集,我所说的也适用于它)。请注意,您可以在 shell 中定义不在 shell 环境中的变量。在运行其他脚本时,这种区别很重要。
这将创建一个名为 src 的非环境变量。您现在可以在命令行中使用它:
echo
命令将列出三个名称,GCC 将运行并尝试编译所有三个源文件。但是,如果您运行env
命令,您将不会在环境中看到src
。另一方面,如果运行set
命令,您将看到src
。现在假设您在当前目录中有(或创建)一个名为
compile
的脚本,其中包含:该脚本应该是可执行的 (
chmod +xcompile
)。如果运行:脚本看不到变量
$src
因为它未导出,因此您会收到有关 GCC 未指定文件名的错误。有趣的是,如果您运行其中任何一个:那么编译就会按照您的意愿进行。这是因为当前 shell 不是启动子 shell 来执行脚本,而是读取脚本,并定义了变量 src 。
如果您现在导出
src
,它将可供子 shell 和其他程序使用:现在,如果您运行
env
,您将看到列出的src
。现在,如果您运行./compile
,它将在其环境中有一个变量src
,因此编译将按预期进行。所以,关键点是:
导出
它。还有一些其他技巧可以通过环境变量来实现,但它们与您的情况并没有密切关系,并且更可能会造成混乱而不是帮助。 (对于那些知道的人,我正在考虑
set -k
和var=value cmd arg1 ...
。)If you're using Cygwin, then you're running on Windows in a Unix-emulation. A pretty good one, but still an emulation. However, you are also probably using
bash
as your shell, which is good (and if by any chance you're using Korn shell, what I say below applies equally, and if you're using/bin/sh
, that is effectively a subset ofbash
and what I say applies to that, too).Note that you can define variables in your shell that are not in the shell's environment. This distinction is important when it comes to running other scripts.
This creates a non-environment variable called
src
. You can now use this at the command line:The
echo
command will list the three names, and GCC will be run and will attempt to compile all three source files. However, if you run theenv
command, you will not seesrc
in the environment. On the other hand, if you run theset
command, you will seesrc
.Now suppose that you have (or create) a script called
compile
in the current directory that contains:The script should be executable (
chmod +x compile
). If you run:the script does not see the variable
$src
because it is not exported, so you get an error about no file names specified from GCC. Funnily enough, if you run either of these:then the compilation occurs as you wanted. That's because instead of starting a sub-shell to execute the script, the current shell reads the script, and the variable
src
is defined.If you now export
src
, it becomes available to sub-shells and other programs:Now if you run
env
, you will seesrc
listed. And now if you run./compile
, it will have a variablesrc
in its environment, so the compilation will occur as expected.So, the key points are:
export
it explicitly.There are some other tricks that can be pulled with environment variables, but they are not yet germane to your situation and would more likely confuse than help. (I'm thinking of
set -k
andvar=value cmd arg1 ...
for those who know.)