首页 > 解决方案 > 是否可以使用在另一个 C++ 库中声明的静态变量

问题描述

我打算在其函数之一中使用带有变量声明的库

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

   /// operation on iwanttouse

}

如何在应用程序 B 中使用它?我是否将其与外部连接

extern int iwanttouse;     

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

或者我可以在没有声明的情况下使用它们?

标签: c++extern

解决方案


不,静态变量具有内部链接,正是因为您不能这样做。


推荐阅读