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
'cFile.h' 헤더 파일을 보여주고 싶을 수도 있습니다. 대부분 * 변수를 * 선언하는 대신 변수를 정의합니다. –
cfile.h의 모양은 무엇입니까? – cup
헤더 파일은 얼마되지 않습니다. – nubbear