[완료] C에서 struct definition에 관한 질문입니다.

hwanikani의 이미지

제가 알기로는 같은 이름의 definition은 무조건 한개만 존재할수 있다고 들었습니다 (같은 scope일때).

예를들어 a.c와 b.c파일이 있다고 볼때

a.c에 int a = 1;
b.c에 int a = 2; 라고 중복되면 에러가 뜨죠 (fatal error LNK1169: one or more multiply defined symbols found).

근데 신기하게 struct definition은 중복에러가 뜨질 않더라구요.
물론 한 파일 내에서 같은 tag로 구조체 선언을 두번 하면 에러가 뜹니다 (error C2011: 'test' : 'struct' type redefinition)

근데
a.c에 struct test { int a; };
b.c에 struct test { int b; int c;};같이

같은 tag로 여러 파일에 구조체 정의를 해도 전혀 에러가 안뜨더라구요 - _-;;;
그 결과 여러 파일에서 같은 구조체 tag가 전혀 다른 definition으로 해석될수 있는 상황이...

물론 실제 프로그래밍 할때는 저런짓 안하고 그냥 struct definition을 헤더에 넣은뒤
구조체가 필요할때마다 헤더를 포함해서 적절히 일관된 정의의 구조체를 사용할수 있기 때문에 저런 상황을 걱정할 필요가 없다는건 아는데요
변수의 경우는 같은 이름이 여러 파일내에 중복될 경우 링커가 에러를 잡아내지만
구조체 (혹은 c++의 클래스)같은 경우는 같은 이름 중복을 링커가 잡지 않는 이유가 알고 싶습니다.

구조체하고 클래스 같은 경우는 "type definition"이고
변수이름 같은 경우는 "object definition"이라 그런건지 헷갈리네요.

혹시 왜 그런지 아시는분 계시나요?

익명 사용자의 이미지

internal/external linkage 차이입니다.

http://publications.gbdirect.co.uk/c_book/chapter8/declarations_and_definitions.html

를 한 번 읽어보세요. 혹시 이미 linkage 에 대한 개념이 있으시다면,
맨 아래에 있는 코드만 보셔도 좋을겁니다.

익명 사용자의 이미지

재밌는 생각입니다.

중복을 검사하려면..

extern typedef struct { int a; } test;

같은 형태가 되어야 할듯..
익명 사용자의 이미지

아 댓글달고 생각해보니...
이것도 안될거 같네요. ^^

익명 사용자의 이미지

:)

hwanikani의 이미지

답변 감사드립니다.
http://publications.gbdirect.co.uk/c_book/chapter8/declarations_and_definitions.html
링크 들어가서 읽어 봤는데 linkage에 대해서 자세히 설명이 나와 있네요.

근데 제가 원하는 답은 안나와 있네요 ㅠㅠ
링커가 에러를 안잡아내는것을 봐서는 struct definition이 internal linkage라는 소리인데
위 링크에서도 struct definition에 관한 언급은 없고,
K&R, reference manual등을 봐도 struct definition의 linkage에 관한 정보는 찾을수가 없네요 ㅠㅠ

혹시 struct definition이 internal linkage라고 언급된 곳 아시는분 계신가요?

명확히 언급 되어야할 요소라 분명히 ANSI standard에 적혀 있어야 할것 같은데...

hwanikani의 이미지

Struct identifier는 linkage가 아예 없네요.

ANSI Standard에

3.1.2.2 Linkages of identifiers

The following identifiers have no linkage: an identifier declared
to be anything other than an object or a function

라고 적혀있네요.

댓글 달기

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