The following is a dummy-proof guide to install meshmonk on your ubuntu 16.04 machine.
sudo apt install git
Because matlab only supports gcc's and g++'s earlier version (not the 5.x versions), we'll have to downgrade. Below we'll give an easy (but permanent) way to do that. If you want to be able to switch versions on the fly, however, follow this post.
First, we'll remove the symbolic links of gcc and g++ to the 5.x versions, we'll install the older versions, and relink gcc and g++ symbols to those older versions:
- Check which versions you have. If they are 4.9, you can skip the other steps:
gcc -v
g++ -v
- Remove the symbolic links:
sudo rm /usr/bin/gcc
sudo rm /usr/bin/g++
- Install the older versions:
sudo apt-get install gcc-4.9
sudo apt-get install g++-4.9
- Set symbolic links to the right binaries
sudo ln -s /usr/bin/gcc-4.9 /usr/bin/gcc
sudo ln -s /usr/bin/g++-4.9 /usr/bin/g++
- Check if the versions now are indeed 4.9
gcc -v
g++ -v
We'll use an IDE to make compiling the code easier.
sudo add-apt-repository ppa:damien-moore/codeblocks-stable
sudo apt-get update
sudo apt-get install codeblocks codeblocks-contrib
The meshmonk library depends on Eigen, nanoflann, and OpenMesh. We'll install these one by one, which typically requires:
- Getting the source code
- Compiling it (in OpenMesh's case)
- Putting the header files and compiled libs in the right directory
- Making sure those directories are searched by the compiler/linker during the compiling/linking process.
Note: if you want to learn more about how libraries work and how to use them, we highly recommend David Wheeler's How-To. Some other useful sources are:
- How header files matter for shared libraries: This stackoverflow answer
Eigen is a header-only library, which makes it very easy to use in your own projects. The only thing you have to do is download the header files and put them in a directory which your compiler searches so that it finds whatever it needs from the Eigen library.
- Go to their webpage and download the latest stable release (3.3.3 at moment of writing). It will end up as an archive in your
/home/user/Downloads/
folder. - Extract it locally.
- Move the Eigen subfolder (which contains all the headers) to /usr/local/include/ so that you end up with /usr/local/include/Eigen/:
sudo mv /home/user/Downloads/eigen-eigen-67e894c6cd8f/Eigen /usr/local/include/
Like Eigen, nanoflann is also a header-only library. There is only one header file you need, so we deliver that in the meshmonk repository under '/vendor' directory. So all that's left is to copy that to /usr/local/include:
sudo cp /home/user/projects/meshmonk/vendor/nanoflann.hpp /usr/local/include/
OpenMesh can be compiled and used as both a static (.a) and shared library (.so). We'll download the source, configure build settings with 'cmake', and build it using 'make'. After all that's happened, we move the compiled library files to /usr/local/lib/ and the header files to /usr/local/include/. And finally, we'll have to update some configurations so that these library files will be found when compiling/linking to it later.
- Download the latest version of the OpenMesh sourcefiles from their downloads page (the .tar.gz or .tar.bz2 file)
- Extract it locally
- Go into the extracted folder and make a new folder inside called 'build'
- In your terminal, switch to the newly created 'build' folder:
cd /home/user/Downloads/OpenMesh-6.3/build/
- Configure the makefiles using cmake:
cmake ..
(include the two dots!) - Build the library:
make
- Move the all the compiled library files to /usr/local/lib/:
sudo mv /home/user/Downloads/OpenMesh-6.3/build/Build/lib/* /usr/local/lib/
- Move the folder with the header files we need to /usr/local/include/:
sudo mv /home/user/Downloads/OpenMesh-6.3/src/OpenMesh/ /usr/local/include/
- Run ldconfig so that your library loader can find it when running an application that needs the library:
sudo ldconfig -v
. To check, runldconfig -p | grep OpenMesh
and it should print a few library names containing OpenMeshCore and OpenMeshTools
First, clone the meshmonk repository:
- Make a folder 'projects' in home (
/home/user/projects/
) - Go into the folder:
cd /home/user/projects/
- From inside the projects directory, clone the online repository:
git clone https://github.com/TheWebMonks/meshmonk.git
As an alternative to this, and the next step, you may use the precompiled version of the meshmonk library in the 'builds' folder.
Next, let's compile MeshMonk using Code::Blocks.
- Select 'Create a new project' and choose 'Shared library'
- Choose C++
- Fill in the project form:
- Project title: 'meshmonk'
- Folder to create project in: '/home/user/projects'
- The other fields are filled in automatically, just verify the 'Resulting filename' equals '/home/user/projects/meshmonk/meshmonk.cbp'
- Set configurations:
- Compiler: 'GNU GCC Compiler'
- Tick both the Debug and Release box
After clicking 'Finish', the meshmonk project is opened automatically. We're going to set some compiler/linker options now:
- Right-click the project (called 'meshmonk' with the symbol of Code::Blocks in front of it) and select 'Build options...'
- In the 'Compiler settings' tab, select the 'Compiler Flags' subtab. Tick off 'Produce debugging symbols' and tick on the 'Optimize even more (for speed) [-O2]' option.
- In the 'Compiler settings' tab, select the 'Other compiler options' and write
-std=c++14 -Wl,-V -fPIC
in the text field.
Note that you have to change these Compiler settings for both the Debug and Release build! (see the leftside pane in the 'Build options' window).
Now let's make sure the build target has the correct name. We want the final compiled product to be named 'libmeshmonk.so'. So right-click the project again but this time select 'Properties'. Go to the 'Build targets' tab. For both the Release and Debug build, ensure the 'Output filename' field is respectively bin/Debug/libmeshmonk.so and bin/Release/libmeshmonk.so. (It might be named 'liblibmeshmonk.so' initially, which would not give us what we need).
One big thing is still missing from the project, namely the sources themselves! Delete the current main.cpp that is in the meshmonk project (which was automatically generated but we're gonna use our own sources). Now add the sources:
- Right-click the meshmonk project and select 'Add files recursively...'. Choose the
/home/user/projects/meshmonk/src
folder that you obtained by cloning the repository earlier. - Right-click the meshmonk project and select 'Add files...'. Choose the meshmonk.hpp and meshmonk.cpp files located in the
home/user/projects/meshmonk/
folder.
Now, compile the code by clicking the small yellow cog in the top toolbar ('Build'). Make sure the version is set to 'Release' and not 'Debug' (should be to the right of the build buttons).
Code::Blocks will print a lot of output, including warnings (in blue). Don't worry about those.
If you started building without changing the 'other compiler options' first, you'll have to 'Rebuild' the meshmonk library after changing the compiler options instead of 'Build'.
Now go into the home/user/projects/meshmonk/meshmonk/bin/Release/ folder and you should see the resulting .so file, which is the shared library we just compiled. Make sure the name is 'libmeshmonk.so'. If that's not the case, simply change the name back to 'libmeshmonk.so' yourself.
Now that you've compiled everything, we're going to put the library files in the right places so that other applications can access them:
- Copy the shared library 'libmeshmonk.so' to /usr/local/lib/:
sudo cp /home/user/projects/meshmonk/bin/Release/libmeshmonk.so /usr/local/lib/
- Copy the header files to /usr/local/include/:
(cd /home/user/projects/meshmonk/ && find . -name '*.hpp' -print | tar --create --files-from -) | (cd /usr/local/include/ && sudo tar xvfp -)
- Run ldconfig to update your library list:
sudo ldconfig -v
Note(!): every time you recompile, don't forget you have to copy the latest shared library to /usr/local/lib/!