내 C 코드에서 인라인 함수를 통해 정적 변수를 노출하고 표준 C에서 'extern'특성 인 OOP를 사용하지 마십시오. 그러나 컴파일 할 수 없습니다.인라인 함수를 통해 정적 변수 노출
[[email protected] snippets]$ cat object.h
inline int get_value(int index);
[[email protected] snippets]$ cat object.c
#include "object.h"
static int data_array[32];
inline int get_value(int index) {
return data_array[index];
}
[[email protected] snippets]$ cat app.c
#include "object.h"
void main(void) {
int datum;
datum=get_value(8);
}
[[email protected] snippets]$ gcc -c object.c
object.c:5:9: warning: ‘data_array’ is static but used in inline function ‘get_value’ which is not static
return data_array[index];
^
[[email protected] snippets]$ ls -l object.o
-rw-rw-r-- 1 niko niko 976 Aug 30 15:56 object.o
[[email protected] snippets]$ gcc -c app.c
In file included from app.c:1:0:
object.h:1:12: warning: inline function ‘get_value’ declared but never defined
inline int get_value(int index);
^
[[email protected] snippets]$ ls -l app.o
-rw-rw-r-- 1 niko niko 1368 Aug 30 15:56 app.o
[[email protected] snippets]$ gcc -o app app.o object.o
app.o: In function `main':
app.c:(.text+0xe): undefined reference to `get_value'
collect2: error: ld returned 1 exit status
[[email protected] snippets]$
, 즉이 모든 .c 인 파일에 인라인 될 것 그들이 인라인 함수를 통해 선언 된 파일 외부에서 어떻게 든 정적 변수를 액세스 할 수 있습니다 그것을 사용하고있는 다음 코드를 고려? 물론 난 그냥 변수 '통근'선언 할 수
extern int data_array[];
를하고 직접 참조하지만 함수 호출의 오버 헤드없이 C에서 OOP를 시뮬레이션 기능을 통해 그것을 할 싶어.
C11 표준 초안의 n1570 : 여기에 모두 ISO C와 GNU C와 호환 일부 코드 *은 6.7.4 기능 지정자 3 수정의 정의를 포함하지 않는다 외부 링크와 함수의 인라인 정의 정적 또는 스레드 저장 기간을 가진 객체이며 내부 연결을 가진 식별자에 참조를 포함하지 않아야합니다. * – EOF
@EOF too bad ... 인라인 함수를 통해 C에서 OOP를 어떻게 시뮬레이트합니까? 유일하게 실행 가능한 대안은 매크로 일 것입니다 ... – Nulik
매크로가 어떻게 어떤 식 으로든 도움이 될지 모르겠습니다. 나는 왜 당신이 인라인 인 기능을 주장하는지 잘 모르겠다. – EOF