叼花硬汉 2009-09-16 14:08 采纳率: 0%
浏览 828

如何使用 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

  • 写回答

14条回答 默认 最新

  • Didn"t forge 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.

    评论

报告相同问题?

悬赏问题

  • ¥15 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题
  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao