如何以正确的方式定义字符串?
我正在重新创建诸如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
我会喜欢如何定义字符串并打印它同样,我调用任何其他字符串,但请使用我设置的颜色变量,并使用blue
和white
谢谢!
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您期望
蓝色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"
. ButPWD
is not a string literal; it isgetcwd((NULL), 0)
. Thegetcwd
routine returns a string at run-time. You cannot concatenate it that way. The easiest solution may be to write three separate calls tomy_putstr
, one forBLUE
, one forPWD
, and one forWHITE
. Alternatively, you need to write additional code to concatenate strings.