Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

Java에서는 복잡성 때문에 제거된 기능이지만, C/C++에서는 compile 전에 전처리기(Pre-Processor)가  #include나 #define, 각종 Macro  를 전처리하고 이 기능을 이용하여 조건부로 컴파일이 가능하다.

  (info) gcc 의 전처리기 명령어는 cpp(The C Preprocessor) 이다

사용할 수 있는 macro에는 _FILE, __LINE_ 같이 compiler마다 있는 macro도 있지만 OS, Compiler 종류별로 다양한 macro 들이 존재하며 이를 잘 활용해서 이식성 있는 프로그램 제작이 가능하다.

실제로 GNU의 소스를 보면 다음과 같이 전처리 기능을 이용하여 이식성 있게 작성한 것을 볼 수 있다.

  

 더 보기..
#if HAVE_SYS_SYSCTL_H
# if HAVE_SYS_PARAM_H
#  include <sys/param.h> /* needed for OpenBSD 3.0 */
# endif
# include <sys/sysctl.h>
# ifdef HW_MODEL
#  ifdef HW_MACHINE_ARCH
/* E.g., FreeBSD 4.5, NetBSD 1.5.2 */
#   define UNAME_HARDWARE_PLATFORM HW_MODEL
#   define UNAME_PROCESSOR HW_MACHINE_ARCH
#  else
/* E.g., OpenBSD 3.0 */
#   define UNAME_PROCESSOR HW_MODEL
#  endif
# endif
#endif

#ifdef __APPLE__
# include <mach/machine.h>
# include <mach-o/arch.h>
#endif

 

문제는 이런 각종 OS, Compiler 의존적인 macro는 외우기도 힘들고 어떤 macro가 어떤 기능을 하는지 찾아보기도 힘들었는데 다른 사람들도 같은 문제를 겪었는지 Macro를 잘 정리해 놓은 사이트가 있다.

http://sourceforge.net/apps/mediawiki/predef/index.php?title=Main_Page

아주 오랜만에 C++ 코드를 좀 수정할 일이 있어서 오랜만에 들어가 봤더니 아예 mediawiki 기반으로 웹페이지가 변경이 되었다.

혹시 기존 C/C+소스를 플랫폼/컴파일러에 이식할 일이 생기면 참고하기 좋은 사이트 같다.

 

주요 Macro 정리

Windows

 

 Type MacroDescription   
 Identification _WIN32 Defined for both 32-bit and 64-bit environments  
 Identification _WIN64 Defined for 64-bit environments.  
 Identification __WIN32__ Defined by Borland C++
 
 


Linux

 TypeMacro  Description
 Identification __linux__ 
 Identification linux Obsolete
 Identification __linux Obsolete



MacOS

 

 Type Macro Description 
 Identification macintosh Mac OS 9 
 Identification Macintosh Mac OS 9 
 Identification __APPLE__ & __MACH__ Mac OS X

Defined by GNU C and Intel C++
 


GCC

 

 

 Type Macro Description
 Identification __GNUC__ 
 Version __GNUC__ Version
 Version __GNUC_MINOR__ Revision

Visual C++
  Type Macro Format  Description
 Identification _MSC_VER  
 Version _MSC_VER VVRR VV = Version
RR = Revision
 Version _MSC_FULL_VER VVRRPPPP VV = Version
RR = Revision
PPPP = Patch

From ?

  • No labels