Skip to content

modify code to allow shared object used independently and some minor changes #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 20 additions & 33 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,11 @@ ExternalProject_Add(
INSTALL_COMMAND cp ${CMAKE_BINARY_DIR}/external/src/qpoases/bin/libqpOASES.so ${CMAKE_INSTALL_PREFIX}/lib/libqpOASES.so
)

set(qpoases_lib "-L${CMAKE_BINARY_DIR}/lib -lqpOASES")
add_library(qpoases_lib SHARED IMPORTED)
set_target_properties(qpoases_lib PROPERTIES
IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/lib/${CMAKE_FIND_LIBRARY_PREFIXES}qpOASES${CMAKE_SHARED_LIBRARY_SUFFIX}
IMPORTED_NO_SONAME TRUE
Copy link
Author

@hwyao hwyao Mar 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment 1:
The IMPORTED_NO_SONAME TRUE for qpOASES is that, libqpOASES.so has no SONAME. This might be a reason that here we directly uses the Makefile instead of CMakeLists.txt provided by the repo.
(SONAME can be check with readelf -d <library-name> | head -10 to compare libosqp.so and libqpOASES.so)

> readelf -d libosqp.so | head -10
Dynamic section at offset 0x1ad90 contains 27 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libdl.so.2]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000e (SONAME)             Library soname: [libosqp.so]
 0x000000000000000c (INIT)               0x4000
> readelf -d libqpOASES.so | head -10
Dynamic section at offset 0x75d38 contains 27 entries:
  Tag        Type                         Name/Value
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libm.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libgcc_s.so.1]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]
 0x000000000000000c (INIT)               0x14000

When not using the IMPORTED_NO_SONAME, the runtime dynamic linking of the library is based on the relative path, instead of the rpath. Simply speaking, when you try to run the library / executable in a folder that is not the folder that you run make command, the loading will be failed.

We can inspect this by running the ldd over the built liblcqpow.so:
(no fixing) inside build folder:

>~/Git/LCQPow/build$ ldd ./lib/liblcqpow.so 
        linux-vdso.so.1 (0x00007ffc4808e000)
        lib/libqpOASES.so (0x00007f62c680d000)
        ...

(no fixing) in some other folder:

>~/Git/LCQPow/build/lib$ ldd ./liblcqpow.so 
        linux-vdso.so.1 (0x00007ffe1cbef000)
        lib/libqpOASES.so => not found

(fixed) everywhere:

>:~/Git/LCQPow/build/lib$ ldd ./liblcqpow.so 
        linux-vdso.so.1 (0x00007ffca27b1000)
        libqpOASES.so => <HOME FOLDER>/Git/LCQPow/build/lib/libqpOASES.so (0x00007f8eeeb6f000)

Some helpful references:
https://cmake.org/cmake/help/latest/prop_tgt/IMPORTED_NO_SONAME.html
https://stackoverflow.com/questions/10199045/c-linker-missing-library-when-running-soname-behavior
https://stackoverflow.com/questions/68164903/cmake-to-link-external-library-with-import-soname-ro-import-location

)
set(qpoases_include "${CMAKE_BINARY_DIR}/external/src/qpoases/include")

# 2) OSQP
Expand All @@ -203,7 +207,10 @@ ExternalProject_Add(
INSTALL_COMMAND cp ${CMAKE_BINARY_DIR}/external/src/osqp-build/out/libosqp.so ${CMAKE_INSTALL_PREFIX}/lib/libosqp.so
)

set(osqp_lib "-L${CMAKE_BINARY_DIR}/lib -losqp")
add_library(osqp_lib SHARED IMPORTED)
set_target_properties(osqp_lib PROPERTIES
IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/lib/${CMAKE_FIND_LIBRARY_PREFIXES}osqp${CMAKE_SHARED_LIBRARY_SUFFIX}
)
set(osqp_include "${CMAKE_BINARY_DIR}/external/src/osqp/include")

# 3) googletest
Expand Down Expand Up @@ -244,27 +251,13 @@ set_target_properties(
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
)

# Add the external projects as depenedncies for our project
add_dependencies(
${PROJECT_NAME}-static
qpoases
osqp
)

if (${QPOASES_SCHUR})
target_link_libraries(
${PROJECT_NAME}-static
PRIVATE ${Matlab_LIBRARIES}
)
endif()


# Add include directories of dependencies: qpOASES, OSQP
include_directories(${PROJECT_NAME}
SYSTEM ${qpoases_include}
SYSTEM ${osqp_include}
)

# create shared lib
add_library(${PROJECT_NAME}-shared SHARED ${SRC_FILES})
set_target_properties(
Expand All @@ -275,16 +268,17 @@ set_target_properties(
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
)

# Add the external projects as depenedncies for our project
add_dependencies(
${PROJECT_NAME}-shared
qpoases
osqp
target_include_directories(${PROJECT_NAME}-shared SYSTEM
PUBLIC ${qpoases_include}
PUBLIC ${osqp_include}
)
target_link_libraries(${PROJECT_NAME}-shared
PUBLIC qpoases_lib
PUBLIC osqp_lib
)

if (${QPOASES_SCHUR})
target_link_libraries(
${PROJECT_NAME}-shared
target_link_libraries(${PROJECT_NAME}-shared
PRIVATE ${Matlab_LIBRARIES}
)

Expand Down Expand Up @@ -318,7 +312,8 @@ if (${BUILD_EXAMPLES})
target_link_libraries(
${EXAMPLE_NAME}
PUBLIC ${PROJECT_NAME}-shared
PRIVATE ${qpoases_lib} ${osqp_lib}
#PUBLIC ${PROJECT_NAME}-static
#PRIVATE qpoases_lib osqp_lib
)

if (${QPOASES_SCHUR})
Expand Down Expand Up @@ -484,13 +479,6 @@ if (${UNIT_TESTS})
# Add Unit testing source file
add_executable(RunUnitTests test/RunUnitTests.cpp)

add_dependencies(
RunUnitTests
qpoases
osqp
gtest
)

set_target_properties(
RunUnitTests
PROPERTIES
Expand All @@ -501,7 +489,7 @@ if (${UNIT_TESTS})
target_link_libraries(
RunUnitTests
PUBLIC ${PROJECT_NAME}-shared
PRIVATE ${qpoases_lib} ${osqp_lib} ${gtest_lib} ${LIB_SOLVER}
PRIVATE ${gtest_lib} ${LIB_SOLVER}
PRIVATE -lpthread
)

Expand All @@ -522,7 +510,6 @@ if (${UNIT_TESTS})
target_link_libraries(
${EXAMPLE_TEST_NAME}
PUBLIC ${PROJECT_NAME}-shared
PRIVATE ${qpoases_lib} ${osqp_lib}
)

if (${QPOASES_SCHUR})
Expand Down