标头中共享的c常量 [英] shared c constants in a header
问题描述
我想在多个C文件中共享某些C字符串常量。常量跨越多行以提高可读性:
I want to share certain C string constants across multiple c files. The constants span multiple lines for readability:
const char *QUERY = "SELECT a,b,c "
"FROM table...";
执行上述操作会重新定义QUERY。我不希望使用宏,因为每行之后都需要使用空格'\'。我可以在单独的c文件中定义它们,并在h文件中替换变量,但是我觉得很懒。
Doing above gives redefinition error for QUERY. I don't want to use macro as backspace '\' will be required after every line. I could define these in separate c file and extern the variables in h file but I feel lazy to do that.
还有其他方法可以在C语言中实现吗?
Is there any other way to achieve this in C?
推荐答案
在某些.c文件中,写下您所写的内容。
在适当的.h文件中,写
In some .c file, write what you've written. In the appropriate .h file, write
extern const char* QUERY; //just declaration
在需要常量的任何地方包括.h文件
Include the .h file wherever you need the constant
没有其他好的方法:)
HTH
No other good way :) HTH
这篇关于标头中共享的c常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!