파일간 struct 변수 공유하기 문제

wogkr3654의 이미지

안녕하세요, c 언어 공부중인데, 아무리 찾아봐도 제가 쓰려는 방식으로 쓰려는 사람이 없는 것 같아서
질문 올립니다.

struct 변수를 여러 파일에서 공유해서 쓰고 싶습니다.
문제는 제가 찾아본 사이트들은 struct 변수라는 자료형 (구조체이름??) 을 공유하는 것이지
실제 오브젝트는 여러 소스파일에서 각각 따로 만들어서 사용하는 것이라고 생각했습니다.

저는 하나의 struct 오브젝트를 만들어서 여러 소스파일에서 변경이 가능하도록 만들고 싶은데
컴파일 시 자꾸 에러가 발생합니다.
다음과 같이 구성하였습니다.

// main.h

#ifdef _main_
struct ABC{
  int a;
  int b;
  int c;
}abc;
 
#else
extern struct ABC;
#endif

// func.h

int func(void);

// func.c

#include "func.h"
#include "main.h"
int func()
{
  abc.a = 1;
  abc.b = 2;
  abc.c = abc.a + abc.b;
  return abc.c;
}

// main.c

#define _main_
#include "main.h"
void main()
{
  int aa;
  aa=func();
  printf("%d",abc.c);
}

여기서 컴파일 시 에러가 발생하는데, "illegal use of incomplete struct 'struct ABC' 라고 나오며,
func.c 파일에서 발생합니다.

지금까지 생각한 방식으로는 main.h 에서 extern struct ABC *abc 로 변경하여
main.c 가 아닌 다른 파일에서는 abc->a 로만 접근할 수 있게 만들면 되지 않을까 하는데,
위 에러가 발생하는 원인과 해결책을 알 수 있을까요?

감사합니다.

꾸벅

ymir의 이미지

func.c 는 struct 에 ABC 라는 tag name 만 알고, 어떤 member 들이 있는지 모르기 때문에..
거기에서 struct ABC 는 incomplete type 이 됩니다.

대개는 length 가 없는 array 나 member 가 없는 struct/union 들과 같이..
size 를 알 수 없는 type 들이 incomplete type 이 됩니다.

그래서 이런 애들은 적어도 같은 scope 내에서 size 를 알 수 있겠끔 member 들을 명시해 주어야 합니다.

extern 은 변수의 linkage 를 결정하는 키워드인데..
func.c 는 struct ABC 가 뭔지도 모르고, 어떤 변수를 참조해야 하는지도 빠져 있죠.

struct ABC{
  int a;
  int b;
  int c;
};
 
#ifdef _main_
struct ABC abc;
#else
extern struct ABC abc;
#endif

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

wogkr3654의 이미지

정말 감사드립니다 해결되었습니다!

댓글 달기

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