필자는 전에 여러 번 포함 된 가드를 사용해 왔지만 실제로 작동하는 방법이나 이유를 결코 이해하지 못했습니다.C++ 포함 가드가 작동하지 않는 것으로 보입니까?
왜 다음 작동하지 않습니까?
#ifndef CAMERA_CLASS_HPP
#define CAMERA_CLASS_HPP
class camera_class
{
....
};
camera_class glcam = camera_class();
#endif // CAMERA_CLASS_HPP
오류는 이것이다 :, 또한
-------------- Build: Debug in System ---------------
Linking console executable: bin/Debug/System
/usr/bin/ld: error: obj/Debug/main.o: multiple definition of 'glcam'
/usr/bin/ld: obj/Debug/camera_class.o: previous definition here
/usr/bin/ld: error: obj/Debug/main.glfunc.o: multiple definition of 'glcam'
/usr/bin/ld: obj/Debug/camera_class.o: previous definition here
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 0 seconds)
0 errors, 0 warnings
(! 당신은 아마이 질문의 제목부터가 될 것 무엇을 추측 할 수있다) 사람이 왜 헤더 가드 작품 나에게 설명해주십시오 수 ?
이해. 헤더 파일이 동일한 소스 파일 *에 두 번 포함되지 않도록합니다. 그들은 헤더가 다른 소스 파일에 두 번 포함되지 않도록하지도 못합니다. – john
감사합니다 존, 나는 그 근본적인 차이를 이해하지 못했습니다! – user3728501