link error with torch library

3 Relevant Page: Getting-started
Larry 3 months ago
  • Situation: I am trying to bind a simple example software with drogon as interface, and torch is involved as core ML function.
  • Expectation: compile pass and work as expected.
  • Result: link failure with following error
/usr/bin/c++ -D_GLIBCXX_USE_CXX11_ABI=0 CMakeFiles/CppModelServ.dir/main.cc.o CMakeFiles/CppModelServ.dir/controllers/MmsCtrl.cc.o -o CppModelServ -Wl,-rpath,/usr/local/libtorch/lib:/usr/local/cuda/lib64/stubs:/usr/local/cuda/lib64 /usr/local/lib/libdrogon.a /usr/local/libtorch/lib/libtorch.so /usr/local/libtorch/lib/libc10.so /usr/local/libtorch/lib/libkineto.a /usr/local/cuda/lib64/stubs/libcuda.so /usr/local/cuda/lib64/libnvrtc.so /usr/local/cuda/lib64/libnvToolsExt.so /usr/local/cuda/lib64/libcudart.so /usr/local/libtorch/lib/libc10_cuda.so /usr/local/lib/libtrantor.a -ldl -lstdc++fs /usr/lib/x86_64-linux-gnu/libjsoncpp.so /usr/lib/x86_64-linux-gnu/libuuid.so /usr/lib/x86_64-linux-gnu/libz.so /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/x86_64-linux-gnu/libcrypto.so -lpthread -Wl,--no-as-needed,"/usr/local/libtorch/lib/libtorch_cpu.so" -Wl,--as-needed -Wl,--no-as-needed,"/usr/local/libtorch/lib/libtorch_cuda.so" -Wl,--as-needed /usr/local/libtorch/lib/libc10_cuda.so /usr/local/libtorch/lib/libc10.so /usr/local/cuda/lib64/libcufft.so /usr/local/cuda/lib64/libcurand.so /usr/local/cuda/lib64/libcublas.so /usr/lib/x86_64-linux-gnu/libcudnn.so -Wl,--no-as-needed,"/usr/local/libtorch/lib/libtorch.so" -Wl,--as-needed /usr/local/cuda/lib64/libnvToolsExt.so /usr/local/cuda/lib64/libcudart.so /usr/bin/ld: CMakeFiles/CppModelServ.dir/main.cc.o: in function `std::enable_if<std::is_default_constructible<drogon::plugin::AccessLogger>::value, void>::type drogon::DrObject<drogon::plugin::AccessLogger>::DrAllocator::registerClass<drogon::plugin::AccessLogger>()': main.cc:(.text._ZN6drogon8DrObjectINS_6plugin12AccessLoggerEE11DrAllocator13registerClassIS2_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv[_ZN6drogon8DrObjectINS_6plugin12AccessLoggerEE11DrAllocator13registerClassIS2_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv]+0x46): undefined reference to `drogon::DrClassMap::registerClass(std::string const&, std::function<drogon::DrObjectBase* ()> const&)' /usr/bin/ld: CMakeFiles/CppModelServ.dir/main.cc.o: in function `std::enable_if<std::is_default_constructible<drogon::plugin::SecureSSLRedirector>::value, void>::type drogon::DrObject<drogon::plugin::SecureSSLRedirector>::DrAllocator::registerClass<drogon::plugin::SecureSSLRedirector>()': main.cc:(.text._ZN6drogon8DrObjectINS_6plugin19SecureSSLRedirectorEE11DrAllocator13registerClassIS2_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv[_ZN6drogon8DrObjectINS_6plugin19SecureSSLRedirectorEE11DrAllocator13registerClassIS2_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv]+0x46): undefined reference to `drogon::DrClassMap::registerClass(std::string const&, std::function<drogon::DrObjectBase* ()> const&)' /usr/bin/ld: CMakeFiles/CppModelServ.dir/main.cc.o: in function `std::enable_if<std::is_default_constructible<drogon::NotFound>::value, void>::type drogon::DrObject<drogon::NotFound>::DrAllocator::registerClass<drogon::NotFound>()': main.cc:(.text._ZN6drogon8DrObjectINS_8NotFoundEE11DrAllocator13registerClassIS1_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv[_ZN6drogon8DrObjectINS_8NotFoundEE11DrAllocator13registerClassIS1_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv]+0x46): undefined reference to `drogon::DrClassMap::registerClass(std::string const&, std::function<drogon::DrObjectBase* ()> const&)' /usr/bin/ld: CMakeFiles/CppModelServ.dir/main.cc.o: in function `std::enable_if<std::is_default_constructible<drogon::LocalHostFilter>::value, void>::type drogon::DrObject<drogon::LocalHostFilter>::DrAllocator::registerClass<drogon::LocalHostFilter>()': main.cc:(.text._ZN6drogon8DrObjectINS_15LocalHostFilterEE11DrAllocator13registerClassIS1_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv[_ZN6drogon8DrObjectINS_15LocalHostFilterEE11DrAllocator13registerClassIS1_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv]+0x46): undefined reference to `drogon::DrClassMap::registerClass(std::string const&, std::function<drogon::DrObjectBase* ()> const&)' /usr/bin/ld: CMakeFiles/CppModelServ.dir/main.cc.o: in function `std::enable_if<std::is_default_constructible<drogon::IntranetIpFilter>::value, void>::type drogon::DrObject<drogon::IntranetIpFilter>::DrAllocator::registerClass<drogon::IntranetIpFilter>()': main.cc:(.text._ZN6drogon8DrObjectINS_16IntranetIpFilterEE11DrAllocator13registerClassIS1_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv[_ZN6drogon8DrObjectINS_16IntranetIpFilterEE11DrAllocator13registerClassIS1_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv]+0x46): undefined reference to `drogon::DrClassMap::registerClass(std::string const&, std::function<drogon::DrObjectBase* ()> const&)' /usr/bin/ld: CMakeFiles/CppModelServ.dir/controllers/MmsCtrl.cc.o: in function `MmsCtrl::get(std::shared_ptr<drogon::HttpRequest> const&, std::function<void (std::shared_ptr<drogon::HttpResponse> const&)>&&, std::string) const': MmsCtrl.cc:(.text+0x12a): undefined reference to `Json::Value::Value(std::string const&)' /usr/bin/ld: MmsCtrl.cc:(.text+0x1ba): undefined reference to `Json::Value::Value(std::string const&)' /usr/bin/ld: CMakeFiles/CppModelServ.dir/controllers/MmsCtrl.cc.o: in function `MmsCtrl::list(std::shared_ptr<drogon::HttpRequest> const&, std::function<void (std::shared_ptr<drogon::HttpResponse> const&)>&&) const': MmsCtrl.cc:(.text+0x494): undefined reference to `Json::Value::Value(std::string const&)' /usr/bin/ld: MmsCtrl.cc:(.text+0x55c): undefined reference to `Json::Value::Value(std::string const&)' /usr/bin/ld: CMakeFiles/CppModelServ.dir/controllers/MmsCtrl.cc.o: in function `MmsCtrl::invoke(std::shared_ptr<drogon::HttpRequest> const&, std::function<void (std::shared_ptr<drogon::HttpResponse> const&)>&&, std::string) const': MmsCtrl.cc:(.text+0xdc8): undefined reference to `drogon::utils::getUuid()' /usr/bin/ld: MmsCtrl.cc:(.text+0xde1): undefined reference to `Json::Value::Value(std::string const&)' /usr/bin/ld: CMakeFiles/CppModelServ.dir/controllers/MmsCtrl.cc.o: in function `std::enable_if<std::is_default_constructible<MmsCtrl>::value, void>::type drogon::DrObject<MmsCtrl>::DrAllocator::registerClass<MmsCtrl>()': MmsCtrl.cc:(.text._ZN6drogon8DrObjectI7MmsCtrlE11DrAllocator13registerClassIS1_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv[_ZN6drogon8DrObjectI7MmsCtrlE11DrAllocator13registerClassIS1_EENSt9enable_ifIXsrSt24is_default_constructibleIT_E5valueEvE4typeEv]+0x46): undefined reference to `drogon::DrClassMap::registerClass(std::string const&, std::function<drogon::DrObjectBase* ()> const&)' /usr/bin/ld: CMakeFiles/CppModelServ.dir/controllers/MmsCtrl.cc.o: in function `std::shared_ptr<MmsCtrl> drogon::DrClassMap::getSingleInstance<MmsCtrl>()': MmsCtrl.cc:(.text._ZN6drogon10DrClassMap17getSingleInstanceI7MmsCtrlEESt10shared_ptrIT_Ev[_ZN6drogon10DrClassMap17getSingleInstanceI7MmsCtrlEESt10shared_ptrIT_Ev]+0x56): undefined reference to `drogon::DrClassMap::getSingleInstance(std::string const&)' collect2: error: ld returned 1 exit status make[2]: *** [CMakeFiles/CppModelServ.dir/build.make:136: CppModelServ] Error 1 make[2]: Leaving directory '/mnt/cpp-model-serv.git' make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/CppModelServ.dir/all] Error 2 make[1]: Leaving directory '/mnt/cpp-model-serv.git' make: *** [Makefile:91: all] Error 2
laurencem 3 months ago , edited 3 months ago
  • the above message is me, just posted as anonymouse user. sorry.
  • cmake file:
