gcc unix环境变量

发布于 2024-12-29 19:03:35 字数 205 浏览 0 评论 0原文

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 type
gcc $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄离 2025-01-05 19:03:35

如果您使用 Cygwin,那么您就是在 Windows 上的 Unix 仿真中运行。相当不错,但仍然是一个模拟。但是,您也可能使用 bash 作为您的 shell,这很好(如果您有机会使用 Korn shell,我下面所说的同样适用,并且如果您使用 /bin/sh,这实际上是 bash 的子集,我所说的也适用于它)。

请注意,您可以在 shell 中定义不在 shell 环境中的变量。在运行其他脚本时,这种区别很重要。

src="binds.c engine.c console.c"

这将创建一个名为 src 的非环境变量。您现在可以在命令行中使用它:

echo $src
gcc -c $src

echo 命令将列出三个名称,GCC 将运行并尝试编译所有三个源文件。但是,如果您运行 env 命令,您将不会在环境中看到 src。另一方面,如果运行 set 命令,您将看到 src

env    # No arguments - list the environment variables that are exported
set    # No arguments - lists variables, exported and non-exported

现在假设您在当前目录中有(或创建)一个名为 compile 的脚本,其中包含:

gcc -c $src

该脚本应该是可执行的 (chmod +xcompile)。如果运行:

./compile

脚本看不到变量 $src 因为它未导出,因此您会收到有关 GCC 未指定文件名的错误。有趣的是,如果您运行其中任何一个:

. ./compile
source ./compile

那么编译就会按照您的意愿进行。这是因为当前 shell 不是启动子 shell 来执行脚本,而是读取脚本,并定义了变量 src 。

如果您现在导出 src,它将可供子 shell 和其他程序使用:

export src     # Sufficient given the initial assignment
export src="binds.c engine.c console.c" # Set and export; not portable to Bourne shell

现在,如果您运行 env,您将看到列出的 src。现在,如果您运行 ./compile,它将在其环境中有一个变量 src,因此编译将按预期进行。

所以,关键点是:

  • 并非所有 shell 变量都是环境变量。
  • 要将变量设置为环境变量,您必须显式地导出它。

还有一些其他技巧可以通过环境变量来实现,但它们与您的情况并没有密切关系,并且更可能会造成混乱而不是帮助。 (对于那些知道的人,我正在考虑 set -kvar=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 of bash 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.

src="binds.c engine.c console.c"

This creates a non-environment variable called src. You can now use this at the command line:

echo $src
gcc -c $src

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 the env command, you will not see src in the environment. On the other hand, if you run the set command, you will see src.

env    # No arguments - list the environment variables that are exported
set    # No arguments - lists variables, exported and non-exported

Now suppose that you have (or create) a script called compile in the current directory that contains:

gcc -c $src

The script should be executable (chmod +x compile). If you run:

./compile

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:

. ./compile
source ./compile

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:

export src     # Sufficient given the initial assignment
export src="binds.c engine.c console.c" # Set and export; not portable to Bourne shell

Now if you run env, you will see src listed. And now if you run ./compile, it will have a variable src in its environment, so the compilation will occur as expected.

So, the key points are:

  • Not all shell variables are environment variables.
  • To make a variable into an environment variable, you must 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 and var=value cmd arg1 ... for those who know.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文