7273

How to share one static variable with multiple translation unit?

Question:

I want to make an array static and also want to reference it in the other translation unit. Then I define it as static int array[100] = {...}, and declare it in other translation unit as extern int array[]. But the compiler tells me that the storage class of static and extern conflict with each other, how could I pass it and still reach my goal?

Answer1:

Remove the static. Just have the int array[100] = {...}; in one .cpp file, and have extern int array[100]; in the header file.

static in this context means that other translation units can't see it. That obviously conflicts with the extern directive.

Answer2:

static in file scope is pretty much a declare-private directive to the assembler. It is most certainly different than static in class or function scope.

E.g. in zlib, #define LOCAL static is used.

Answer3:

Instead of making the variable global, consider leaving it static and adding public accessors and modifiers to it. It's not a great thing to directly couple to naked variables in other modules.

Recommend

  • SCons: modify intermediate builder calls
  • Ruby on Rails two create actions to two different controllers simultaneously
  • Extract audio frames from AMR-NB file
  • dynamic server time
  • Bootstrap ScrollSpy skips section when no more scrollable area
  • No System image available when adding AVD in Android studio
  • How to get style in Elm
  • TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
  • Objective C - Create a framework for my iphone apps?
  • undefined reference error due to use of static variables [duplicate]
  • How to package a jar and all dependencies within a new jar with maven
  • android-support-v7-appcompat has same attrs as actionbarsherlock library
  • Opaque reference instead of PImpl. Is it possible?
  • JSR-330 support in Picocontainer : @Inject … @Named(\"xxx)
  • Creating a DropDownList
  • Who propagate bugfixes across branches (corporate development)?
  • Compiling dlib on OS X
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • saving file generated by TCPDF
  • How to view images from protected folder with php?
  • Textfile Structure (tables)
  • With Hadoop, can I create a tasktracker on a machine that isn't running a datanode?
  • Spark fat jar to run multiple versions on YARN
  • Scrapy recursive link crawler
  • Regex thinks I'm nesting, but I'm not
  • What is the “return” in scheme?
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Javascript Callbacks with Object constructor
  • vba code to select only visible cells in specific column except heading
  • When should I choose bucket sort over other sorting algorithms?
  • How do you troubleshoot character encoding problems?
  • How to disable jQuery.jplayer autoplay?
  • Understanding cpu registers
  • Java static initializers and reflection
  • unknown Exception android
  • Running Map reduces the dimensions of the matrices
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app