游.程 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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题