C++에서 #if 질문입니다.

kknd345의 이미지

C++에서

#if 조건
//내용
#endif

 
 
여기서 #if 조건에 defined SEL 이런거 말고
변수나 이전에 define 된 문자열의 값을 살피는게 가능한가요?
 
#define aa 3
#if aa==3   // 이런거;; 아님 비슷하게라도 쓰는 방법 있을까요? //
//내용
#endif

 
꼭 필요한건 아닌데 #if 가 있는게 신기하기도 하고 궁금하네요. 가르쳐주세요
ftfuture의 이미지

man g++에서

       -D name=definition
           The contents of definition are tokenized and processed as if they
           appeared during translation phase three in a #define directive.  In
           particular, the definition will be truncated by embedded newline
           characters.
 
           If you are invoking the preprocessor from a shell or shell-like
           program you may need to use the shell’s quoting syntax to protect
           characters such as spaces that have a meaning in the shell syntax.
 
           If you wish to define a function-like macro on the command line,
           write its argument list with surrounding parentheses before the
           equals sign (if any).  Parentheses are meaningful to most shells,
           so you will need to quote the option.  With sh and csh,
           -D’name(args...)=definition’ works.
 
           -D and -U options are processed in the order they are given on the
           command line.  All -imacros file and -include file options are pro?
           cessed after all -D and -U options.

g++ -D __DEBUG__=1 -o test_debug test_debug.cpp

형식으로 Makefile안에서 쓰시면 되지 않을까 싶네요 :)

ftfuture의 이미지

다시보니 변수나 이전에 define된 스트링값을 말씀하신거였네요..^^;
#define, #if 등은 preprocessor에서 처리되기 때문에.. 아마도 안될듯 합니다.

lipide의 이미지

요즘 커널을 보고 있는데요 이런 부분이 있던데..

#if (__GNUC__ == 3 && __GNUC_MINOR__ < 3)
#error Your compiler is too buggy; it is known to miscompile kernels.
#error    Known good compilers: 3.3
#endif

위에 말씀하신 것이 #ifdef 말고 #if로 사용 가능한지 여부라면 사용 가능한 거 같습니다.
다만, 변수로는 안되고 미리 define된 매크로이거나 makefile에서 위엣분이 말씀하신 대로 -D 옵션으로 넣어준 값이어야만 적용되는거 같네요 ^^
swiri15의 이미지

preprocessor이기 때문에, 즉 컴파일에 진행되기 이전에 처리되는 내용이므로

compile time에 존재하는 변수들에 대해서는 불가능하구요.

대신에 같은 preprocessor상에 존재하는 define된 애들에 대해서는 가능합니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.