kthohr / mcmc

A C++ library of Markov Chain Monte Carlo (MCMC) methods
https://mcmclib.readthedocs.io/en/latest/
Apache License 2.0
166 stars 45 forks source link

error compiling the sample code #1

Closed grandtiger closed 6 years ago

grandtiger commented 6 years ago

I put the sample code into mcmc_sample.cpp and tried to compile it with the following command:

g++ -std=c++11 -Wall -fPIC -O3 -march=native -ffp-contract=fast -flto -DARMA_NO_DEBUG -I./include -lmcmc mcmc_sample.cpp

but kept getting the following error:

/tmp/ccpHtc3q.ltrans0.ltrans.o: In function `main':
<artificial>:(.text.startup+0x5cc): undefined reference to `mcmc::rwmh(arma::Col<double> const&, arma::Mat<double>&, std::function<double (arma::Col<double> const&, void*)>, void*)'
collect2: error: ld returned 1 exit status

Following is the info about my ubuntu linux and g++.

lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.4 LTS
Release:        16.04
Codename:       xenial

g++ --version
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

What is the minimum version of g++ required by this project? Thanks!

kthohr commented 6 years ago

Looks like a linking issue; where did you install the actual library?

The library and test files should build with g++ 5, 6, and 7 (per Travis) and clang 5 and 6.

grandtiger commented 6 years ago

@kthohr I simply followed your instruction:

# clone mcmc into the current directory
git clone https://github.com/kthohr/mcmc ./mcmc
# build and install
cd ./mcmc
./configure
make
sudo make install

It's installed to /usr/local/lib. I also did

export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

What else did I miss? Thanks a lot for looking into this.

grandtiger commented 6 years ago

@kthohr BTW, I am trying out Ubuntu 18.04 beta and it has gcc 7.3.0. I changed the command to the following, and it compiled successfully.

g++ -std=c++11 -fPIC -O3 -march=native -ffp-contract=fast -flto -DARMA_NO_DEBUG -fPIC -shared -I./include -L/usr/local/lib mcmc_sample.cpp 

However, when I tried to run it, I got

./a.out 
Segmentation fault (core dumped)

How can I get more information about the root cause of the error?

BTW, what is your development environment? I would like to use virtual machine to mimic your development environment and see if I can get the sample working. Thanks again for any help you may render!

kthohr commented 6 years ago

You haven't actually linked to libmcmc with this line:

g++ -std=c++11 -fPIC -O3 -march=native -ffp-contract=fast -flto -DARMA_NO_DEBUG -fPIC -shared -I./include -L/usr/local/lib mcmc_sample.cpp 

I just changed two of the test build scripts; let's see if the test suite will run your system. Can you change directory to where you cloned the library and pull the changes:

cd ./mcmc
git pull

Then run (from the same directory):

./configure 
make clean 
make
sudo make install
cd ./tests/unit_tests
./configure
make

Works on my Linux machine (distro: Fedora 27).

grandtiger commented 6 years ago

@kthohr Here is what I got. Does it match your output?

george@ubuntu:~/GitHub/mcmc/tests/unit_tests$ make
g++ -std=c++11 -Wall -O3 -march=native -ffp-contract=fast -DARMA_NO_DEBUG -I/home/george/GitHub/mcmc/tests/unit_tests/../../include -I. log_jacobian.cpp -o log_jacobian.test -L/usr/local/lib -lmcmc -lblas -llapack
log_jacobian.cpp: In function ‘int main()’:
log_jacobian.cpp:48:12: warning: unused variable ‘lj_val’ [-Wunused-variable]
     double lj_val = mcmc::log_jacobian(vals_trans,bounds_type,lb,ub);
            ^~~~~~
g++ -std=c++11 -Wall -O3 -march=native -ffp-contract=fast -DARMA_NO_DEBUG -I/home/george/GitHub/mcmc/tests/unit_tests/../../include -I. aees.cpp -o aees.test -L/usr/local/lib -lmcmc -lblas -llapack
g++ -std=c++11 -Wall -O3 -march=native -ffp-contract=fast -DARMA_NO_DEBUG -I/home/george/GitHub/mcmc/tests/unit_tests/../../include -I. mala.cpp -o mala.test -L/usr/local/lib -lmcmc -lblas -llapack
g++ -std=c++11 -Wall -O3 -march=native -ffp-contract=fast -DARMA_NO_DEBUG -I/home/george/GitHub/mcmc/tests/unit_tests/../../include -I. determine_bounds_type.cpp -o determine_bounds_type.test -L/usr/local/lib -lmcmc -lblas -llapack
g++ -std=c++11 -Wall -O3 -march=native -ffp-contract=fast -DARMA_NO_DEBUG -I/home/george/GitHub/mcmc/tests/unit_tests/../../include -I. rmhmc.cpp -o rmhmc.test -L/usr/local/lib -lmcmc -lblas -llapack
g++ -std=c++11 -Wall -O3 -march=native -ffp-contract=fast -DARMA_NO_DEBUG -I/home/george/GitHub/mcmc/tests/unit_tests/../../include -I. de.cpp -o de.test -L/usr/local/lib -lmcmc -lblas -llapack
g++ -std=c++11 -Wall -O3 -march=native -ffp-contract=fast -DARMA_NO_DEBUG -I/home/george/GitHub/mcmc/tests/unit_tests/../../include -I. rwmh.cpp -o rwmh.test -L/usr/local/lib -lmcmc -lblas -llapack
g++ -std=c++11 -Wall -O3 -march=native -ffp-contract=fast -DARMA_NO_DEBUG -I/home/george/GitHub/mcmc/tests/unit_tests/../../include -I. transform_vals.cpp -o transform_vals.test -L/usr/local/lib -lmcmc -lblas -llapack
g++ -std=c++11 -Wall -O3 -march=native -ffp-contract=fast -DARMA_NO_DEBUG -I/home/george/GitHub/mcmc/tests/unit_tests/../../include -I. hmc.cpp -o hmc.test -L/usr/local/lib -lmcmc -lblas -llapack
grandtiger commented 6 years ago

@kthohr looking at the test output, I changed the command to:

g++ -std=c++11 -Wall -O3 -march=native -ffp-contract=fast -DARMA_NO_DEBUG -I/home/george/GitHub/mcmc/tests/unit_tests/../../include -I. mcmc_sample.cpp -L/usr/local/lib -lmcmc -lblas -llapack

it compiles, and I can run it ./a.out and there is no more core dump. I added some output to the sample, and it seems to work now. Thanks a lot for pointing me to the right direction.

kthohr commented 6 years ago

Great, happy it worked.