游.程 2009-09-16 14:08 采纳率: 0%
浏览 694

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

  • lrony* 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 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题