[問題] C static variable in shared library
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
GCC
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
無
問題(Question):
在library中宣告static variable StaticInt
在main.c中用staticInt++沒有效果
預期的正確結果(Expected Output):
1
2
3
4
錯誤結果(Wrong Output):
1
1
1
1
程式碼(Code):(請善用置底文網頁, 記得排版)
// variable.h
#include<stdio.h>
static int staticInt = 1;
void printStatic();
-----------------------------
// variable.c
#incdlue<stdio.h>
#include "variable.h"
void printStatic(){
printf("staticInt = %d\n",staticInt);
return;
}
------------------------------
// main.c
#include<stdio.h>
#include "variable.h"
int main(){
printStatic(); staticInt++;
printStatic(); staticInt++;
printStatic(); staticInt++;
printStatic();
return 0;
}
麻煩各位大大
幫我看一下為什麼staticInt++ 不會加到staticInt
我的想法裡 staticInt 的scope是宣告在library裡的
所以scope應該在main中可以access到
請問問題再哪~
謝謝各位!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.90.49
→
07/02 13:20, , 1F
07/02 13:20, 1F
→
07/02 13:21, , 2F
07/02 13:21, 2F
推
07/02 15:05, , 3F
07/02 15:05, 3F
推
07/02 15:16, , 4F
07/02 15:16, 4F
→
07/02 16:43, , 5F
07/02 16:43, 5F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章