cmake_minimum_required(VERSION 3.5)

project(CppModelServ CXX) project(CppModelServ VERSION 1.0)

configure_file(CppModelServConfig.h.in CppModelServConfig.h)

include(CheckIncludeFileCXX)

check_include_file_cxx(any HAS_ANY) check_include_file_cxx(string_view HAS_STRING_VIEW) check_include_file_cxx(coroutine HAS_COROUTINE)

specify the C++ standard

if (HAS_ANY AND HAS_STRING_VIEW AND HAS_COROUTINE) set(CMAKE_CXX_STANDARD 20) elseif (HAS_ANY AND HAS_STRING_VIEW) set(CMAKE_CXX_STANDARD 17) else () set(CMAKE_CXX_STANDARD 14) endif ()

set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF)

add_executable(${PROJECT_NAME} main.cc)

If you include the drogon source code locally in your project, use this method

to add drogon

add_subdirectory(drogon)

target_link_libraries(${PROJECT_NAME} PRIVATE drogon)

and comment out the following lines

find_package(Drogon CONFIG REQUIRED)

find_package(Torch REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${DROGON_CXX_FLAGS} ${TORCH_CXX_FLAGS}") target_link_libraries(${PROJECT_NAME} "${DROGON_LIBRARIES}" "${TORCH_LIBRARIES}")

if (CMAKE_CXX_STANDARD LESS 17) # With C++14, use boost to support any, string_view and filesystem message(STATUS "use c++14") find_package(Boost 1.61.0 REQUIRED) target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost) elseif (CMAKE_CXX_STANDARD LESS 20) message(STATUS "use c++17") else () message(STATUS "use c++20") endif ()

aux_source_directory(controllers CTL_SRC) aux_source_directory(filters FILTER_SRC) aux_source_directory(plugins PLUGIN_SRC) aux_source_directory(models MODEL_SRC)

drogon_create_views(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/views ${CMAKE_CURRENT_BINARY_DIR})

use the following line to create views with namespaces.

drogon_create_views(${PROJECT_NAME} ${CMAKE_CURRENT_SOURCE_DIR}/views

${CMAKE_CURRENT_BINARY_DIR} TRUE)

target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/models) target_sources(${PROJECT_NAME} PRIVATE ${SRC_DIR} ${CTL_SRC} ${FILTER_SRC} ${PLUGIN_SRC} ${MODEL_SRC})

uncomment the following line for dynamically loading views

set_property(TARGET ${PROJECT_NAME} PROPERTY ENABLE_EXPORTS ON)

add_subdirectory(test)

laurencem 3 months ago
  • controller
void MmsCtrl::invoke(const HttpRequestPtr &req, std::function<void (const HttpResponsePtr &)> &&callback, std::string model_name) const { LOG_DEBUG <<"invoke model: " << model_name; //Authentication algorithm, read database, verify identity, etc... //... // at::Tensor a = at::ones({2, 2}, at::kInt); // at::Tensor b = at::randn({2, 2}); // auto c = a + b.to(at::kInt); Json::Value ret; ret["hello"] = "world"; ret["uuid"] = drogon::utils::getUuid(); auto resp=HttpResponse::newHttpJsonResponse(ret); callback(resp); }
marty188586 3 months ago

Please open an issue GitHub so the maintainers can track the progress of it (and so other people can search it). The maintainers can't do much when you post on the documents.

Log in to comment