如何以正确的方式定义字符串?

发布于 2025-01-22 03:02:39 字数 1088 浏览 2 评论 0原文

我正在重新创建诸如Shell之类的基本bash,并且使用getCWD来确定我目前要以一种不错的方式打印它的路径。

我的标题文件中有一个:

#define BLUE "\e[1;36m"
#define WHITE "\e[0;00m"
#define PWD getcwd((NULL), 0)
#define PROMPT BLUE PWD WHITE

然后,我尝试使用putStr打印提示,但是当我编译时,我会收到此错误:

cc -g3   -c -o src/minishell.o src/minishell.c
In file included from src/minishell.c:9:
src/minishell.c: In function ‘minishell’:
src/../include/minishell2.h:14:13: error: expected ‘)’ before ‘getcwd’
   14 | #define PWD getcwd((NULL), 0)
      |             ^~~~~~
src/../include/minishell2.h:15:21: note: in expansion of macro ‘PWD’
   15 | #define PROMPT BLUE PWD WHITE
      |                     ^~~
src/minishell.c:36:15: note: in expansion of macro ‘PROMPT’
   36 |     my_putstr(PROMPT);
      |               ^~~~~~
src/minishell.c:36:14: note: to match this ‘(’
   36 |     my_putstr(PROMPT);
      |              ^
make: *** [<builtin>: src/minishell.o] Error 1

我会喜欢如何定义字符串并打印它同样,我调用任何其他字符串,但请使用我设置的颜色变量,并使用bluewhite 谢谢!

I am recreating a basic bash like shell and I use getcwd for determining the path I am currently in to print it in a nice way.

I have this in my header file:

#define BLUE "\e[1;36m"
#define WHITE "\e[0;00m"
#define PWD getcwd((NULL), 0)
#define PROMPT BLUE PWD WHITE

Then, I try to print PROMPT using a putstr but when I compile I get this error:

cc -g3   -c -o src/minishell.o src/minishell.c
In file included from src/minishell.c:9:
src/minishell.c: In function ‘minishell’:
src/../include/minishell2.h:14:13: error: expected ‘)’ before ‘getcwd’
   14 | #define PWD getcwd((NULL), 0)
      |             ^~~~~~
src/../include/minishell2.h:15:21: note: in expansion of macro ‘PWD’
   15 | #define PROMPT BLUE PWD WHITE
      |                     ^~~
src/minishell.c:36:15: note: in expansion of macro ‘PROMPT’
   36 |     my_putstr(PROMPT);
      |               ^~~~~~
src/minishell.c:36:14: note: to match this ‘(’
   36 |     my_putstr(PROMPT);
      |              ^
make: *** [<builtin>: src/minishell.o] Error 1

I would love some help on how I can define a string and print it the same way I call any other string but use the color variables I have set with BLUE and WHITE
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

沒落の蓅哖 2025-01-29 03:02:39

看来您期望蓝色PWD白色会使字符串连接。那将行不通。汇编过程中相邻的字符串文字是串联的; “ ABC”“ DEF”将成为“ ABCDEF”。但是pwd不是字符串文字;它是getCWD((NULL),0)getCWD例程在运行时返回字符串。您不能以这种方式连接它。最简单的解决方案可能是为my_putstr编写三个单独的调用,一个用于blue,一个用于pwd,一个用于White < /代码>。另外,您需要编写其他代码来连接字符串。

It looks like you expect BLUE PWD WHITE to concatenate the strings. That will not work. Adjacent string literals are concatenated during compilation; "abc" "def" will become "abcdef". But PWD is not a string literal; it is getcwd((NULL), 0). The getcwd routine returns a string at run-time. You cannot concatenate it that way. The easiest solution may be to write three separate calls to my_putstr, one for BLUE, one for PWD, and one for WHITE. Alternatively, you need to write additional code to concatenate strings.

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