私のC++プロジェクトでANSI-Cのgnuplotインタフェースgnuplot_iを使用しようとしています。 (ダウンロードセクションのhereを参照してください) 1つのヘッダーと1つのソースファイルで構成されているようです。 Makefileはオブジェクトファイルを作成するだけなので、私はそれらの2つのファイルをプロジェクトに完全に統合することに決めました。しかし、私は上記のソースファイルで実装されている関数への未定義の参照のエラーを取得しています。 次の簡単な例(main.cppに)を考慮してください`gnuplot_init() 'への未定義の参照
#include <gnuplot_i.h>
int main(int argc, char** argv) {
gnuplot_ctrl * h;
h = gnuplot_init();
return 0;
}
ヘッダファイルhere、ソースファイルhereを得ることができます。
私が得るエラーは以下の通りです:しかしgnuplot_init()
は、その後、プログラムをリンクするために使用されたオブジェクトファイルにコンパイルされるソースファイルに実装されて
build/Debug/GNU-Linux/main.o: In function `main':
/<some path>/proj/test/main.cpp:18: undefined reference to `gnuplot_init()'
collect2: error: ld returned 1 exit status
。あなたは以下の完全なログでこれを見ることができます。
$ nm gnuplot_i.o | grep gnuplot_init
0000000000000000 T gnuplot_init
完全なログ:
cd '/<some path>/proj/test'
/usr/bin/make -f Makefile CONF=Debug clean
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .clean-conf
make[1]: Entering directory '/<some path>/proj/test'
rm -f -r build/Debug
rm -f dist/Debug/GNU-Linux/test
make[1]: Leaving directory '/<some path>/proj/test'
CLEAN SUCCESSFUL (total time: 52ms)
cd '/<some path>/proj/test'
/usr/bin/make -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/<some path>/proj/test'
"/usr/bin/make" -f nbproject/Makefile-Debug.mk dist/Debug/GNU-Linux/test
make[2]: Entering directory '/<some path>/proj/test'
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/gnuplot_i.o.d"
gcc -c -g -I. -MMD -MP -MF "build/Debug/GNU-Linux/gnuplot_i.o.d" -o build/Debug/GNU-Linux/gnuplot_i.o gnuplot_i.c
gnuplot_i.c: In function ‘gnuplot_tmpfile’:
gnuplot_i.c:696:5: warning: implicit declaration of function ‘close’ [-Wimplicit-function-declaration]
close(unx_fd);
^~~~~
mkdir -p build/Debug/GNU-Linux
rm -f "build/Debug/GNU-Linux/main.o.d"
g++ -c -g -I. -MMD -MP -MF "build/Debug/GNU-Linux/main.o.d" -o build/Debug/GNU-Linux/main.o main.cpp
mkdir -p dist/Debug/GNU-Linux
g++ -o dist/Debug/GNU-Linux/test build/Debug/GNU-Linux/gnuplot_i.o build/Debug/GNU-Linux/main.o
build/Debug/GNU-Linux/main.o: In function `main':
/<some path>/proj/test/main.cpp:18: undefined reference to `gnuplot_init()'
collect2: error: ld returned 1 exit status
make[2]: *** [nbproject/Makefile-Debug.mk:64: dist/Debug/GNU-Linux/test] Error 1
make[2]: Leaving directory '/<some path>/proj/test'
make[1]: *** [nbproject/Makefile-Debug.mk:60: .build-conf] Error 2
make[1]: Leaving directory '/<some path>/proj/test'
make: *** [nbproject/Makefile-impl.mk:40: .build-impl] Error 2
BUILD FAILED (exit value 2, total time: 304ms)
私は、NetBeansのMakefileから自動生成されたビルドシステムを使用していたようで、非常に大きく、複雑でも、生成されたオブジェクト・ファイルが必要なシンボルが含まれています。しかし、どのコマンドが発行されたかは、ログからはっきりとわかるはずです。ここで何が間違っていますか? CオブジェクトファイルとC++オブジェクトファイルをリンクするのは問題ですか?私の理解から、それはすべきではありません。