2014-02-16 2 views
0

g ++ 컴파일러를 사용하여 cpp 스크립트를 c 파일에 연결하려고했습니다. 하지만 이중 정의로 선언 된 .c 파일에서 전역 변수를 계속 가져옵니다.C++과 C 객체를 연결할 수 없습니다.

통화 당

extern "C" 
{ 
#include "cFile.h" 
} 

아래는이 개 파일을 컴파일 내 방법입니다.

g++ -c -o cFile.o cFile.c 
g++ -c -o cppFile.o cppFile.cpp 
g++ -o ExeFile cFile.o cppFile.o 

다음은 나의 오류 메시지입니다.

cFile.o:(.bss+0x0): multiple definition of `NoOfRecordsRead' 
cppFile.o:(.bss+0x0): first defined here 
cFile.o:(.bss+0x20): multiple definition of `globalCountryDataArray' 
cppFile.o:(.bss+0x20): first defined here 
collect2: error: ld returned 1 exit status 

도와주세요. 미리 감사드립니다!

다음은 요청한대로 원시 헤더 파일입니다.

#ifndef COUNTRY_DATA_H 
#define COUNTRY_DATA_H 

// ==================================================================== 

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 

// ==================================================================== 

#define TLD_LEN    2 
#define COUNTRY_LEN   100 
#define FIPS104_LEN   2 
#define ISO2_LEN   2 
#define ISO3_LEN   3 
#define CAPITAL_LEN   100 
#define REGION_LEN   100 
#define CURRENCY_LEN  50 
#define CURRENCY_CODE_LEN 3 
#define No_Of_Rec_Fields  11 
#define Max_Record_Size   250 
#define Line_Char_Buffer_Size 400 
#define LINE_DATA_DELIMITER  "," 
#define INPUT_FILE_NAME   "Countries.txt" 

// =================================================================== 
//const char* LINE_DATA_DELIMITER  = ","; 
//const char* INPUT_FILE_NAME   = "Countries.txt"; 

typedef struct CountryRecord 

{ 

    char TLD   [TLD_LEN+1];   // Top Level Domain code 

    char Country  [COUNTRY_LEN+1];  

    char FIPS104  [FIPS104_LEN+1];  // Ctry code according to FIPS104 standard 

    char ISO2   [ISO2_LEN+1];   // Ctry code according to ISO2 standard 

    char ISO3   [ISO3_LEN+1];   // Ctry code according to ISO3 standard 
    double ISONo; 
    char Capital  [CAPITAL_LEN+1];  
    char Region   [REGION_LEN+1];   // E.g. Asia, Europe, etc. 
    char Currency  [CURRENCY_LEN+1];  // Full name of currency 
    char CurrencyCode [CURRENCY_CODE_LEN+1]; // Currency abbreviation 
    double Population; 

} CountryRecordType; 

int NoOfRecordsRead; 
CountryRecordType globalCountryDataArray [Max_Record_Size]; 


// ==================================================================== 

void readData(); 
char* get_line (char *s, size_t n, FILE *f); 
CountryRecordType createCountryRecord (char* aLine); 
void displayRecordContent (CountryRecordType ctryRec); 
void showAllRecords(); 
int findCountryRecord (const char* countryName);  
char* getCapital (const char* countryName); 
char* getCurrencyCode (const char* countryName); 
// ==================================================================== 

#endif // COUNTRY_DATA_H 
+0

'cFile.h' 헤더 파일을 보여주고 싶을 수도 있습니다. 대부분 * 변수를 * 선언하는 대신 변수를 정의합니다. –

+0

cfile.h의 모양은 무엇입니까? – cup

+0

헤더 파일은 얼마되지 않습니다. – nubbear

답변

3

당신의 cFile.h 파일이 라인은 라인 int NoOfRecordsRead;은 다음과 같아야합니다

extern int NoOfRecordsRead; // declare that this variable is defined somewhere 

그런 다음 cFile.c에, 당신이해야

int NoOfRecordsRead; // define this variable in exactly one place 

현재 코드의 문제점은 입니다. 글로벌 varia를 정의합니다. .h 파일에 있습니다. 따라서 모든 .o 파일에 정의되어 있습니다. 여기서 .h 파일이 컴파일에 포함됩니다. 링커는 그런 다중 정의를 좋아하지 않습니다. .c/.cpp 파일을 한 번만 .o 파일로 컴파일하면됩니다.

+0

배열에 대해 동일한 작업을 수행합니까? – nubbear

+0

배열에 대해 동일한 방법을 시도했습니다. 동일한 오류를 출력합니다. 흠 ... – nubbear

+0

* 동일한 * 오류가 출력되면 이유는 .o 파일이 고쳐질 수 있습니다. 전체 재구성을 시도하십시오. 또한 오류를 나타내는 기호에 grep을 지정하고 다른 곳에서는 정의되지 않았는지 확인하십시오. – hyde

2

1) 나는 extern "C" {...}INSIDE에게 헤더가 아닌 외부를 둘 것입니다.

2) 문제는 헤더에 extern int NoOfRecordsRead;을 넣을 것이란 점입니다.

실제로 "NoOfRecordsRead"를 하나만 ... C 또는 C++ 모듈에 선언하십시오.

3) ountryRecordType globalCountryDataArray[]도 마찬가지입니다.

0

hyde와 FroggyDay가 제공하는 결합 솔루션. 내 .cpp 파일에서 다른 코드와 함께 cppFile.h에 내

extern "C" 
{ 
    #include "CountryData.h"  
} 

배치 복사 된 다음 내 CPP 파일의 .H을 생성합니다.

나는 다음 내 C 파일에 varibles를 선언,

extern int NoOfRecordsRead; 
extern CountryRecordType globalCountryDataArray [Max_Record_Size]; 

마지막으로 내 C 헤더에 전역 변수에 대한 통근을 배치 하이드의 솔루션을 따랐다.

int NoOfRecordsRead; 
CountryRecordType globalCountryDataArray [Max_Record_Size]; 

그러면 스크립트를 성공적으로 컴파일 할 수 있습니다.

+0

SO에 오신 것을 환영합니다. 이 사이트가 작동하는 방식으로 "감사합니다.이 답변"을 추가하지 말고 녹색 체크 표시를 사용하여 "이 문제가 해결되었습니다."라고 대답 하나를 선택하십시오. –

+0

okay.noted.thanks! – nubbear

관련 문제