2012-07-01 77 views
1

我做學校的一個項目,所以我需要編譯它:錯誤在編譯時(可能包括不止一個時間的函數)

gcc hide.c stegano.c -o hide -ansi -pedantic -Wall -Werror 

但後來我得到這個錯誤:

/tmp/ccDME1jC.o: In function `calculate_n': 
stegano.c:(.text+0x0): multiple definition of `calculate_n' 
/tmp/ccQxPZJu.o:hide.c:(.text+0x0): first defined here 
/tmp/ccDME1jC.o: In function `tam_msg': 
stegano.c:(.text+0x87): multiple definition of `tam_msg' 
/tmp/ccQxPZJu.o:hide.c:(.text+0x87): first defined here 
/tmp/ccDME1jC.o: In function `insere_msg': 
stegano.c:(.text+0xe1): multiple definition of `insere_msg' 
/tmp/ccQxPZJu.o:hide.c:(.text+0xe1): first defined here 
/tmp/ccDME1jC.o: In function `copia': 
stegano.c:(.text+0x201): multiple definition of `copia' 
/tmp/ccQxPZJu.o:hide.c:(.text+0x201): first defined here 
/tmp/ccDME1jC.o: In function `esconde_msg': 
stegano.c:(.text+0x274): multiple definition of `esconde_msg' 
/tmp/ccQxPZJu.o:hide.c:(.text+0x274): first defined here 
collect2: ld returned 1 exit status 

程序代碼是這樣的,我認爲錯誤可能是在包括的我便藏的實際代碼:

程序hide.c是這樣的:

#include <stdio.h> 
#include <stdlib.h> 
#include "stegano.c" 
//code// 

然後調用stegano.c,包含在hide.c使用的所有實際功能:

#include <stdio.h> 
#include <stdlib.h> 
#include "stegano.h" 
//code// 

而頭文件stegano.h:

#include <stdio.h> 
#include <stdlib.h> 
#define MAX 100 

typedef unsigned char Byte; 

void calculate_n(char name[MAX], int* n, int* x); 
int tam_msg(char name[MAX]); 
void insere_msg(int size, char name[MAX], Byte* v); 
void copia(Byte* v1, Byte *v2, int size); 
void esconde_msg(Byte* msg, char name1[MAX], char name2[MAX]); 

感謝您的幫助!

回答

5

通過這導致:

#include "stegano.c" 

這將拉動中stegano.c的所有函數定義爲hide.c。含義stegano.chide.c現在定義了相同的功能。這會產生您嘗試(編譯和)鏈接時看到的多個定義的錯誤

包含頭文件來代替:

#include "stegano.h" 
2

您需要刪除#include "stegano.c"。改爲包含stegano.h文件。

通過(包括它的時候,一旦編譯直接文件時一次),包括.c文件你基本上嘗試編譯從該文件中的代碼兩次,因此兩個stegano.ohide.o將包含將在聯打破相同的功能相。