-3
다른 파일에 포함 :처럼 나는 간단한 프로그램을
extra.h:5:5: error: 'velocity' was not declared in this scope
물론, extra.h
내 코드는 변수 마이에서 "볼 수 없습니다" n.c, 왜 이럴까? 이 문제를 어떻게 해결할 수 있습니까?
다른 파일에 포함 :처럼 나는 간단한 프로그램을
extra.h:5:5: error: 'velocity' was not declared in this scope
물론, extra.h
내 코드는 변수 마이에서 "볼 수 없습니다" n.c, 왜 이럴까? 이 문제를 어떻게 해결할 수 있습니까?
당신은 extra.h
에 다음과 같은 선언을 추가 할 수 있습니다
extern int velocity;
그러나, extra()
가 처음에 extra.h
에 정의 할 수 없습니다. extra.h
이 같은 이진 파일에 여러 개의 .c
파일에 포함되어 있으면 문제가 발생할 수 있습니다. 다음은이 있어야 무엇 :
extra.h
:
void extra();
extra.c
:
#include "extra.h"
static int velocity = 0;
void extra() {
velocity += 1;
}
main.c
:
#include "extra.h"
int main()
{
extra();
return 0;
}
대신 .H 파일에서 속도 변수를 넣어보십시오. – uvr
@uvr 확실히 좋은 습관은 아닙니다. –
http://opackoverflow.com/questions/10422034/when-to-use-extern-in-c – willll