Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to build and use onnxruntime static lib on windows? #1472

Closed
smilexin opened this issue Jul 23, 2019 · 12 comments
Closed

How to build and use onnxruntime static lib on windows? #1472

smilexin opened this issue Jul 23, 2019 · 12 comments

Comments

@smilexin
Copy link

smilexin commented Jul 23, 2019

Use build flag --build_shared_lib, we can get onnxruntime.lib, onnxruntime.dll, which can be dynamically linked,
While how to build a single onnxruntime static lib like onnxruntime.lib for static link?

I had tried to change the following code in onnxruntime.cmake to get a single onnxruntime.lib,but it can't be resolve when used in vs2017, I cannot figure out why, can someone meet the same issue?

add_library(onnxruntime SHARED ${onnxruntime_session_srcs})
=>
add_library(onnxruntime STATIC ${onnxruntime_session_srcs})
and use flag --build_shared_lib to trigger the onnxruntime.cmake build

@jywu-msft
Copy link
Member

there isn't a single onnxruntime static lib.
there are static libs per project module
e.g. onnxruntime_common.lib , onnxruntime_framework.lib, onnxruntime_graph.lib , onnxruntime_providers.lib etc.

unit tests are static linked against these libs so you can look at onnxruntime_unittests.cmake as example

@smilexin
Copy link
Author

@jywu-msft Nice, thanks so much, Let me have a try.

@faxu faxu added the question label Jul 25, 2019
@faxu
Copy link
Contributor

faxu commented Jul 26, 2019

@smilexin did it work for you? if so, please close this issue.

@smilexin
Copy link
Author

@faxu I'll try it this week and feedback quickly, thanks~

@smilexin
Copy link
Author

smilexin commented Aug 1, 2019

@jywu-msft I had spend some time but had't figure out which static libs were really needed to do inference. In the onnxruntime_unittests.cmake, if we can refer the part of enable onnxruntime_BUILD_SHARED_LIB code block? It's still not very clear about the dependency of test code and the source code, If we can select out the only infer part source code denpendencies? Thanks so much for your kindly help~

@smilexin
Copy link
Author

smilexin commented Aug 1, 2019

@jywu-msft One more question: What are the correspondent .h file dependencies of each static lib? I tried to use all the built static lib and the related .h file in my sight, but it always have unresolved external symbol errors as followings, thanks for having a look:
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtReleaseSession@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtGetTensorElementType@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtReleaseAllocator@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtGetTensorMutableData@8
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtCreateDefaultAllocator@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtCreateCpuAllocatorInfo@12
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtGetNumOfDimensions@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtCreateEnv@12
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtReleaseEnv@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtGetErrorMessage@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtReleaseValue@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtCreateTensorWithDataAsOrtValue@28
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtCreateSessionOptions@0
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtSessionGetOutputTypeInfo@12
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtSetSessionLogVerbosityLevel@8
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtSessionGetInputCount@8
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtSetSessionGraphOptimizationLevel@8
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtReleaseTypeInfo@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtRun@32
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtSetSessionThreadPoolSize@8
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtReleaseAllocatorInfo@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtSetSessionLogId@8
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtReleaseStatus@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtGetDimensions@12
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtSessionGetInputTypeInfo@12
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtSessionGetInputName@16
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtCreateSession@16
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtReleaseSessionOptions@4
1>official_release_AI_audio.obj : error LNK2001: unresolved external symbol _OrtCastTypeInfoToTensorInfo@4

@jywu-msft
Copy link
Member

shared lib is the preferred/recommended approach instead of static lib.
I assume you are not as familiar with cmake? (the dependencies are listed there in the cmake file for unit tests)

another approach might be for you to do a build.bat --update (i.e. build without shared lib) to let cmake generate the VS project files.
you can look at onnx_test_runner.vcxproj as an example of an application that static links onnxruntime libs. the AdditionalDependencies and AdditionalLibraryDirectories should tell you what is needed

@smilexin
Copy link
Author

smilexin commented Aug 2, 2019

@jywu-msft Thanks for your kindly reply in a very detailed, it help me a lot. As you assum, i'am new to cmake, and we want to use onnxruntime in our sdk, which should use static link. I'll follow you guide to have a try, thanks 🙃

@smilexin
Copy link
Author

smilexin commented Aug 8, 2019

@jywu-msft thanks for you guide, i had successfully figured out the needed static libs and .h. When I load them in my project, there exist a another problem, the "RuntimeLibrary" didn't match, the error massage is as following: onnxruntime_session.lib(onnxruntime_c_api.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in ...

