8.6.2. Static vs Dynamic Linking - 5.2 English - 57404

AOCL User Guide (57404)

Document ID
57404
Release Date
2025-12-29
Version
5.2 English

Dynamic (Shared) Library Linking (Recommended):

Advantages:

  • Smaller executable size

  • Easier updates (just replace the .so file)

  • No special linking flags needed

  • Simpler build configuration

Static Library Linking:

Warning

CRITICAL: Static linking requires the --whole-archive flag.

AOCL-DLP uses static registration via constructor functions to automatically register optimized kernels and JIT generators at program startup. Without --whole-archive, the linker will discard object files containing only static constructors, resulting in:

  • Missing JIT kernels

  • Degraded performance (up to 10-100x slower)

  • Fallback to reference implementations

CMake Example (Static Linking):

find_package(AoclDlp REQUIRED)
add_executable(my_app main.c)

# Method 1: Using CMake's LINK_LIBRARY (CMake 3.24+)
target_link_libraries(my_app PRIVATE
    $<LINK_LIBRARY:WHOLE_ARCHIVE,AoclDlp::aocl-dlp_static>
)

# Method 2: Manual linker flags (for older CMake)
target_link_libraries(my_app PRIVATE
    -Wl,--whole-archive
    AoclDlp::aocl-dlp_static
    -Wl,--no-whole-archive
)

Manual Example (Static Linking):

gcc -o my_app main.c \
    -I/usr/local/include \
    -L/usr/local/lib \
    -Wl,--whole-archive -laocl-dlp_static -Wl,--no-whole-archive \
    -lpthread -lm -lstdc++ -fopenmp

Verification:

Verify that static constructors were included:

# Check for JIT generator symbols
nm my_app | grep -i "jit.*register"

# Check for kernel registration symbols
nm my_app | grep -i "kernel.*register"

# If you see multiple matches, static linking is working correctly