对“gnuplot_init()”的未定义引用
undefined reference to `gnuplot_init()'
我正在尝试为我的 C++ 项目使用 gnuplot_i,一个用于 ANSI-C 的 gnuplot 接口。 (请参阅下载部分中的 here)
它似乎只包含一个头文件和一个源文件。 Makefile 只是创建一个目标文件,所以我决定将这两个文件完全集成到我的项目中。但是,我收到对上述源文件中实现的函数的未定义引用错误。
考虑以下简化示例 (main.cpp):
#include <gnuplot_i.h>
int main(int argc, char** argv) {
gnuplot_ctrl * h;
h = gnuplot_init();
return 0;
}
可以得到头文件here and the source file here.
我得到的错误如下:
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
然而 gnuplot_init()
是在源文件中实现的,源文件被编译成目标文件,然后用于 link 程序。您可以在下面的完整日志中看到这一点。生成的目标文件还包含必要的符号:
$ 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 非常庞大且复杂。但是从日志中应该可以很明显地看出发出了哪些命令。这里究竟出了什么问题?我 link C-Object 文件和 C++-Object 文件有问题吗?根据我的理解,它不应该。
好吧,当我按下发送键的那一刻,答案就出现了。
我显然错过了 extern "C"
extern "C" {
#include <gnuplot_i.h>
}
int main(int argc, char** argv) {
gnuplot_ctrl * h;
h = gnuplot_init();
return 0;
}
我正在尝试为我的 C++ 项目使用 gnuplot_i,一个用于 ANSI-C 的 gnuplot 接口。 (请参阅下载部分中的 here) 它似乎只包含一个头文件和一个源文件。 Makefile 只是创建一个目标文件,所以我决定将这两个文件完全集成到我的项目中。但是,我收到对上述源文件中实现的函数的未定义引用错误。 考虑以下简化示例 (main.cpp):
#include <gnuplot_i.h>
int main(int argc, char** argv) {
gnuplot_ctrl * h;
h = gnuplot_init();
return 0;
}
可以得到头文件here and the source file here.
我得到的错误如下:
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
然而 gnuplot_init()
是在源文件中实现的,源文件被编译成目标文件,然后用于 link 程序。您可以在下面的完整日志中看到这一点。生成的目标文件还包含必要的符号:
$ 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 非常庞大且复杂。但是从日志中应该可以很明显地看出发出了哪些命令。这里究竟出了什么问题?我 link C-Object 文件和 C++-Object 文件有问题吗?根据我的理解,它不应该。
好吧,当我按下发送键的那一刻,答案就出现了。
我显然错过了 extern "C"
extern "C" {
#include <gnuplot_i.h>
}
int main(int argc, char** argv) {
gnuplot_ctrl * h;
h = gnuplot_init();
return 0;
}