C++を忘れているようです...
C でいくつかの関数を別のソースで宣言しようとしており、.h
必要に応じて適切なものを含めています。コンパイルはOKです。しかし、問題はリンク中にあり、リンカーは関数が既に定義されていると不平を言います。
関数を として定義しようとさえしextern
ましたが、(無駄な) 関数を宣言するだけで、実装が.c
.
これは私のコードの要約です:
common.h
#include <stdio.h>
module1.h
#include "common.h"
#ifndef MODULE1_H_
#define MODULE1_H_
int init(int option);
int open(char* db, char* username, char* password);
int get(int handler, int date[2], int time[2], int* data, int& rowsize, int& numrows);
int put(int handler, int* data, int& datasize, int& numrows);
int close(int handler);
int finalize();
#endif /* MODULE1_H_ */
module2.h
#include "common.h"
#ifndef MODULE2_H_
#define MODULE2_H_
int get1(int handler, int date, int time, int *data, int& datasize, int& rowsize);
int put1(int handler, int* data, int datasize);
#endif /*MODULE2_H_*/
module1.cpp
#include "module1.h"
int init(int option) { ... }
int finalize() { ... }
int get(int handler, int date[2], int time[2], int* data, int& rowsize, int& numrows) {
....
}
...
module2.cpp
#include "module1.h"
#include "module2.h"
int get1(int handler, int date, int time, int* data, int rowsize) {
int daterange[2]={date,date};
int timerange[2]={time,time};
int rsize, numrows, result;
result=get(handler, daterange,timerange, data, rsize, numrows);
rowsize=rsize;
if(numrows!=1) printf("Uh oh...\n");
return result;
}
...
コンパイルとリンケージ:
g++ -o module1.o -c module1.cpp
g++ -o module2.o -c module2.cpp
g++ -fPIC -shared -o library.so module1.o module2.o
私が言ったように、それはOKをコンパイルします。module1.h
問題は、リンカが から 2つの実装された関数があることを「認識する」リンケージ中にありmodule1.h
ますmodule1.cpp
。module1.h
もう 1 つは一緒に を含めることmodule2.h
ですmodule2.cpp
。
関数が宣言されることになっていることは知っていますが、明らかに間違っています。誰かがそれがどこにあるのか指摘してもらえますか? 前もって感謝します。