microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
14.59k stars 2.92k forks source link

Linking executable with static libraries --> error LNK2038: mismatch detected #15928

Open microsoft-sampsa opened 1 year ago

microsoft-sampsa commented 1 year ago

Describe the issue

Since Windoze doesn't have a proper shared lib versioning system (i.e. some standard place in /usr/lib/whatever where different version of .so files could live), I am forced to try to link onnxruntime statically into my executable (other possibility is to keep the correct onnxruntime.dll hanging around in the same directory as the exec, but it's the same thing almost).

I am following this github issue, although some of the directories seem to have changed.

So far:

There, the emphasis is on: value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug'

Here is an excerpt from my CMakeLists.txt:

...
set(CMAKE_BUILD_TYPE Debug)
...
if(WIN32)
    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
endif(WIN32)
...
set(ORT_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../ext/onnxruntime-1.14.1")

target_link_directories(
main PUBLIC
"${ORT_ROOT}/build/Windows/${CMAKE_BUILD_TYPE}"
)

target_link_libraries(
main
${CMAKE_BUILD_TYPE}/onnxruntime_session
${CMAKE_BUILD_TYPE}/onnxruntime_optimizer
${CMAKE_BUILD_TYPE}/onnxruntime_providers
${CMAKE_BUILD_TYPE}/onnxruntime_util
${CMAKE_BUILD_TYPE}/onnxruntime_framework
${CMAKE_BUILD_TYPE}/onnxruntime_graph
${CMAKE_BUILD_TYPE}/onnxruntime_mlas
${CMAKE_BUILD_TYPE}/onnxruntime_common
${CMAKE_BUILD_TYPE}/onnxruntime_flatbuffers
${CMAKE_BUILD_TYPE}/onnx_test_data_proto
_deps/onnx-build/${CMAKE_BUILD_TYPE}/onnx
_deps/onnx-build/${CMAKE_BUILD_TYPE}/onnx_proto
_deps/protobuf-build/${CMAKE_BUILD_TYPE}/libprotobuf-lited
_deps/re2-build/${CMAKE_BUILD_TYPE}/re2
_deps/abseil_cpp-build/absl/base/${CMAKE_BUILD_TYPE}/absl_base
_deps/abseil_cpp-build/absl/base/${CMAKE_BUILD_TYPE}/absl_throw_delegate
_deps/abseil_cpp-build/absl/container/${CMAKE_BUILD_TYPE}/absl_raw_hash_set
_deps/abseil_cpp-build/absl/hash/${CMAKE_BUILD_TYPE}/absl_hash
_deps/abseil_cpp-build/absl/hash/${CMAKE_BUILD_TYPE}/absl_city
_deps/abseil_cpp-build/absl/hash/${CMAKE_BUILD_TYPE}/absl_low_level_hash
)

target_include_directories(
main PUBLIC
${ORT_ROOT}/include/onnxruntime
)

Urgency

No response

Target platform

windows

Build script

build.bat --build --enable_msvc_static_runtime --config Debug --parallel --cmake_generator "Visual Studio 17 2022"

Error / output

onnxruntime_session.lib(onnxruntime_c_api.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in wrap.obj [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
onnxruntime_session.lib(abi_session_options.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in wrap.obj [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
onnxruntime_session.lib(default_cpu_allocator_c_api.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in wrap.obj [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
onnxruntime_session.lib(custom_ops.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in wrap.obj [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
onnxruntime_session.lib(allocator_adapters.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in wrap.obj [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
onnxruntime_session.lib(provider_bridge_ort.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in wrap.obj [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
...
...
msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: struct _Cvtvec __cdecl std::_Locinfo::_Getcvt(void)const " (?_Getcvt@_Locinfo@std@@QEBA?AU_Cvtvec@@XZ) already defined in onnxruntime_session.lib(onnxruntime_c_api.obj) [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: class std::_Yarn<char> & __cdecl std::_Yarn<char>::operator=(char const *)" (??4?$_Yarn@D@std@@QEAAAEAV01@PEBD@Z) already defined in onnxruntime_providers.lib(string_normalizer.obj) [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: static void * __cdecl std::_Crt_new_delete::operator new(unsigned __int64)" (??2_Crt_new_delete@std@@SAPEAX_K@Z) already defined in onnxruntime_session.lib(onnxruntime_c_api.obj) [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: static void __cdecl std::_Crt_new_delete::operator delete(void *)" (??3_Crt_new_delete@std@@SAXPEAX@Z) already defined in onnxruntime_session.lib(onnxruntime_c_api.obj) [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: __cdecl std::locale::id::operator unsigned __int64(void)" (??Bid@locale@std@@QEAA_KXZ) already defined in onnxruntime_session.lib(onnxruntime_c_api.obj) [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "private: void __cdecl std::locale::_Locimp::_Addfac(class std::locale::facet *,unsigned __int64)" (?_Addfac@_Locimp@locale@std@@AEAAXPEAVfacet@23@_K@Z) already defined in onnxruntime_providers.lib(string_normalizer.obj) [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
msvcprtd.lib(MSVCP140D.dll) : error LNK2005: "public: int __cdecl std::codecvt<wchar_t,char,struct _Mbstatet>::in(struct _Mbstatet &,char const *,char const *,char const * &,wchar_t *,wchar_t *,wchar_t * &)const " (?in@?$codecvt@_WDU_Mbstatet@@@std@@QEBAHAEAU_Mbstatet@@PEBD1AEAPEBDPEA_W3AEAPEA_W@Z) already defined in onnxruntime_providers.lib(string_normalizer.obj) [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
...
...
LINK : warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
onnxruntime_common.lib(stacktrace.obj) : error LNK2019: unresolved external symbol __imp_SymSetOptions referenced in function "public: __cdecl onnxruntime::detail::SymbolHelper::SymbolHelper(void)" (??0SymbolHelper@detail@onnxruntime@@QEAA@XZ) [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
onnxruntime_common.lib(stacktrace.obj) : error LNK2019: unresolved external symbol __imp_SymGetOptions referenced in function "public: __cdecl onnxruntime::detail::SymbolHelper::SymbolHelper(void)" (??0SymbolHelper@detail@onnxruntime@@QEAA@XZ) [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
onnxruntime_common.lib(stacktrace.obj) : error LNK2019: unresolved external symbol __imp_SymCleanup referenced in function "public: __cdecl onnxruntime::detail::SymbolHelper::~SymbolHelper(void)" (??1SymbolHelper@detail@onnxruntime@@QEAA@XZ) [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
onnxruntime_common.lib(stacktrace.obj) : error LNK2019: unresolved external symbol __imp_SymGetLineFromAddr64 referenced in function "private: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl onnxruntime::detail::CaptureStackTrace::Lookup(void *)const " (?Lookup@CaptureStackTrace@detail@onnxruntime@@AEBA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PEAX@Z) [C:\cygwin64\home\v-sriikonen\mlops-flow\deploy\onnx\build\main.vcxproj]
...
...

Visual Studio Version

17 2022

GCC / Compiler Version

n/a

microsoft-sampsa commented 1 year ago

Tried also in my CMakeLists.txt:

if(WIN32)
    # set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded)
    set(CMAKE_CXX_FLAGS_RELEASE "/MT") # for static libs
    set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
endif(WIN32)

to no avail.

A ref here

microsoft-sampsa commented 1 year ago

Looking at my CMakeCache.txt it still says:

...
//Flags used by the CXX compiler during DEBUG builds.
CMAKE_CXX_FLAGS_DEBUG:STRING=/MDd /Zi /Ob0 /Od /RTC1
...

i.e. there's that /MDd instead of /MTd.. (stubborn cmake)

EDIT: I'm able to force CMake to use the correct

CMAKE_CXX_FLAGS_DEBUG:STRING=/MTd /Zi /Ob0 /Od /RTC1

but I still have exactly the same problem

yuslepukhin commented 1 year ago

We do not support static linking. The .lib is an import library for the DLL.

xeeetu commented 1 year ago

We do not support static linking. The .lib is an import library for the DLL.

Hello, could you please tell me what and where to change when building the .lib files to build statically, without .dll. If I can not do this, why? I will be grateful for any answer.