簡體   English   中英

未定義對gnuplot_init()的引用

[英]undefined reference to `gnuplot_init()'

我正在嘗試為我的C ++項目使用gnuplot_i(ANSI-C的gnuplot接口)。 (請參見下載部分中的此處 )它似乎僅由一個標題和一個源文件組成。 Makefile只是創建一個目標文件,因此我決定將這兩個文件完全集成到我的項目中。 但是我收到未定義的引用的錯誤,這些引用在上述源文件中實現。 考慮下面的簡化示例(main.cpp):

#include <gnuplot_i.h>
int main(int argc, char** argv) {
    gnuplot_ctrl * h;
    h = gnuplot_init();
    return 0;
}

頭文件可在此處獲取,源文件可在此處獲取

我得到的錯誤如下:

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()在源文件中實現,該源文件被編譯為目標文件,然后用於鏈接程序。 您可以在下面的完整日志中看到。 同樣,生成的目標文件包含必要的符號:

$ 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 ++對象文件是否有問題? 據我了解,不應該。

好吧,當我按下發送鍵時,答案就傳給了我。

我顯然想念一個extern "C"

extern "C" {
#include <gnuplot_i.h>
}
int main(int argc, char** argv) {
        gnuplot_ctrl * h;
    h = gnuplot_init();
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM