c++makefilelinkerg++undefined-reference

Program won't compile anymore


I'm current working on a C++ that I edit locally on my Mac but run on an Ubuntu server. I always make sure that the code compiles on my mac before uploading it to the server to compile it there, where I have to use a makefile to link with libraries that are installed in my local directory. Basically, I had edited a significant portion of my code, found that it compiled on my mac, and uploaded it to the server to compile, but it doesn't compile! Luckily, I had a backup version of the code, so I tried that on the server, and that won't compile anymore either! In between the last time I knew that my code compiled on the server and now, I know that they ran some updates, but that's all I can think of that's different. For reference, here's my make file:

LOCAL_INCLUDE = /home/schraiber/.local/include
LOCAL_LIB = /home/schraiber/.local/lib
CXXFLAGS = -I$(LOCAL_INCLUDE)
CXX_LDFLAGS = -L$(LOCAL_LIB) -lgsl -lm -lgslcblas -lpthread

CoalHMMgf: main.o IOUtilities.o parameters.o Algorithms.o Optimization.o probabilities.o RNGUtilities.o Data.o ThreadData.h
        g++ $(CXXFLAGS) $(CXX_LDFLAGS) main.o IOUtilities.o parameters.o Algorithms.o Optimization.o probabilities.o RNGUtilities.o Data.o -o CoalHMMgf
main.o: main.cpp IOUtilities.h parameters.h Algorithms.h Optimization.h probabilities.h ThreadData.h Data.h
        g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c main.cpp
IOutilities.o: IOUtilities.h IOUtilities.cpp parameters.h data.h
        g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c IOUtilities.cpp
parameters.o: parameters.h parameters.cpp
        g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c parameters.cpp
Algorithms.o: Algorithms.h Algorithms.cpp
        g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c Algorithms.cpp
Optimization.o: Optimization.h Optimization.cpp Algorithms.h parameters.h probabilities.h RNGUtilities.h ThreadData.h IOUtilities.h
        g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c Optimization.cpp
probabilities.o: probabilities.h probabilities.cpp parameters.h
        g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c probabilities.cpp
RNGUtilities.o: RNGUtilities.h RNGUtilities.cpp
        g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c RNGUtilities.cpp
Data.o: Data.h Data.cpp
        g++ $(CXXFLAGS) $(CXX_LDFLAGS) -c Data.cpp

and the error:

g++ -I/home/schraiber/.local/include -L/home/schraiber/.local/lib -lgsl -lm -lgslcblas -lpthread main.o IOUtilities.o parameters.o Algorithms.o Optimization.o probabilities.o RNGUtilities.o Data.o -o CoalHMMgf
main.o: In function `main':
main.cpp:(.text+0xbf1): undefined reference to `pthread_create'
main.cpp:(.text+0xd0e): undefined reference to `pthread_join'
Optimization.o: In function `my_df(gsl_vector const*, void*, gsl_vector*)':
Optimization.cpp:(.text+0x3149): undefined reference to `gsl_vector_alloc'
Optimization.cpp:(.text+0x316b): undefined reference to `gsl_vector_get'
Optimization.cpp:(.text+0x3180): undefined reference to `gsl_vector_set'
Optimization.cpp:(.text+0x31bc): undefined reference to `gsl_vector_get'
Optimization.cpp:(.text+0x31ec): undefined reference to `gsl_vector_set'
Optimization.cpp:(.text+0x323b): undefined reference to `gsl_vector_set'
Optimization.cpp:(.text+0x327e): undefined reference to `gsl_vector_set'
Optimization.cpp:(.text+0x32c9): undefined reference to `gsl_vector_set'
Optimization.o: In function `MHthreaded(void*)':
Optimization.cpp:(.text+0x3489): undefined reference to `gsl_rng_env_setup'
Optimization.cpp:(.text+0x3490): undefined reference to `gsl_rng_default'
Optimization.cpp:(.text+0x360b): undefined reference to `gsl_rng_uniform'
Optimization.cpp:(.text+0x3653): undefined reference to `gsl_ran_gaussian'
Optimization.cpp:(.text+0x3686): undefined reference to `gsl_ran_gaussian'
Optimization.cpp:(.text+0x377f): undefined reference to `gsl_rng_uniform'
RNGUtilities.o: In function `AllocRNG(gsl_rng*&, gsl_rng_type const*, int)':
RNGUtilities.cpp:(.text+0x9a): undefined reference to `gsl_rng_alloc'
RNGUtilities.cpp:(.text+0xba): undefined reference to `gsl_rng_set'
RNGUtilities.o: In function `FreeRNG(gsl_rng*&)':
RNGUtilities.cpp:(.text+0xe3): undefined reference to `gsl_rng_free'
collect2: ld returned 1 exit status
make: *** [CoalHMMgf] Error 1

and just to verify that gsl is installed in the local directory:

schraiber@trump:~/test_rsync$ ls ~/.local/lib
libbpp-core.a     libbpp-core.so.2.0.0  libbpp-seq.so.9      libgslcblas.a   libgslcblas.so.0      libgsl.so         pkgconfig
libbpp-core.so    libbpp-seq.a          libbpp-seq.so.9.1.0  libgslcblas.la  libgslcblas.so.0.0.0  libgsl.so.0       python2.7
libbpp-core.so.2  libbpp-seq.so         libgsl.a             libgslcblas.so  libgsl.la             libgsl.so.0.16.0
schraiber@trump:~/test_rsync$ ls ~/.local/include/
Bpp  gsl

An interesting thing to note is that my program is also supposed to link with Bpp, and it does that just fine as far as I can tell.


Solution

  • Your "-lfoo" parameters should come last on your "g++" command line. So, for example, make this change in your makefile:

    CoalHMMgf: main.o IOUtilities.o parameters.o Algorithms.o Optimization.o probabilities.o RNGUtilities.o Data.o ThreadData.h
            g++ $(CXXFLAGS) main.o IOUtilities.o parameters.o Algorithms.o Optimization.o probabilities.o RNGUtilities.o Data.o -o CoalHMMgf $(CXX_LDFLAGS)