笑故挽风 2009-09-16 14:08 采纳率: 100%
浏览 192

如何使用 extern 来共享源文件之间的变量?

I know that global variables in C sometimes have the extern keyword. What is an extern variable? What is the declaration like? What is its scope?

This is related to sharing variables across source files, but how does that work precisely? Where do I use extern?

转载于:https://stackoverflow.com/questions/1433204/how-do-i-use-extern-to-share-variables-between-source-files

  • 写回答

15条回答 默认 最新

  • 10.24 2009-09-16 14:11
    关注

    Extern is the keyword you use to declare that the variable itself resides in another translation unit.

    So you can decide to use a variable in a translation unit and then access it from another one, then in the second one you declare it as extern and the symbol will be resolved by the linker.

    If you don't declare it as extern you'll get 2 variables named the same but not related at all, and an error of multiple definitions of the variable.

    评论

报告相同问题?