为什么我在链接期间得到“未定义的引用`glibtop_init'”?

时间:2010-02-13 03:18:04

标签: c++ gcc linker g++

我正在使用eclipse构建一个非常小的C / C ++项目,我在构建期间得到以下内容:

make all
Building file: ../Metric.cpp
Invoking: GCC C++ Compiler
g++ -I/usr/include/glib-2.0 -I/usr/include/libgtop-2.0 -I/usr/lib/glib-2.0/include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"Metric.d" -MT"Metric.d" -o"Metric.o" "../Metric.cpp"
Finished building: ../Metric.cpp

Building target: linuxMonitor
Invoking: GCC C++ Linker
g++ -L/usr/lib -o"linuxMonitor" ./Metric.o
./Metric.o: In function main':
/home/mike/workspace_c/linuxMonitor/Debug/../Metric.cpp:27: undefined reference to
glibtop_init'
collect2: ld returned 1 exit status
make: *** [linuxMonitor] Error 1

我无法弄清楚为什么链接失败,或者使用哪个链接标志来使这个糟糕的东西工作! main':
/home/mike/workspace_c/linuxMonitor/Debug/../Metric.cpp:27: undefined reference to
应该将链接器指向库所在的目录,但它仍然失败。当我-L/usr/lib时,它仍然无法说它找不到-l/usr/lib/myLibrary.a

任何有关为链接器使用正确命令的提示或建议都将不胜感激!我被卡住了!

-Mike

2 个答案:

答案 0 :(得分:1)

你的库文件应该是libsomething.a和g ++选项-lsomething

g ++的联机帮助页更加具体:

-l library

...

The linker searches a standard list of directories for the library,
which is actually a file named liblibrary.a.  The linker then uses
this file as if it had been specified precisely by name.

The directories searched include several standard system
directories plus any that you specify with -L.

...

The only difference between using an -l option and
specifying a file name is that -l surrounds library with lib and .a
and searches several directories.

答案 1 :(得分:0)

g++ -L/usr/lib -o"linuxMonitor" ./Metric.o

您没有指定任何库。

另外,-l/usr/lib/myLibrary.a错误,您应该将库重命名为libmyLibrary.a,然后使用-lmyLibrary。如果你不能这样做,那么你需要g++ -o linuxMonitor ./Metric.o /usr/lib/myLibrary.a