Move and set the osqp-eigen library and eigen from 0 to 1

1 I have installed osqp-eigen in the default location on my computer now. Can you not install it? Just get it to /home/ubuntu/Code_Study/osqp_study/osqp-eigen on your computer and find it later. Find it here

You want to move the osqp-eigen library that is already installed in the default location to a custom location and ensure that subsequent CMake searches for the library can find it. Here are the steps you can follow:

  1. Copy related files:

    First make sure you know the installation path of osqp-eigen. If you’re not sure, usually header files are probably installed in /usr/local/include and libraries in /usr/local/lib. But it depends on your installation method and settings.

    Assuming you know the installation path, use the cp or mv command to copy or move the relevant header files and library files to /home/ubuntu/Code_Study/osqp_study/ osqp-eigen.

    mkdir -p /home/ubuntu/Code_Study/osqp_study/osqp-eigen/include
    mkdir -p /home/ubuntu/Code_Study/osqp_study/osqp-eigen/lib
    
    cp -r /path_to_default_location/include/osqp-eigen /home/ubuntu/Code_Study/osqp_study/osqp-eigen/include/
    cp -r /path_to_default_location/lib/libosqp-eigen* /home/ubuntu/Code_Study/osqp_study/osqp-eigen/lib/
    

    Note replacing /path_to_default_location with the default installation path of osqp-eigen.

  2. Update CMake to find libraries:

    In your CMakeLists.txt file, you need to set the paths to the header files and library files to ensure that CMake can find them.

    list(APPEND CMAKE_PREFIX_PATH "/home/ubuntu/Code_Study/osqp_study/osqp-eigen")
    
    find_path(OSQP_EIGEN_INCLUDE_DIR NAMES osqp-eigen.hpp PATHS "${CMAKE_PREFIX_PATH}/include")
    find_library(OSQP_EIGEN_LIBRARY NAMES osqp-eigen PATHS "${CMAKE_PREFIX_PATH}/lib")
    
    include_directories(${OSQP_EIGEN_INCLUDE_DIR})
    link_directories(${CMAKE_PREFIX_PATH}/lib)
    

    After you need to link against the target of osqp-eigen, use:

    target_link_libraries(your_target ${OSQP_EIGEN_LIBRARY})
    

    Here your_target is the name of the target you wish to link to.

  3. Compile and run:

    When you run CMake with your build tool (such as make), it should now look for osqp-eigen in the new location you specified.

This is a basic guideline and may need to be adapted to your specific situation.

2 I have installed osqp-eigen in the default location on my computer now. Can you not install it? Just get it from your computer to /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth. You can find it later. Find it here. You can find the header files and library files of osqp-eigen through commands, and then copy them

sure. You can use the find command to find the osqp-eigen header files and library files on your system. Then use the cp command to copy to the location you specify.

First, look for header and library files:

# Find header files
find /usr/local/include /usr/include -name "OsqpEigen*" 2>/dev/null

# Find library files
find /usr/local/lib /usr/lib -name "libOsqpEigen*" 2>/dev/null

Assuming you have found the paths to the header and library files, you can copy them using the following command:

# Create target directory
mkdir -p /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/include
mkdir -p /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/lib

#Copy header file
cp -r /path_to_osqp-eigen_header/ /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/include/

#Copy library files
cp /path_to_osqp-eigen_lib* /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/lib/

Note replacing /path_to_osqp-eigen_header/ and /path_to_osqp-eigen_lib with the actual paths you found in the previous find command.

Then, in your CMakeLists.txt, you can set CMAKE_PREFIX_PATH as I mentioned before to specify the location of the library.

Hope this helps!

3 Does OsqpEigen depend on Eigen3

cmake_minimum_required(VERSION 3.1)
set (CMAKE_CXX_STANDARD 11)
project(OsqpEigen-Example)
find_package(OsqpEigen)
find_package(Eigen3)
include_directories(SYSTEM ${
   <!-- -->EIGEN3_INCLUDE_DIR})
