Skip to content

cc_library failed to merge multiple static libraries into one on Linux #4321

@Xreki

Description

@Xreki

To create a single c-api static library, we need to merge all the needed libraries, including

  • paddle_capi
  • paddle_utils
  • paddle_parameter
  • paddle_math
  • paddle_cuda
  • paddle_function
  • paddle_gserver
  • paddle_proto
  • paddle_pserver
  • paddle_network

It was originally implemented by following codes:

# combine all paddle static libraries together, into libpaddle_capi_whole.a
# user should use PaddleCAPI as -lpaddle_capi_whole
set(capi_whole_library libpaddle_capi_whole.a)
add_custom_target(paddle_capi_whole ALL
        COMMAND mkdir -p o_files/capi && cd o_files/capi/ && ar -x $<TARGET_FILE:paddle_capi>
        COMMAND mkdir -p o_files/utils && cd o_files/utils/ && ar -x $<TARGET_FILE:paddle_utils>
        COMMAND mkdir -p o_files/parameter && cd o_files/parameter/ && ar -x $<TARGET_FILE:paddle_parameter>
        COMMAND mkdir -p o_files/math && cd o_files/math/  && ar -x $<TARGET_FILE:paddle_math>
        COMMAND mkdir -p o_files/cuda && cd o_files/cuda/ && ar -x $<TARGET_FILE:paddle_cuda>
        COMMAND mkdir -p o_files/function && cd o_files/function/ && ar -x $<TARGET_FILE:paddle_function>
        COMMAND mkdir -p o_files/gserver && cd o_files/gserver/ && ar -x $<TARGET_FILE:paddle_gserver>
        COMMAND mkdir -p o_files/proto && cd o_files/proto/ && ar -x $<TARGET_FILE:paddle_proto>
        COMMAND mkdir -p o_files/network && cd o_files/network/ && ar -x $<TARGET_FILE:paddle_network>
        COMMAND mkdir -p o_files/pserver && cd o_files/pserver/ && ar -x $<TARGET_FILE:paddle_pserver>
        COMMAND ar crs ${capi_whole_library} `find ./o_files -name '*.o'`
        COMMAND rm -rf o_files
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
        DEPENDS paddle_capi paddle_utils paddle_parameter paddle_math
                paddle_cuda paddle_function paddle_gserver
                paddle_proto paddle_pserver paddle_network
        )
set_target_properties(paddle_capi_whole
  PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_BINARY_DIR}/${capi_whole_library})

However, the codes failed on Mac. Since the function cc_library also supports to merge static libraries, we chose to use cc_library instead. The codes were changed to

# combine all paddle static libraries together, into libpaddle_capi_whole.a
# user should use PaddleCAPI as -lpaddle_capi_whole
set(PADDLE_CAPI_INFER_LIBS
    paddle_utils
    paddle_parameter
    paddle_math
    paddle_cuda
    paddle_function
    paddle_gserver
    paddle_proto
    paddle_pserver
    paddle_network)

cc_library(paddle_capi_whole DEPS paddle_capi ${PADDLE_CAPI_INFER_LIBS})

However, when trying to link libpaddle_capi_whole.a in external applications, we are tracked by following errors

error: undefined reference to `paddle_matrix_create`
error: undefined reference to `paddle_matrix_get_row`
error: undefined reference to `paddle_matrix_create_none`
error: undefined reference to `paddle_matrix_get_shape`
error: undefined reference to `paddle_matrix_get_destroy`
...

Overal, cc_library failed to merge multiple static libraries into one on Linux.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions