views:

46

answers:

2

Hi there,

I am new to g++ and Makefile. I am trying to link this BeBOP SMC library, which is in my lib directory. Under the lib directory are bebop_util and sparse_matrix_converter, both of which have already been built without errors. I see libbebop_util.a, libbebop_util.so under bebop_util and libsparse_matrix_converter.a, libsparse_matrix_converter.so under sparse_matrix_converter. Below is the source:

Makefile

CC=g++
CFLAGS=-c
# CFLAGS=-c -Wall
INCLUDE_DIRS=-Ilib/bebop_util/include -Ilib/sparse_matrix_converter/include
LIB_DIRS=-Llib/bebop_util -Llib/sparse_matrix_converter
LIBS=-lbebop_util -lsparse_matrix_converter

test.out: test.o
        $(CC) -o test.out $(LIB_DIRS) $(LIBS) test.o

test.o: test.cpp
        $(CC) $(CFLAGS) $(INCLUDE_DIRS) test.cpp

clean:
        rm -f test.o test.out

test.cpp

extern "C" {
#include <bebop/smc/sparse_matrix.h>
#include <bebop/smc/sparse_matrix_ops.h>
}

int main(int argc, const char* argv[])
{
        struct sparse_matrix_t* A = load_sparse_matrix (MATRIX_MARKET, "sample_i
nput");
        destroy_sparse_matrix(A);
        return 0;
}

As a safeguard, I also have the LD_LIBRARY_PATH set:

login4% setenv | grep LD_LIBRARY_PATH
LD_LIBRARY_PATH=/share/apps/teragrid/globus-4.0.8-r1/myproxy-3.4/lib:/share/apps/teragrid/globus-4.0.8-r1/lib:/share/apps/teragrid/srb-client-3.4.1-r1/lib:/opt/apps/pgi7_2/mvapich/1.0.1/lib:/opt/apps/pgi7_2/mvapich/1.0.1/lib/shared:/opt/apps/pgi/7.2-5/linux86-64/7.2-5/libso:/opt/gsi-openssh-4.3/lib:/opt/apps/binutils-amd/070220/lib64:/share/home/01355/tomwang/cs380p_assn3/lib:/share/home/01355/tomwang/cs380p_assn3/lib/bebob_util:/share/home/01355/tomwang/cs380p_assn3/lib/sparse_matrix_converter

Output

login3% make
g++ -c -Ilib/bebop_util/include -Ilib/sparse_matrix_converter/include test.cpp
g++ -o test.out -Llib/bebop_util -Llib/sparse_matrix_converter -lbebop_util -lsparse_matrix_converter test.o
login3% ./test.out
./test.out: error while loading shared libraries: libbebop_util.so: cannot open shared object file: No such file or directory

Please suggest what may be wrong or additional info for me to provide. Thanks.

Tom

A: 

Are you sure the directory that libbebop_util.so is in is mentioned in your LD_LIBRARY_PATH? Based on your build line, the following should work:

env LD_LIBRARY_PATH=./lib:${LD_LIBRARY_PATH} ./test_out
R Samuel Klatchko
Thanks for the suggestion. I tried again with bebop_util included, as edited above, and it still does not work. :(
Tom Wang
I tried setting the path in .cshrc this time, and everything worked. Not sure if it makes a difference when the setenv was done. Thanks!
Tom Wang
Also, do you need to set LD_LIBRARY_PATH even when you explicitly set -L for g++? Thanks.
Tom Wang
Sort of. The g++/linker option -L only tells the linker where to find the library; but the ld.so.1 which loads shared object for execution does not have access to that data. ld.so.1 can use LD_LIBRARY_PATH, data embedded in the binary via the -rpath option or ldconfig data.
R Samuel Klatchko
Is -r[path to library] a g++ option? I cannot find this in the gcc documentation.
Tom Wang
-rpath is an ld option. You will need the g++ -Wl option to pass it through to the linker: `g++ -Wl,-rpath,/path/to/wherever`
R Samuel Klatchko
Thanks. I got the Makefile to work, too.
Tom Wang
A: 

It looks like you're not having problems with linking. Instead, the problem is that your built executable has a reference to libbebop_util.so that is invalid.

Try running ldd test.out to see where it's looking for the shared libraries.

perimosocordiae
Thanks. The suggestion was very useful as I try out different things. Too bad I do not have 15 reputation to vote up.
Tom Wang