#MPCExample
add_executable(MPCExample src/MPCExample.cpp)
target_link_libraries(MPCExample OsqpEigen::OsqpEigen)

Yes, OsqpEigen is a library that provides a C++ interface to OSQP, a solver for quadratic programming problems, and it relies on Eigen3 for Linear algebra operations. Eigen3 is a C++ template library for linear algebra, matrix and vector operations, and related mathematical operations.

From the CMakeLists.txt you provided, it is obvious that the project searches and includes Eigen3 while searching for and including OsqpEigen.

In this line:

include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR})

It instructs CMake to include the header file directory for Eigen3.

In this line:

target_link_libraries(MPCExample OsqpEigen::OsqpEigen)

The project is linked with the OsqpEigen library. But please note that because Eigen3 is a header-only library, you do not need to link it in target_link_libraries. When you include its header file, all its functionality becomes available.

Overall, yes, OsqpEigen depends on Eigen3. If you use OsqpEigen in your project, you also need to ensure that Eigen3 is found and included correctly.

4 find_package(Eigen3 REQUIRED), can you output the file location of Eigen3, and then copy all Eigen3 to /home/ubuntu/Code_Study/osqp_study/

When you use find_package(Eigen3 REQUIRED) in CMake, CMake will use the configured module or configuration file to find Eigen3. If found, it will set some variables, the most critical of which is EIGEN3_INCLUDE_DIR, which usually points to the location of the Eigen3 header file.

To get the value of EIGEN3_INCLUDE_DIR on the command line, you can create a simple CMake script that outputs this information.

  1. Create a CMakeLists.txt file:
cmake_minimum_required(VERSION 3.1)
project(FindEigen3)
find_package(Eigen3 REQUIRED)
message(STATUS "Eigen3 include directory is: ${EIGEN3_INCLUDE_DIR}")
  1. Run CMake in a new directory:
mkdir eigen_finder
cd eigen_finder
cmake path_to_the_above_CMakeLists.txt

Make sure to replace path_to_the_above_CMakeLists.txt with the path to the CMakeLists.txt file you created.

  1. View CMake output:

In the CMake output, you should see lines similar to the following:

-- Eigen3 include directory is: /path/to/eigen3
  1. Copy Eigen3 to the target location:

After knowing the location of Eigen3’s header file, use the cp command to copy it:

cp -r /path/to/eigen3 /home/ubuntu/Code_Study/osqp_study/Eigen3

Make sure to replace /path/to/eigen3 with the actual path you found from the CMake output.

You should now have a directory called Eigen3 under /home/ubuntu/Code_Study/osqp_study/ that contains all Eigen3 header files.

5 Now I moved eigen3 from /usr/local/include/eigen3 to /home/ubuntu/Code_Study/osqp_study; then the header files and library files of OsqpEigen are in /home/ubuntu/Code_Study/osqp_study/osqp -eigen_smooth/include and /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/lib

Now that you have moved eigen3 and OsqpEigen to the specified directories, you need to make sure that when you use CMake to build the project, it can find these resources in these new locations.

Here are instructions on how to update your CMakeLists.txt to accommodate the new paths:

  1. Set Eigen3 header file path:

    In your CMakeLists.txt, set the header file path for Eigen3:

    set(EIGEN3_INCLUDE_DIR /home/ubuntu/Code_Study/osqp_study/eigen3)
    include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR})
    
  2. Set the OsqpEigen header file and library file paths:

    Likewise, in your CMakeLists.txt, set the header and library paths for OsqpEigen:

    set(OSQP_EIGEN_INCLUDE_DIR /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/include)
    set(OSQP_EIGEN_LIB_DIR /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/lib)
    
    include_directories(SYSTEM ${OSQP_EIGEN_INCLUDE_DIR})
    link_directories(${OSQP_EIGEN_LIB_DIR})
    
  3. Link OsqpEigen library:

    When you link libraries for your target, make sure to link the correct library files. Assuming your library name is still libOsqpEigen.so or similar, you can do this:

    target_link_libraries(MPCExample osqpEigen)
    