So how can we change onnxruntime the default Runtime Library from /MD to /MT ? where can we change it? I try to set in VS ide, but some third party lib such as libprotobuf error occurs, as following:
onnx.lib(defs.cc.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in inference_session_without_environment_standalone_test.obj

@smilexin
Copy link
Author

smilexin commented Aug 8, 2019

https://github.com/microsoft/onnxruntime/pull/757/commits

I got it, use flag when build: --enable_msvc_static_runtime, can refer to CMakeLists.txt: onnxruntime_MSVC_STATIC_RUNTIME logic

@lucasjinreal
Copy link

@smilexin what's wrong with onnxruntime on windows?

f-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\repeated_ptr_field.cc(60,45): error C2062: 意外的类型“void” [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libprotobuf-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\repeated_ptr_field.cc(85,17): error C2039: "SizedDelete": 不是 "google::protobuf::internal" 的成员 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\pr
otobuf-build\libprotobuf-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\repeated_ptr_field.cc(49): message : 参见“google::protobuf::internal”的声明 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libprotobu
f-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\repeated_ptr_field.cc(85,28): error C3861: “SizedDelete”: 找不到标识符 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libprotobuf-lite
.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\repeated_ptr_field.cc(87,15): error C2039: "ReturnArrayMemory": 不是 "google::protobuf::Arena" 的成员 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps
\protobuf-build\libprotobuf-lite.vcxproj]
C:\Users\lewisjin\Anaconda3\Library\include\google/protobuf/arena.h(243): message : 参见“google::protobuf::Arena”的声明 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libprotobuf-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\repeated_ptr_field.cc(110,13): error C2039: "SizedDelete": 不是 "google::protobuf::internal" 的成员 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\p
rotobuf-build\libprotobuf-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\repeated_ptr_field.cc(49): message : 参见“google::protobuf::internal”的声明 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libprotobu
f-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\repeated_ptr_field.cc(110,24): error C3861: “SizedDelete”: 找不到标识符 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libprotobuf-lit
e.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\stubs\stringprintf.cc(155,3): warning C4267: “初始化”: 从“size_t”转换到“int”,可能丢失数据 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libp
rotobuf-lite.vcxproj]
C:\Users\lewisjin\Anaconda3\Library\include\google/protobuf/parse_context.h(324,20): warning C4244: “初始化”: 从“__int64”转换到“int”,可能丢失数据 (编译源文件 E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\parse_context.cc)
[E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libprotobuf-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\parse_context.cc(208): message : 查看对正在编译的函数 模板 实例化“const char *google::protobuf::internal::EpsCopyInputStream::AppendSize<google::protobuf::internal::EpsCopyI
nputStream::SkipFallback::<lambda_9fa394976caaed878bf473fdaa662b57>>(const char *,int,const A &)”的引用 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libprotobuf-lite.vcxproj]
          with
          [
              A=google::protobuf::internal::EpsCopyInputStream::SkipFallback::<lambda_9fa394976caaed878bf473fdaa662b57>
          ]
C:\Users\lewisjin\Anaconda3\Library\include\google/protobuf/parse_context.h(338,45): warning C4244: “=”: 从“__int64”转换到“int”,可能丢失数据 (编译源文件 E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\parse_context.cc) [E
:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libprotobuf-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\stubs\structurally_valid.cc(492,25): warning C4244: “=”: 从“__int64”转换到“int”,可能丢失数据 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-buil
d\libprotobuf-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\parse_context.cc(417,45): warning C4244: “=”: 从“int64_t”转换到“T”,可能丢失数据 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libprotobuf
-lite.vcxproj]
          with
          [
              T=int32_t
          ]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\parse_context.cc(430): message : 查看对正在编译的函数 模板 实例化“const char *google::protobuf::internal::VarintParser<int32_t,false>(void *,const char *,google::protobuf::i
nternal::ParseContext *)”的引用 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\libprotobuf-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\stubs\structurally_valid.cc(527,13): warning C4244: “=”: 从“__int64”转换到“int”,可能丢失数据 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-buil
d\libprotobuf-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\parse_context.cc(419,46): warning C4244: “参数”: 从“uint64_t”转换到“uint32_t”,可能丢失数据 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-build\li
bprotobuf-lite.vcxproj]
E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-src\src\google\protobuf\stubs\structurally_valid.cc(532,25): warning C4244: “=”: 从“__int64”转换到“int”,可能丢失数据 [E:\codes\libs\onnxruntime\build\Windows\RelWithDebInfo\_deps\protobuf-buil
d\libprotobuf-lite.vcxproj]

@csukuangfj
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants