aliyun / aliyun-oss-cpp-sdk

Aliyun OSS SDK for C++
Other
188 stars 88 forks source link

cmake 链接oss sdk 静态库undefined reference #104

Open LionelDong opened 3 years ago

LionelDong commented 3 years ago

oss

include_directories(/usr/local/include) link_directories(/usr/local/lib) target_link_libraries(${name} libalibabacloud-oss-cpp-sdk.a) 编译安装oss sdk后,最新的cmake中的链接方式如上;

undefined reference to `AlibabaCloud::OSS::PutObjectRequest::PutObjectRequest(std::string const&, std::string const&, std::shared_ptr const&)' 错误。

搞了好几天,希望可以得到同学的帮助进行排查,万分感谢~ @huiguangjun

LionelDong commented 3 years ago

环境:ubuntu18.04 cmake 3.21.3 gcc 7.5.0 使用了阿里云rocketmq tcp商业版C++ sdk和oss C++ sdk. cmakelistst.txt 全部内容如下:

cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0048 NEW)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

project(onsclient4cpp_demo VERSION 1.0
        LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions -fno-rtti")

add_definitions(-w)
include(FindCURL)
include(FindOpenSSL)

if(NOT CURL_FOUND)
  message(FATAL_ERROR "Could not find curl")
endif()

if(NOT OPENSSL_FOUND)
  message(FATAL_ERROR "Could not find openssl")
endif()

set(CRYPTO_LIBS ${OPENSSL_LIBRARIES})
set(CRYPTO_INCLUDE_DIRS ${OPENSSL_INCLUDE_DIRS}) 
set(CRYPTO_LIBS_ABSTRACT_NAME crypto ssl)
include_directories(${OPENSSL_INCLUDE_DIRS})
if(CURL_FOUND)
    include_directories(${CURL_INCLUDE_DIR})
    message(STATUS "Curl Found: ${CURL_VERSION_STRING} ${CURL_LIBRARIES} ${CURL_LINK_LIBRARIES}!")
    set(curl_library ${CURL_LIBRARIES})
endif()

if (CONAN_LIBCXX STREQUAL "libstdc++11")
    message("Using -D_GLIBCXX_USE_CXX11_ABI=1")
    add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)
elseif (CONAN_LIBCXX STREQUAL "libstdc++")
    message("Using -D_GLIBCXX_USE_CXX11_ABI=0")
    add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
else ()
    message("As Default -D_GLIBCXX_USE_CXX11_ABI=0, not set.")
    add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
endif ()

# oss
include_directories(/usr/local/include)
link_directories(/usr/local/lib)

include_directories(../include)
include_directories(/usr/local/include)

find_library(ROCKETMQ_CLIENT_CORE
        NAMES rocketmq_client_core
        HINTS ../lib)

if (${ROCKETMQ_CLIENT_CORE-NOTFOUNT})
    message("find_library for rocketmq_client_core failed")
endif ()

find_library(ONS_CLIENT
        NAMES onsclient4cpp
        HINTS ../lib)

if (${ONS_CLIENT-NOTFOUNT})
    message("find_library for rocketmq_client_core failed")
endif ()

macro(add_demo name source_file)
    add_executable(${name} ${source_file})

    target_link_libraries(${name} ${ROCKETMQ_CLIENT_CORE} ${ONS_CLIENT})

    target_link_libraries(${name} libalibabacloud-oss-cpp-sdk.a)
    target_link_libraries(${name} ${curl_library})
    target_link_libraries(${name} OpenSSL::Crypto OpenSSL::SSL)
    target_link_libraries(${name} ${CMAKE_DL_LIBS} -lpthread)

    set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
endmacro()

add_demo(consumer Consumer.cpp)
LionelDong commented 3 years ago

问题解决了。原因定位到rocketmq SDK 需要设置ABI=0,而OSS SDK默认编译的是ABI=1的版本,所以导致冲突。将OSS SDK的cmakelists.txt中添加一行add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) ,重新编译生成新的库就可以了。

LionelDong commented 3 years ago

@alibaba-oss 建议新增-D_GLIBCXX_USE_CXX11_ABI 编译项,并提示多个aliyun C++ sdk链接时可能存在的问题。