Overall, your CMakeLists.txt should now look similar to the following:

cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 11)
project(OsqpEigen-Example)

# Eigen3 path
set(EIGEN3_INCLUDE_DIR /home/ubuntu/Code_Study/osqp_study/eigen3)
include_directories(SYSTEM ${EIGEN3_INCLUDE_DIR})

#OsqpEigen paths
set(OSQP_EIGEN_INCLUDE_DIR /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/include)
set(OSQP_EIGEN_LIB_DIR /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/lib)
include_directories(SYSTEM ${OSQP_EIGEN_INCLUDE_DIR})
link_directories(${OSQP_EIGEN_LIB_DIR})

#MPCExample
add_executable(MPCExample src/MPCExample.cpp)
target_link_libraries(MPCExample osqpEigen)

After setting this up, CMake should be able to find the eigen3 and OsqpEigen resources in the new location you specified and build your project correctly.

6 Problems with the above osqp code

losqpEigen
collect2: error: ld returned 1 exit status
CMakeFiles/MPCExample.dir/build.make:96: recipe for target 'MPCExample' failed
make[2]: *** [MPCExample] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/MPCExample.dir/all' failed
make[1]: *** [CMakeFiles/MPCExample.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2

This error indicates that a problem occurred during the linking phase. Specifically, collect2: error: ld returned 1 exit status is a sign of a linker error. According to the information given, it is related to OsqpEigen.

To solve this problem we need to ensure the following:

  1. Make sure the library file exists: First, make sure there is indeed OsqpEigen/home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/lib path you mentioned. code> library file.

    ls /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/lib
    

    Check the output for a file like libOsqpEigen.so or libOsqpEigen.a.

  2. Correctly linked library files:

    In your CMakeLists.txt, make sure your target_link_libraries calls match your library filenames. For example, if your library file is named libOsqpEigen.so, you should link against OsqpEigen (note that CMake will automatically add the lib prefix).

    target_link_libraries(MPCExample osqpEigen)
    
  3. Possible dependency issues:

    OsqpEigen may depend on other libraries. You need to make sure you link all necessary libraries. For OsqpEigen, you may need to link against OSQP in addition to Eigen3 (which is a header-only library, so no linking is required) library. Make sure you find and link to that too.

  4. Detailed error message:

    The error message you provide is relatively brief, but link errors usually give a detailed description in the preceding lines. Check the error output for information about missing symbols or files, which can help identify the problem.

  5. Rebuild:

    Sometimes, simply building from scratch may do the trick. Try deleting all files in the build directory and re-running cmake and make.

  6. Check path:

    Now that you moved the libraries and header files, make sure all paths are set correctly. In particular, check that the paths to header and library files are set correctly in your CMakeLists.txt.

If none of the above suggestions solve the problem, please provide more details about the link error so I may be more helpful.

7 is still not resolved, but the error is more clear

/usr/bin/ld: CMakeFiles/MPCExample.dir/MPCExample.cpp.o: undefined reference to symbol 'csc_spalloc'
/usr/local/lib/libosqp.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
CMakeFiles/MPCExample.dir/build.make:96: recipe for target 'MPCExample' failed
make[2]: *** [MPCExample] Error 1
CMakeFiles/Makefile2:82: recipe for target 'CMakeFiles/MPCExample.dir/all' failed
make[1]: *** [CMakeFiles/MPCExample.dir/all] Error 2
Makefile:90: recipe for target 'all' failed
make: *** [all] Error 2

The error is now more explicit. undefined reference to symbol 'csc_spalloc' and /usr/local/lib/libosqp.so: error adding symbols: DSO missing from command line indicate that the linker is trying to link libosqp.so encountered a problem. In particular, it found no definition for the csc_spalloc symbol.

This usually means that you also need to link against other libraries that contain these missing symbols. In the case of OSQP, you may need to link against the linear algebra libraries it depends on.

Here’s how to solve this problem:

  1. Make sure you link the OSQP library:

    In your CMakeLists.txt , if you haven’t already done so, add a link to the OSQP library:

    target_link_libraries(MPCExample OsqpEigen OSQP::OSQP)
    

    Please note that the specific library names may vary depending on your system and how you installed OSQP. If it’s not OSQP::OSQP, it might just be OSQP.

  2. Link other necessary libraries:

    OSQP may depend on other linear algebra libraries, such as qdldl or others. Make sure you link against these libraries:

    target_link_libraries(MPCExample OsqpEigen OSQP::OSQP qdldl)
    

    Again, please modify the library name according to your system and installation.

  3. Make sure the library search paths are correct:

    Use the link_directories directive to make sure CMake knows where to find all library files:

    link_directories(/usr/local/lib) # or wherever libosqp.so is located
    

Rebuild your project after trying the above changes. If you’re still having trouble, please provide more details about your setup so I can provide more specific help.

8 All mine are .so files. Isn’t it good for you to use link?

If you are using a dynamic link library (.so file), you should indeed use target_link_libraries() for linking. When I mention link_directories, I’m making sure the linker knows where to find these .so files.

However, the DSO missing from command line error you mentioned indicates that you may be missing some necessary libraries. This is not a question about whether it is a .so file, but about ensuring that all necessary libraries are linked into your application.

Here are some suggestions:

  1. Make sure to link all necessary libraries:

    If your application depends on OsqpEigen, it may also indirectly depend on other libraries, such as OSQP or other libraries. You need to make sure you link against all these libraries.

    target_link_libraries(MPCExample OsqpEigen osqp ...)
    

    The ... here means that other libraries may need to be linked. Which ones are needed depends on the dependencies of OsqpEigen and OSQP.

  2. Check the name of the library file:

    Depending on your system and installation method, the name of the library file may be different. For example, it might be libosqp.so or libOsqpEigen.so. Make sure you use the correct name in target_link_libraries. Generally, you should omit the lib prefix and .so suffix, such as osqp.

  3. Explicitly specify the complete library path:

    You can also use the full path in target_link_libraries if you know the exact location of each library file. For example:

    target_link_libraries(MPCExample /path/to/libOsqpEigen.so /path/to/libosqp.so)
    
  4. Make sure your system knows where to find these libraries:

    At runtime, the operating system needs to know where to find the .so file. You can use the LD_LIBRARY_PATH environment variable to specify the search path:

    export LD_LIBRARY_PATH=/path/to/libraries:$LD_LIBRARY_PATH
    

    Alternatively, you may consider adding the paths to these libraries to /etc/ld.so.conf or related configuration files and running ldconfig.

Try these suggestions and see if they solve your problem. If you’re still having trouble, tell me more details and I’ll try to help you.

9 The sample code after the solution is completed is as follows:

cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 11)
project(OsqpEigen-Example)

# Eigen3 path
set(EIGEN3_INCLUDE_DIR /home/ubuntu/Code_Study/osqp_study/eigen3)
include_directories(${
   <!-- -->EIGEN3_INCLUDE_DIR})

#OsqpEigen paths
set(OSQP_EIGEN_INCLUDE_DIR /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/include)
set(OSQP_EIGEN_LIB_DIR /home/ubuntu/Code_Study/osqp_study/osqp-eigen_smooth/lib)
include_directories(${
   <!-- -->OSQP_EIGEN_INCLUDE_DIR})
link_directories(${
   <!-- -->OSQP_EIGEN_LIB_DIR})

# set the custom osqp paths
set(OSQP_CUSTOM_PATH "/home/ubuntu/Code_Study/osqp_study/osqp_path_smooth")
set(OSQP_INCLUDE_DIR "${OSQP_CUSTOM_PATH}/include")