Attention
This is older information related to ROS2 Humble. Newer versions, such as Jazzy, are part of the ongoing versions of this tutorial.
Install a CMake package without sudo privileges
To install a CMake package or library without sudo privileges, we need to define a directory to which we have access. For instance,
in ~/.
Create a custom folder
In this tutorial, we are going to create a custom folder ~/opt containing
the folders lib and include. This will be our directory to install all our CMake packages.
Run the following commands,
cd ~/
mkdir -p opt && cd opt
mkdir -p include
mkdir -p lib
Then, we update the LD_LIBRARY_PATH, LIBRARY_PATH, and CPATH in ~/.bashrc.
Do the following just once, so that all terminal windows automatically source this new workspace for you.
echo "# Update the environment variable LD_LIBRARY_PATH to include ~/opt/lib, as instructed in https://ros2-tutorial.readthedocs.io" >> ~/.bashrc
echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/opt/lib" >> ~/.bashrc
echo "# Update the environment variable LIBRARY_PATH to include ~/opt/lib, as instructed in https://ros2-tutorial.readthedocs.io" >> ~/.bashrc
echo "export LIBRARY_PATH=$LIBRARY_PATH:~/opt/lib" >> ~/.bashrc
echo "# Update the environment variable CPATH to include ~/opt/include, as instructed in https://ros2-tutorial.readthedocs.io" >> ~/.bashrc
echo "export CPATH=$CPATH:~/opt/include" >> ~/.bashrc
source ~/.bashrc
Install a CMake package
To install a CMake package, we set the CMAKE_INSTALL_PREFIX:PATH flag with our custom folder (/home/USERNAME/opt)
cmake -DCMAKE_INSTALL_PREFIX:PATH=~/opt ..
make
make install
Example: Installing qpOASES
This example shows how to build and install the qpOASES to be used in your CMake project.
Note
Check the official qpOASES documentation for more details.
Warning
This example assumes you have git, CMake, Eigen, and a C++ compiler installed in your GNU/Linux distribution.
To install qpOASES as a shared library, we use the instructions provided by the DQ Robotics in cpp-interface-qpoases specifying the installation directory.
cd ~/Downloads
git clone https://github.com/coin-or/qpOASES.git
cd qpOASES
sed -i -e 's/option(BUILD_SHARED_LIBS "If ON, build shared library instead of static" OFF)/option(BUILD_SHARED_LIBS "If ON, build shared library instead of static" ON)/g' CMakeLists.txt
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=~/opt
make
make install
Example: include and link the qpOASES in your project
1cmake_minimum_required(VERSION 3.5)
2
3project(test_proxsuite)
4
5set(CMAKE_CXX_STANDARD 20)
6
7FIND_PACKAGE(Eigen3 REQUIRED)
8INCLUDE_DIRECTORIES(${EIGEN3_INCLUDE_DIR})
9ADD_COMPILE_OPTIONS(-Werror=return-type
10 -Wall -Wextra -Wmissing-declarations
11 -Wredundant-decls -Woverloaded-virtual)
12
13add_executable(${PROJECT_NAME}
14 ${PROJECT_NAME}.cpp)
15
16target_link_libraries(${PROJECT_NAME}
17 qpOASES
18)
1/*
2 * This is an official example provided by qpOASES
3 * https://github.com/coin-or/qpOASES/blob/master/examples/example1.cpp
4*/
5
6#include <qpOASES.hpp>
7
8/** Example for qpOASES main function using the QProblem class. */
9int main( )
10{
11 USING_NAMESPACE_QPOASES
12
13 /* Setup data of first QP. */
14 real_t H[2*2] = { 1.0, 0.0, 0.0, 0.5 };
15 real_t A[1*2] = { 1.0, 1.0 };
16 real_t g[2] = { 1.5, 1.0 };
17 real_t lb[2] = { 0.5, -2.0 };
18 real_t ub[2] = { 5.0, 2.0 };
19 real_t lbA[1] = { -1.0 };
20 real_t ubA[1] = { 2.0 };
21
22 /* Setup data of second QP. */
23 real_t g_new[2] = { 1.0, 1.5 };
24 real_t lb_new[2] = { 0.0, -1.0 };
25 real_t ub_new[2] = { 5.0, -0.5 };
26 real_t lbA_new[1] = { -2.0 };
27 real_t ubA_new[1] = { 1.0 };
28
29
30 /* Setting up QProblem object. */
31 QProblem example( 2,1 );
32
33 Options options;
34 example.setOptions( options );
35
36 /* Solve first QP. */
37 int_t nWSR = 10;
38 example.init( H,g,A,lb,ub,lbA,ubA, nWSR );
39
40 /* Get and print solution of first QP. */
41 real_t xOpt[2];
42 real_t yOpt[2+1];
43 example.getPrimalSolution( xOpt );
44 example.getDualSolution( yOpt );
45 printf( "\nxOpt = [ %e, %e ]; yOpt = [ %e, %e, %e ]; objVal = %e\n\n",
46 xOpt[0],xOpt[1],yOpt[0],yOpt[1],yOpt[2],example.getObjVal() );
47
48 /* Solve second QP. */
49 nWSR = 10;
50 example.hotstart( g_new,lb_new,ub_new,lbA_new,ubA_new, nWSR );
51
52 /* Get and print solution of second QP. */
53 example.getPrimalSolution( xOpt );
54 example.getDualSolution( yOpt );
55 printf( "\nxOpt = [ %e, %e ]; yOpt = [ %e, %e, %e ]; objVal = %e\n\n",
56 xOpt[0],xOpt[1],yOpt[0],yOpt[1],yOpt[2],example.getObjVal() );
57
58 example.printOptions();
59 /*example.printProperties();*/
60
61 /*getGlobalMessageHandler()->listAllMessages();*/
62
63 return 0;
64}
Warning
If you have the library installed in two directories, you need to ensure you are linking the library you want.
For instance, let’s say you have the DQ Robotics library installed globally (i.e., /usr/local/lib/) and locally (i.e., ~/opt/lib),
and you want to use the local one. Then, you can use find_library with the NO_DEFAULT_PATH flag.
1find_library(my_local_dqrobotics
2 NAMES dqrobotics
3 PATHS ~/opt/lib
4 NO_DEFAULT_PATH
5 )
6if (my_local_dqrobotics)
7 message(STATUS "Local DQ Robotics installed in ${my_local_dqrobotics}")
8target_link_libraries(${PROJECT_NAME}
9 ${my_local_dqrobotics})
10else()
11 message(FATAL_ERROR "Local DQ Robotics not found!")
12endif()