如何声明外部字符指针?

T.T*_*.T. 0 c variables syntax external global-variables

档案1:

static char* const path; //GLOBAL

int main()
{
   path = FunctionReturningPath();
   UsePath()
}
Run Code Online (Sandbox Code Playgroud)

文件2:

extern char* const path; //GLOBAL from file 1

UsePath() //function using global
{
   something = path;
}
Run Code Online (Sandbox Code Playgroud)

(伪)

想在文件2中使用路径.
我在主文件1中定义全局,是使用全局的不良做法吗?

并且不编译:

Compile Error: error LNK2001: unresolved external symbol _path
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏.谢谢.

小智 6

static char* path; //GLOBAL
Run Code Online (Sandbox Code Playgroud)

错误.使其静态意味着它是文件的本地,并且不能使用extern公开.你要:

char* path; //GLOBAL
Run Code Online (Sandbox Code Playgroud)


Dav*_*ter 5

文件范围中的static关键字表示使该变量特定于该编译单元.摆脱它.

  • (Nitpick,忽略如果你(dis?)喜欢)正确的术语是"翻译单位". (5认同)