简体   繁体   中英

undefined reference to `gnuplot_init()'

I'm trying to use gnuplot_i, a gnuplot interface for ANSI-C, for my C++ project. (See here in the download section) It seems to just consist of one header and one source file. The Makefile just creates an object file, so I decided to completely integrate those two files in my project. However I'm getting errors of undefined references to functions that are implemented in aforementioned source file. Consider the following simplified example (main.cpp):

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

The header file can be obtained here and the source file here .

The error I get is the following:

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

However gnuplot_init() is implemented in the source file which gets compiled to an object file which is then used to link the program. You can see this in the full log below. Also the generated object file contains the necessary symbol:

$ nm gnuplot_i.o | grep gnuplot_init
0000000000000000 T gnuplot_init

full log:

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)

As I'm using an auto-generated buildsystem from NetBeans the Makefiles are quite big and complex. But it should be pretty obvious from the log which commands have been issued. What is exactly wrong here? Is it a problem that I link C-Object files and C++-Object files? From my understanding it shouldn't.

Well the answer came to me the moment I pressed send.

I'm obviously missing an extern "C"

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM