C언어 multiple definition of (함수) 오류
글쓴이: niceedu / 작성시간: 수, 2018/03/14 - 3:58오후
함수를 선언한 헤더파일 header.h
#ifndef __FUNCTION_H__ #define __FUCNTION_H__ #include<stdio.h> #include<stdlib.h> #include<string.h> #define TRUE 1 #define FALSE 0 #define MAX_COMMAND_SIZE 100 char COMMAND[MAX_COMMAND_SIZE]; int what_is_command(char command[]); void func_help(void); #endif
함수를 정의한 define.c 파일
#include "header.h"
int what_is_command(char command[]){
if(strcmp(command,"help") == 0 || strcmp(command,"h") == 0)
return 1;
else if(strcmp(command, "dir")==0|| strcmp(command,"d") == 0)
return 2;
else if(strcmp(command,"quit" )==0|| strcmp(command,"q")==0)
return 3;
else if(strcmp(command,"history")==0|| strcmp(command,"hi")==0)
return 4;
else if(strcmp(command,"dump" )==0|| strcmp(command,"du")==0)
return 5;
else if(strcmp(command,"edit" )==0|| strcmp(command,"e")==0)
return 6;
else if(strcmp(command,"fill" )==0|| strcmp(command,"f")==0)
return 7;
else if(strcmp(command,"reset" )==0)
return 8;
else if(strcmp(command,"opcode" )==0)
return 9;
else if(strcmp(command,"opcodelist")==0)
return 10;
else
return 0;
}
void func_help(void){
printf("h[elp]\n");
printf("d[ir]\n");
printf("q[uit\n]");
printf("hi[story\n");
printf("du[mp] [start, end]\n");
printf("e[dit] address, value\n");
printf("f[ill] start, end, value");
printf("reset");
printf("opcode mnemonic\n");
printf("opcodelist\n");
}main함수가 담긴 파일 main.c
#include "header.h"
#include "define.c"
int main(){
while(TRUE){
printf("sicsim> ");
scanf("%s", COMMAND);
switch(what_is_command(COMMAND)){
case 1 : func_help(); break;
default: break;
}
}
return 0;
}Makefile의 내용
main.out: main.o define.o
gcc -o main.out main.o define.o
define.o: define.c header.h
gcc -c define.c
main.o: main.c define.c header.h
gcc -c main.c define.c오류내용
define.o: In function `what_is_command':
define.c:(.text+0x0): multiple definition of `what_is_command'
main.o:main.c:(.text+0x0): first defined here
define.o: In function `func_help':
define.c:(.text+0x1d0): multiple definition of `func_help'
main.o:main.c:(.text+0x1d0): first defined here
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'main.out' failed
make: *** [20120121.out] Error 1
cse20120121@cspro:~/sp20120121_proj1$
분명 중복 오류를 잘 피할 수 있을거라 생각했는데 위와같은 오류가 발생했습니다.
makefile에서 잘못된건가요? ㅠ_ㅠ
Forums:


main과 define을 따로 컴파일하고 링크를
main과 define을 따로 컴파일하고 링크를 하던지 아니면 그냥 한 커맨드에서 컴파일하던지 선택을 해야지요. 지금은 define을 두 번 컴파일하잖아요. define.o 타겟에서 한 번 하고 main.o 타겟에서 한 번 더 하고.
#include "define.c" 부분을 지우세요.
#include "define.c" 부분을 지우세요.
댓글 달기