개발공부/C++
전처리기의 활용
dyson_ok
2019. 10. 21. 11:44
#include <iostream>
#include <algorithm>
using namespace std;
#defing LIKE_APPLE
int main()
{
#ifdef LIKE_APPLE //정의가 되어있으면!
cout << "Apple " << endl;
#endif
#ifndef LIKE_APPLE
cout << "Orange " << endl;
#endif
return 0;
}
Apple
#include <iostream>
#include <algorithm>
using namespace std;
//#defing LIKE_APPLE
int main()
{
#ifdef LIKE_APPLE
cout << "Apple " << endl;
#endif
#ifndef LIKE_APPLE //정의가 되어있지 않으면!
cout << "Orange " << endl;
#endif
return 0;
}
Orange
전처리기를 활용하면 위와 같이 분기로 나누어, 빌드할때 내용을 결정하게 할 수 있다.
멀티플랫폼개발에 많이 사용하게 된다.(빌드할때 윈도우인지 리눅스인지 알고 시작하게 하기 위함)
'conditional compilation'이라고도 한다. 전처리로 조건을 만족시킨 후에 컴파일한다는 뜻!
define의 효력범위도 잘 신경써서 코딩하는 것이 좋다.