gcc arm에서 혹시 /##/ => //로 해주는 것 아시는 분 있나요

kyssmart의 이미지

C에서 ##을 이용해서 문자열을 합치는 define문을 정의해서 컴파일하는데
gcc컴파일시 token오류가 발생하네요... (참고:비쥬얼스튜디오 2003에서는 됨)

//////////////////////////////////////////
1. 개발중
//////////////////////////////////////////
//해더파일에 선언.h
#define DEBUG_N printf //개발중에
//#define DEBUG_N /##/ //개발완료후
...

//파일내부.c
DEBUG_N("디버그중\n");
...

=> 이와 같이 #define의 이용해서 함수를 바꿔서 간단하게 코딩후에
개발이 완료된후에는 위의 소스에서

//////////////////////////////////////////
2. 개발완료
//////////////////////////////////////////
//해더파일에 선언.h
//#define DEBUG_N printf //개발중에
#define DEBUG_N /##/ //개발완료후
...

//파일내부.c
DEBUG_N("디버그중\n");
...

=> [의도]
위의 DEBUG_N => //("디버그중\n");으로 변경되어서 주석처리로됨

=> 이와같이 의도가 비쥬얼스튜디오에서는 잘되는데
=> gcc로 컴파일 하면 아래와 같은 오류가 발생합니다.
src/SpriteManager.c: In function `drawSprite':
src/SpriteManager.c:221: parse error before '/' token
어떻게 다른 방법이 없을까요?

klutzy의 이미지

주석은 전처리기 작업 이전에 모두 사라집니다. VC++가 비표준적인 방식으로 동작하는 것입니다.

그리고 매크로를 좀 위험한 방식으로 처리하는 것 같은데(개인적인 의견이지만 함수에 매크로를 할 때에는 되도록 매크로 함수를 쓰는 게 좋다고 생각해요), 이렇게 하는 게 어떨까요.

#ifdef DEBUG
#define DEBUG_N(...) printf(__VA_ARGS__)
#else
#define DEBUG_N(...)
#endif

C99에서는 매크로 함수에서도 가변 인수를 지원합니다. 사용할 때 주의할 점이 있기는 하지만요. (__VA_ARGS__가 빈 값일 경우 func(a, b, __VA_ARGS__)가 func(a, b, )로 처리되어 컴파일 오류가 납니다)

어쨌거나 C99를 지원하는 컴파일러라면 지원합니다. GCC는 지원하겠는데 VC++는 잘 모르겠네요 --;;

Hyun의 이미지

if( something )
    DEBUG_N( "hi\n" );
another();
...

위와같을 때 오동작을 할 수 있습니다.

#define DEBUG_N(...) do{}while(0)
등으로 뭘 넣어주는게 좋을 듯 합니다.


나도 세벌식을 씁니다
klutzy의 이미지

네.. 실은 그걸 빼먹었었는데 고친다 해놓고 잊어버렸네요 --;;

저는 #define DEBUG_N(...) {}과 같이 쓰는 방식을 좋아해요. 뒤에 세미콜론을 붙일 수 있는 매크로 중에서는 {}가 짧은 편이니까요.

전웅의 이미지

$ cat > foo.c
#define DEBUG_N(...) {}
 
extern int something;
 
int main(void)
{
    if (something)
        DEBUG_N("hi\n");
    else
        another();
}
 
$ gcc -c foo.c
foo.c: In function `main':
foo.c:9: error: syntax error before "else"

--
Jun, Woong (woong at icu.ac.kr)
Web: http://www.woong.org (서버 공사중)

--
Jun, Woong (woong at gmail.com)
http://www.woong.org

전웅의 이미지

$ cat > foo.c
#define DEBUG_N(...)
 
if (something)
    DEBUG_N("hi\n");
another();
 
$ gcc -E foo.c
# 1 "foo.c"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "foo.c"
 
 
if (something)
    ;
another();

http://www.google.com/search?complete=1&hl=ko&q=C+null+statement&lr=&aq=f

--
Jun, Woong (woong at icu.ac.kr)
Web: http://www.woong.org (서버 공사중)

--
Jun, Woong (woong at gmail.com)
http://www.woong.org

오호라의 이미지

"모든 소스는 C-Language 한 권을 읽어 본 남녀노소 누구나 읽을수 있어야 한다."

## 은 ML 만의 전처리입니다. ~

표준을 되도록 안지키면 나중에 자잘한 것도 포팅이슈가 되는 것같습니다.

Hello World.

klutzy의 이미지

전처리기의 ##는 C/C++ 표준이 맞습니다. 적어도 C99와 C++98 표준에는 ##가 있습니다.

오호라의 이미지

CL 과 GCC 차이가 있습니다.

C:\> CL test.c
C:\> test.exe

HELLO WORLD
helloworld

$> gcc -std=c99 test.c
test.c: In function ‘main’:
test.c:8: warning: initialization makes integer from pointer without a cast
test.c:10: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
test.c:11:1: error: pasting ""hello"" and ""world"" does not give a valid preprocessing token

#define HELLO1 hello ## world
#define HELLO2 "hello" ## "world"
 
int main ( int argc, char* argv[] )
{
    int helloworld = "HELLO WORLD";
 
    printf( "%s\n", HELLO1 );
    printf( "%s\n", HELLO2 );
 
    return 0;
}

Hello World.

klutzy의 이미지

문자열에 대한 ## 동작은 VC++ 확장으로 보입니다. "hello" ## "world"의 결과로 helloworld가 나온다면 그것도 이상하고, "helloworld"가 필요하면 그냥 "hello" "world"로 쓰면 되고요.

lifthrasiir의 이미지

C 프로그래밍 언어는 전처리기와 문법, 의미론, 표준 라이브러리를 모두 포함하는 광범위한 표준입니다.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.