-2

I intend to use a library with a declaration of a variable in one of its function

/// in library A
function fun(){
   static int iwanttouse = 1;

   /// operation on iwanttouse

}

How can i use it in Application B? Do I connect it with extern

extern int iwanttouse;     

// and then use it
if(iwanttouse == x)
    ..... 

or I could use them without declaration?

JaMiT
  • 14,422
  • 4
  • 15
  • 31
user1538798
  • 1,075
  • 3
  • 17
  • 42
  • This variable is probably not seen even in other files of the same library, let alone yours – Alexey S. Larionov Nov 12 '21 at 06:52
  • 3
    You can't do that, static variables are not visible from the outside – n. m. could be an AI Nov 12 '21 at 06:53
  • Please explain what makes your think that this is possible and how far you are prepared to walk the foul trickery path. Would you accept to use a non-portable, unsafe, strictly undefined method, which might fail with any change or update of either your own program or the lib? – Yunnosch Nov 12 '21 at 07:06
  • Are you specifically asking about static variables from libraries? I.e. can you show your solution for using a static variable from one of your own functions? Whatever trickery you use for that, it might be adapted to suit the library situation. – Yunnosch Nov 12 '21 at 07:08
  • 2
    This is [XY problem](https://xyproblem.info/) please explain why do you need this? What problem this should solve? What library is it? – Marek R Nov 12 '21 at 07:13
  • @MarekR: It is just that I have an variable declared globally in one of my library header... i could use it by declaring extern variable name; so I am thinking of the same could be done to a static function variable – user1538798 Nov 12 '21 at 07:18
  • You are still explaining problem Y. Anyway sharing global state between libraries (in fact between anything) is terrible code design and should be avoided. – Marek R Nov 12 '21 at 07:20
  • @MarekR Maybe I have failed to explain my problem properly. I am thinking of this solution and nothing has been done yet. I am soliciting views on whether such methods/functions could be used – user1538798 Nov 12 '21 at 07:26
  • So you want to do bad code design. Do not do it: https://stackoverflow.com/a/485020/1387438 – Marek R Nov 12 '21 at 07:31

3 Answers3

3

No. Static variables have internal linkage, precisely so that you can't do that.

sp2danny
  • 7,488
  • 3
  • 31
  • 53
1

Don't use global variables, they make unit testing of your code next to impossible, use dependency injection instead.

Example:

#include <iostream>

// header file.

// Define a struct with all the data you need globally
struct my_data_t
{
    bool i_want_house = true;
};

// Get a static instance 
my_data_t& getGlobalData()
{
    static my_data_t data;
    return data;
};

// Put code where you want to use data in a class.
class my_class_t
{
public:
    // constructor with dependency injection!
    // this way any data can be injected (valuable for unit testing!)
    explicit my_class_t(my_data_t& data) :
        m_data{ data }
    {
    }

    void show_i_want_house()
    {
        // use data
        if (m_data.i_want_house)
        {
            std::cout << "I really want that house!\n";
        }
        else
        {
            std::cout << "Nah, this house is not good enough\n";
        }
    }

private:
    my_data_t& m_data;

};


// cpp file
int main()
{
    // instantiate objects with a reference to the data you want it to use.
    my_class_t object_with_global_data{ getGlobalData() };
    object_with_global_data.show_i_want_house();

    
    my_data_t test_data{ false };                           //aggregate initialization of struct
    my_class_t object_with_test_data{ test_data };          //create an object with non-global data for testing
    object_with_test_data.show_i_want_house();

    return 0;
}
Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19
1

A variable marked with the keyword static(outside the class) is only visible to that translation unit. static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime.

So in your case, static int iwanttouse = 1; is not even seen by another translation unit. :)

Rid
  • 397
  • 2
  • 12