기초 질문 입니다.

하하의 이미지

아래 문구에 대해 설명해 주실수 있나요?

_cplusplus 가 define 되있을때....

extern 키워드를 가지고 "C" 라는 문자열 속성을 지정??

#ifdef __cplusplus
extern "C" {
#endif

:oops: .. 즐거운 하루 되세요~

ㅡ,.ㅡ;;의 이미지

하하 wrote:
아래 문구에 대해 설명해 주실수 있나요?

_cplusplus 가 define 되있을때....

extern 키워드를 가지고 "C" 라는 문자열 속성을 지정??

#ifdef __cplusplus
extern "C" {
#endif

:oops: .. 즐거운 하루 되세요~

오류 아닌가..?디파인으로 막혀있으면 에러는 안나겠네요.


----------------------------------------------------------------------------

jj의 이미지

하하 wrote:
아래 문구에 대해 설명해 주실수 있나요?

_cplusplus 가 define 되있을때....

extern 키워드를 가지고 "C" 라는 문자열 속성을 지정??

#ifdef __cplusplus
extern "C" {
#endif

:oops: .. 즐거운 하루 되세요~

아마 밑에 가면

#ifdef __cplusplus
}
#endif

이런게 하나 더 있을텐데요, ifdef __cplus... 요건 compile되는 환경이 C++ 라면 이라는 뜻입니다. 보통 C++에서 class에 속하지 않는 free function 이나 변수 선언할때 쓰입니다.

--
Life is short. damn short...

buelgsk8er의 이미지

C와 C++은 기본적으로 컴파일시 심볼 이름을 부여하는데 차이가 있습니다. 예를들어 void hello(int, int)라는 함수가 있다면 C 컴파일러는 대략 _hello 라는 심볼 이름을 붙이는데 반해 C++ 컴파일러는 ?hello@@YAXHH@Z 이런 식으로 이름을 붙이죠. 그래서 이를테면 a.cpp에서 void hello(int,int)를 정의하고 b.c에서 그 함수를 참조하게 되면, b.o에서 참조하는 심볼은 _hello이지만 실제로 a.o에 정의된 심볼은 ?hello@@YAXHH@Z이므로 링크 단계에서 실패하게 됩니다. 따라서 C++컴파일러가 어떤 identifier를 C 형식 심볼 이름으로 해석해내게끔 할 때 extern "C"를 사용합니다. 이를테면 extern "C" void hello(int, int); 혹은 extern "C" { void hello(int, int); } 라고 하면 됩니다.

하지만 주의해야할 것은 extern "C"는 C++에만 있는 것이고 C에는 없다는 점입니다. C/C++양쪽에서 모두 인클루드되어 사용될 수 있는 헤더화일의 경우에 문제가 발생합니다. 예를들어 windows.h를 작성한다고 합시다. 단순히

...
HDC GetDC(HWND hwnd);
...

라고만 선언해버리면, C화일에서는 문제없이 사용할 수 있겠지만, C++화일에서 이러한 선언을 사용해버리면 링크할때 엉뚱한 심볼을 참조하려고 하면서 실패하게 됩니다. 그렇다고
extern "C" {
...
HDC GetDC(HWND hwnd);
...
}

이렇게 해버리면 C++에서는 문제없이 사용할 수 있겠지만 C컴파일러는 extern "C"라는 것을 인식하지 못할테므로 에러가 발생하겠지요. 따라서
#ifdef __cplusplus
extern "C" {
#endif
...
HDC GetDC(HWND hwnd);
...
#ifdef __cplusplus
}
#endif

이렇게 해서 C, C++ 양쪽에서 include할 수 있도록 해줍니다.

댓글 달기

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