Skip to content

Commit 640d32a

Browse files
committed
fix dynamic linking of sod
1 parent 51968e7 commit 640d32a

File tree

5 files changed

+388
-533
lines changed

5 files changed

+388
-533
lines changed

CMakeLists.txt

+46-39
Original file line numberDiff line numberDiff line change
@@ -98,39 +98,19 @@ endif()
9898

9999
# Set up SOD library if enabled
100100
if(ENABLE_SOD)
101-
# Only add the SOD subdirectory if we're doing static linking
102-
if(NOT SOD_DYNAMIC_LINK)
103-
add_subdirectory(src/sod)
104-
endif()
101+
# Add the SOD subdirectory regardless of linking method
102+
# The CMakeLists.txt in the SOD directory will handle
103+
# building as static or shared based on SOD_DYNAMIC_LINK
104+
add_subdirectory(src/sod)
105105

106106
# Add the SOD_ENABLED define regardless of linking method
107107
add_definitions(-DSOD_ENABLED)
108108

109109
# Always set the include directory
110110
set(SOD_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/sod")
111111

112-
# For dynamic linking, we need to find the SOD library in the system
112+
# If dynamic linking is enabled, add the define
113113
if(SOD_DYNAMIC_LINK)
114-
# First try pkg-config
115-
pkg_check_modules(SOD sod)
116-
117-
# If not found via pkg-config, try find_library
118-
if(NOT SOD_FOUND)
119-
find_library(SOD_LIBRARY NAMES sod)
120-
if(SOD_LIBRARY)
121-
set(SOD_LIBRARIES ${SOD_LIBRARY})
122-
set(SOD_FOUND TRUE)
123-
message(STATUS "Found SOD library: ${SOD_LIBRARY}")
124-
endif()
125-
endif()
126-
127-
# If still not found, warn but continue as the code will fall back to dynamic loading
128-
if(NOT SOD_FOUND)
129-
message(WARNING "SOD library not found for dynamic linking. "
130-
"The application will attempt to load it at runtime.")
131-
endif()
132-
133-
# Add a define to indicate dynamic linking should be used
134114
add_definitions(-DSOD_DYNAMIC_LINK)
135115
endif()
136116
endif()
@@ -250,6 +230,11 @@ endif()
250230
# Define the executable
251231
add_executable(lightnvr ${SOURCES})
252232

233+
# Set the output directory for the binary
234+
set_target_properties(lightnvr PROPERTIES
235+
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
236+
)
237+
253238
# Link libraries
254239
target_link_libraries(lightnvr
255240
${FFMPEG_LIBRARIES}
@@ -268,17 +253,42 @@ endif()
268253

269254
# Link SOD library if enabled
270255
if(ENABLE_SOD)
271-
if(SOD_DYNAMIC_LINK AND SOD_FOUND)
272-
# For dynamic linking, link against the found SOD library
273-
target_link_libraries(lightnvr ${SOD_LIBRARIES})
274-
message(STATUS "Using dynamic linking for SOD library")
275-
elseif(NOT SOD_DYNAMIC_LINK)
276-
# For static linking, link against our built SOD library
277-
target_link_libraries(lightnvr sod)
278-
message(STATUS "Using static linking for SOD library")
256+
# Always link to the sod target, whether it's built as static or shared
257+
target_link_libraries(lightnvr sod)
258+
259+
# Log the linking method for clarity
260+
if(SOD_DYNAMIC_LINK)
261+
message(STATUS "Using dynamic linking for SOD library (built from source)")
262+
263+
# Set proper RPATH settings for the main executable when using dynamic SOD
264+
set_target_properties(lightnvr PROPERTIES
265+
BUILD_WITH_INSTALL_RPATH TRUE
266+
INSTALL_RPATH "$ORIGIN/../src/sod:$ORIGIN/../lib"
267+
)
268+
269+
# Make sure the shared library gets installed to lib directory
270+
install(TARGETS sod
271+
LIBRARY DESTINATION lib
272+
RUNTIME DESTINATION bin
273+
)
274+
275+
# Copy the SOD shared library to a directory next to the executable
276+
add_custom_command(TARGET lightnvr POST_BUILD
277+
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/lib"
278+
COMMAND ${CMAKE_COMMAND} -E copy
279+
"$<TARGET_FILE:sod>"
280+
"${CMAKE_BINARY_DIR}/lib/"
281+
COMMENT "Copying SOD shared library to lib directory"
282+
)
283+
284+
# Additional direct RPATH via linker flags for older systems
285+
if(UNIX AND NOT APPLE)
286+
set_target_properties(lightnvr PROPERTIES
287+
LINK_FLAGS "-Wl,-rpath,\$ORIGIN/../src/sod:\$ORIGIN/../lib"
288+
)
289+
endif()
279290
else()
280-
# SOD will be loaded at runtime
281-
message(STATUS "SOD will be loaded at runtime via dlopen()")
291+
message(STATUS "Using static linking for SOD library")
282292
endif()
283293
endif()
284294

@@ -293,9 +303,6 @@ if(BUILD_TESTS)
293303
add_subdirectory(tests)
294304
endif()
295305

296-
# Output binary to a 'bin' directory
297-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
298-
299306
# Create a version.h file
300307
configure_file(
301308
${CMAKE_CURRENT_SOURCE_DIR}/include/core/version.h.in
@@ -317,4 +324,4 @@ message(STATUS "- Include directories:")
317324
foreach(dir ${LIGHTNVR_INCLUDE_DIRS})
318325
message(STATUS " * ${dir}")
319326
endforeach()
320-
message(STATUS "Ensure all dependencies are optimized for low memory usage")
327+
message(STATUS "Ensure all dependencies are optimized for low memory usage")

scripts/build.sh

+33-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ while [[ $# -gt 0 ]]; do
5959
echo " --clean Clean build directory before building"
6060
echo " --with-sod Build with SOD support (default)"
6161
echo " --without-sod Build without SOD support"
62-
echo " --sod-dynamic Use dynamic linking for SOD"
62+
echo " --sod-dynamic Use dynamic linking for SOD (builds libsod.so)"
6363
echo " --sod-static Use static linking for SOD (default)"
6464
echo " --with-tests Build test suite (default)"
6565
echo " --without-tests Build without test suite"
@@ -74,6 +74,10 @@ while [[ $# -gt 0 ]]; do
7474
esac
7575
done
7676

77+
78+
# Add this to your build script before building
79+
export LD_LIBRARY_PATH="$PWD/build/Release/src/sod:$LD_LIBRARY_PATH"
80+
7781
# Set build directory
7882
BUILD_DIR="build/$BUILD_TYPE"
7983

@@ -100,10 +104,10 @@ SOD_OPTION=""
100104
if [ "$ENABLE_SOD" -eq 1 ]; then
101105
if [ "$SOD_DYNAMIC" -eq 1 ]; then
102106
SOD_OPTION="-DENABLE_SOD=ON -DSOD_DYNAMIC_LINK=ON"
103-
echo "Building with SOD support (dynamic linking)"
107+
echo "Building with SOD support as a shared library (dynamic linking)"
104108
else
105109
SOD_OPTION="-DENABLE_SOD=ON -DSOD_DYNAMIC_LINK=OFF"
106-
echo "Building with SOD support (static linking)"
110+
echo "Building with SOD support as a static library (static linking)"
107111
fi
108112
else
109113
SOD_OPTION="-DENABLE_SOD=OFF"
@@ -217,6 +221,31 @@ cmake --build "$BUILD_DIR" -- -j$(nproc)
217221
echo "Build completed successfully!"
218222
echo "Binary location: $BUILD_DIR/bin/lightnvr"
219223

224+
# Check if the binary is linked to libsod.so when dynamic linking is enabled
225+
if [ "$ENABLE_SOD" -eq 1 ] && [ "$SOD_DYNAMIC" -eq 1 ]; then
226+
echo "Checking if lightnvr is linked to libsod.so..."
227+
if ldd "$BUILD_DIR/bin/lightnvr" | grep -q "libsod.so"; then
228+
echo "SUCCESS: lightnvr is correctly linked to libsod.so"
229+
else
230+
echo "WARNING: lightnvr is not linked to libsod.so"
231+
echo "This might indicate a problem with the dynamic linking setup."
232+
233+
# Check if libsod.so exists in the build output
234+
echo "Checking for libsod.so in the build directory..."
235+
if find "$BUILD_DIR" -name "libsod.so" | grep -q .; then
236+
echo "Found libsod.so in the build directory. It might not be in the library path."
237+
LIBSOD_PATH=$(find "$BUILD_DIR" -name "libsod.so" | head -1)
238+
239+
# Try to load the library directly
240+
echo "You can run the application with:"
241+
echo "LD_LIBRARY_PATH=\"$(dirname \"$LIBSOD_PATH\"):$LD_LIBRARY_PATH\" $BUILD_DIR/bin/lightnvr"
242+
else
243+
echo "Could not find libsod.so in the build directory."
244+
echo "This suggests a problem with building the shared library."
245+
fi
246+
fi
247+
fi
248+
220249
# Look for test binaries
221250
if [ "$ENABLE_TESTS" -eq 1 ] && [ "$ENABLE_SOD" -eq 1 ]; then
222251
if [ -f "$BUILD_DIR/bin/test_sod_realnet" ]; then
@@ -234,4 +263,4 @@ fi
234263
if [ -f "$BUILD_DIR/bin/lightnvr" ]; then
235264
ln -sf "$BUILD_DIR/bin/lightnvr" lightnvr
236265
echo "Created symbolic link: lightnvr -> $BUILD_DIR/bin/lightnvr"
237-
fi
266+
fi

0 commit comments

Comments
 (0)