Skip to content

Commit da729f9

Browse files
committed
fix the issue of error LNK2038: mismatch detected for 'RuntimeLibrary' in CMake for Windows debug build
1 parent 0c817ac commit da729f9

File tree

1 file changed

+34
-17
lines changed

1 file changed

+34
-17
lines changed

plugin_execution_providers/tensorrt/CMakeLists.txt

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# usage:
22
# cd build/
3-
# cmake -S ../ -B ./ -DCMAKE_BUILD_TYPE=Debug -DORT_HOME=/home/lochi/onnxruntime-win-x64-gpu-1.22.0 -DCMAKE_CUDA_ARCHITECTURES=80 -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DTENSORRT_HOME=/home/lochi/tensorrt/TensorRT-10.3.0.26 (see the result of "nvidia-smi --query-gpu=compute_cap --format=csv,noheader,nounits")
3+
# cmake -S ../ -B ./ -DCMAKE_BUILD_TYPE=Debug -DORT_HOME=/home/lochi/onnxruntime-win-x64-gpu-1.23.0 -DCMAKE_CUDA_ARCHITECTURES=80 -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DTENSORRT_HOME=/home/lochi/tensorrt/TensorRT-10.3.0.26 (see the result of "nvidia-smi --query-gpu=compute_cap --format=csv,noheader,nounits")
44
# cmake --build ./ --config Debug
55
cmake_minimum_required(VERSION 3.26)
66
project(TensorRTEp VERSION 1.0)
@@ -9,8 +9,15 @@ enable_language(CUDA)
99
file(TO_CMAKE_PATH CUDAToolkit_ROOT "/usr/local/cuda")
1010
find_package(CUDAToolkit REQUIRED)
1111

12-
# Use dynamic runtime /MD and /MDd
13-
set(CMAKE_CUDA_FLAGS_DEBUG "${CMAKE_CUDA_FLAGS_DEBUG} -Xcompiler=\"/MTd\"")
12+
# CMake config to force dynamic debug CRT globally for all dependencies.
13+
# This is to address the issue of:
14+
# libprotobufd.lib(common.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in unary_elementwise_ops_impl.obj
15+
if (WIN32)
16+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
17+
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDebugDLL" CACHE STRING "" FORCE) # /MDd
18+
set(BUILD_SHARED_LIBS OFF) # Build protobuf as static .lib, but using dynamic runtime
19+
endif()
20+
endif()
1421

1522
add_definitions(-DONNX_NAMESPACE=onnx)
1623
add_definitions(-DONNX_ML)
@@ -42,6 +49,13 @@ FetchContent_Declare(
4249
GIT_TAG v21.12 # Use a specific tag or commit
4350
)
4451

52+
if (WIN32)
53+
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
54+
# Sometimes, protobuf ignores CMAKE_MSVC_RUNTIME_LIBRARY. To ensure it works:
55+
set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "" FORCE)
56+
endif()
57+
endif()
58+
4559
FetchContent_MakeAvailable(protobuf)
4660

4761
# Add ONNX
@@ -53,6 +67,10 @@ FetchContent_Declare(
5367

5468
FetchContent_MakeAvailable(onnx)
5569

70+
#set(ONNX_USE_LITE_PROTO OFF CACHE BOOL "" FORCE)
71+
#set(ONNX_BUILD_TESTS OFF CACHE BOOL "" FORCE)
72+
#set(ONNX_GEN_PB_TYPE_STUBS OFF CACHE BOOL "" FORCE)
73+
5674
# Add GSL
5775
FetchContent_Declare(
5876
gsl
@@ -74,26 +92,25 @@ FetchContent_MakeAvailable(flatbuffers)
7492
set(DEPS_PATH "${CMAKE_BINARY_DIR}/_deps")
7593

7694
if (WIN32) # Windows
77-
set(PLATFORM "Windows")
78-
set(ORT_LIB "${ORT_HOME}/build/${PLATFORM}/${CMAKE_BUILD_TYPE}/${CMAKE_BUILD_TYPE}/onnxruntime.lib")
79-
set(DEPS_PATH "${ORT_HOME}/build/${PLATFORM}/${CMAKE_BUILD_TYPE}/_deps")
95+
set(ORT_LIB "${ORT_HOME}/lib/onnxruntime.lib")
8096
set(TRT_LIBS "${TENSORRT_HOME}/lib/nvinfer_10.lib"
8197
"${TENSORRT_HOME}/lib/nvinfer_plugin_10.lib"
8298
"${TENSORRT_HOME}/lib/nvonnxparser_10.lib")
83-
set(DEPS_LIBS "${DEPS_PATH}/flatbuffers-build/${CMAKE_BUILD_TYPE}/flatbuffers.lib"
84-
"${DEPS_PATH}/onnx-build/${CMAKE_BUILD_TYPE}/onnx.lib"
85-
"${DEPS_PATH}/onnx-build/${CMAKE_BUILD_TYPE}/onnx_proto.lib")
8699

87100
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
88101
set(DEPS_LIBS ${DEPS_LIBS}
89-
"${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotobufd.lib"
90-
"${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotocd.lib")
102+
"${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotobufd.lib"
103+
"${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotocd.lib")
91104
else()
92105
set(DEPS_LIBS ${DEPS_LIBS}
93-
"${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotobuf.lib"
94-
"${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotoc.lib")
106+
"${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotobuf.lib"
107+
"${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotoc.lib")
95108
endif()
96109

110+
set(DEPS_LIBS "${DEPS_PATH}/flatbuffers-build/${CMAKE_BUILD_TYPE}/flatbuffers.lib"
111+
"${DEPS_PATH}/onnx-build/${CMAKE_BUILD_TYPE}/onnx.lib"
112+
"${DEPS_PATH}/onnx-build/${CMAKE_BUILD_TYPE}/onnx_proto.lib")
113+
97114
set(TRT_EP_LIB_LINK_FLAG
98115
"-DEF:${CMAKE_SOURCE_DIR}/tensorrt_execution_provider.def")
99116

@@ -125,8 +142,7 @@ MESSAGE(STATUS "Deps libs: ${DEPS_LIBS}")
125142
set_property(TARGET TensorRTEp APPEND_STRING PROPERTY LINK_FLAGS
126143
${TRT_EP_LIB_LINK_FLAG})
127144

128-
target_include_directories(TensorRTEp PUBLIC #"${ORT_HOME}/include"
129-
"${ORT_HOME}/include/onnxruntime/core/session"
145+
target_include_directories(TensorRTEp PUBLIC "${ORT_HOME}/include"
130146
"./utils"
131147
"/usr/local/cuda/include"
132148
"${TENSORRT_HOME}/include"
@@ -137,8 +153,9 @@ target_include_directories(TensorRTEp PUBLIC #"${ORT_HOME}/include"
137153
"${DEPS_PATH}/protobuf-src/src"
138154
)
139155

140-
target_link_libraries(TensorRTEp PUBLIC ${ORT_LIB}
156+
target_link_libraries(TensorRTEp PUBLIC #${DEPS_LIBS}
157+
protobuf::libprotobuf onnx flatbuffers
158+
${ORT_LIB}
141159
${TRT_LIBS}
142160
CUDA::cudart
143-
${DEPS_LIBS}
144161
)

0 commit comments

Comments
 (0)