views:

86

answers:

2

I'm building a very small C/C++ project using eclipse and i'm getting the following during build:

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

What I can't figure out is why the linking is failing, or which linking flags to use to make this darn thing work! -L/usr/lib should be point the linker to the directory where the library is, but it still fails. When I do -l/usr/lib/myLibrary.a it still fails saying that it can't find -l/usr/lib/myLibrary.a

Any tips or advice on using the right commands for the linker would be appreciated! I'm stuck!

-Mike

+1  A: 

Your library file should be libsomething.a and the g++ option -lsomething

The manpage of g++ is more specific about this :

-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.
f4
Yes, I didn't realize -l would automatically change the name of the libraries like that. Once I properly specified the library name with -l the linking completed successfully. FYI, here is what I ended up with for the linker:g++ -L/usr/lib -o"linuxMonitor" ./Metric.o -lgtop-2.0 -lglib-2.0
Mike
A: 

In

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

you're not specifying any library.

Also, -l/usr/lib/myLibrary.a is wrong, you should rename your library to libmyLibrary.a and then use -lmyLibrary. If you can't do that, then you need g++ -o linuxMonitor ./Metric.o /usr/lib/myLibrary.a.

Alok
Yeah, that did it. Thanks.
Mike