g ++ 컴파일러를 사용하여 cpp 스크립트를 ac 파일에 연결하려고했습니다. 하지만 .c 파일에서 전역 var를 계속해서 이중 정의로 선언했습니다.
.cpp
extern "C"
{
#include "cFile.h"
}
아래는 2 개의 파일을 컴파일하는 방법입니다.
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
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
현재 코드에서 문제 는 .h 파일 에 전역 변수 를 정의 한다는 것 입니다. 따라서 모든 .o 파일 에서 정의되며 .h 파일이 컴파일에 포함됩니다. 링커는 이와 같은 여러 정의를 좋아하지 않습니다. 만에 물건 정의 가 .c / 의 .cpp 하나 개 .o 인 파일을 한 번만 컴파일되는 파일을.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다