• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    해결됨

my_functions.h 파일 질문입니다

21.02.25 00:43 작성 조회수 262

0

my_functions.h에서 

static int si = 0;

static int multiply(int a, int b) {

return a * b;

}

에서 두개 다 static을 빼면 컴파일이 안되던데 왜 그런지 잘 모르겠습니다

답변 7

·

답변을 작성해보세요.

2

안소님의 프로필

안소

2021.02.28

static이 붙지 않으면 extern 으로 간주됩니다. 전역변수들은 모두 extern 이기 떄문이에요. extern 을 생략할 수도 있구요.

static을 떼면 si 는 extern 변수가 되고, multiply 함수는 extern 함수가 됩니다.

근데 my_function.c 와 main.c 두군데에서 my_function.h 헤더 파일을 인클루딩 하고 있죠.

헤더파일을 인클루딩할 땐 헤더파일에 있는 모든 코드가 인클루딩한 곳으로 복사가 됩니다.

그러니 extern 변수, 함수인 si와  multiply 는 my_function.c 와 main.c 두군데에서 "정의"가 되는 것이나 마찬가지이기 때문에 에러가 발생하는 것이에요.

즉 동일한 범위 내에 동일한 이름의 변수와 함수를 정의해서 발생하는 에러와도 같다고 보시면 됩니다.

my_function.h 에서 정의를 하지 않고 그냥 선언만 하고 my_function.c 에서 정의를 해준다면 이렇게 중복 정의를 피할 수 있기 때문에 문제가 되지 않으실거에요.

// 아래와 같은 모습일 땐 문제 없음

// my_functions. h  (선언만)
int si;
int multiply(int a, int b);

// my_functions.c  (정의)
#include "my_functions.h"
int si = 0;
int multiply(int a, int b)
{
	return a * b;
}

1

안소님의 프로필

안소

2021.02.25

일단 제 환경에선 떼도 빌드 문제가 없고 어차피 static을 떼도 extern 처리 되기 때문에 별 문제 없을 것 같아서 저도 의아하고 잘 모르겠네요 ㅠㅠ

에러 메세지를 보니 두군데서 정의되서 그런 것 같은데 작성하신 main.cpp, my_functions.h, my_functions.cpp 코드 모두 다 주실 수 있을까요?

1

안소님의 프로필

안소

2021.02.25

static 떼도 잘 빌드 되는 것이 확인 되는데 의아하네요..!

일단 이 상황만 봐선 원인을 파악하기 힘드네요. 죄송합니다 ㅠㅠ 

좀 더 구체적인 설명과 강의 시간대 부탁드리겠습니다!

0

it09kim님의 프로필

it09kim

질문자

2021.02.28

감사합니다!! 이해되었어요 ㅎㅎ

0

it09kim님의 프로필

it09kim

질문자

2021.02.25

이건 에러메시지입니당,,ㅜ

0

it09kim님의 프로필

it09kim

질문자

2021.02.25

-main.c-

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include "my_functions.h"

#include "my_structures.h"

#include "my_macros.h"

extern int status;

int main()

{

#include "hello_world.h"

printf("PI = %f\n", PI);

printf("%p %d\n", &status, status);

print_status();

printf("%d\n", multiply(51, 2));

printf("main()\n");

printf("Static function address %p\n", multiply);

printf("Static variable address %p\n", &si);

print_address();

return 0;

}

- my_function.h - 

#pragma once

#include "my_functions.h"

extern int status;

int si = 0;

extern int add(int a, int b);

int multiply(int a, int b) {

return a * b;

}

// int subtract(int a, int b)

inline int subtract(int a, int b) {

return a - b;

}

void print_status();

void print_address();


-my_function.c-


#include "my_functions.h"

#include <stdio.h>

int status = 0;

int add(int a, int b) {

return a + b;

}

void print_status() {

printf("Address = %p, Value = %d\n", &status, status);

}

void print_address() {

printf("print_address()\n");

printf("Static function address %p\n", multiply);

printf("Static variable address %p\n", &si);

}

이렇게 교수님이랑 static 뺴곤 다 동일합니다 ㅜㅜ 왜이런지모르겠네요,,

0

it09kim님의 프로필

it09kim

질문자

2021.02.25

my_function.h에 있는 int multiply와 int si에서 static을 떼니까 _multiply already defined in main.obj
                              _si already defined in main.obj
                              one or more multiply defined symbols found 라고 링킹에러 뜨는데 왜이런지 모르겠네요 ㅜㅜ
static 떼도 잘 빌드되시나요?