简体   繁体   中英

How to use a static library (/MT) in a dynamic lib (/MD) project?

Our project structured like this.

1) MainApp, it loads dlls dynamically by using boost, and all the dlls projects/modules are build with /MD.

2) Any other projects that is needed by these dlls are built as static (.lib) or dynamic (.dll) lib. There is no problem when these dependency libraries are dynamic since I can use the same MD. However, if those are static, I have to build them with MD instead of default MT, otherwise it can't be loaded in these dlls.

This has worked all the times until I am trying to compile google cloud sdk c++.

Here is the problem: Google cloud c++ sdk has many dependencies that come with the google git, BUT Google only include (or build default) the static(/MT) version. And they don't even provide options to change to MD. I can change the google cloud lib from /MT to /MD by using cmake command set(CMAKE_CXX_FLAGS_RELEASE "/MD") , but this won't build because its dependency are /MT.

Simplified situation:

MainApp.exe dynamic loads -> Function.dll (/MD), then Function.dll static links Google_cloud.lib (.lib but with /MD), then Google_cloud.lib static link its dependencies (.lib with /MT, can't change it to /MD)

So I guess the only option is to manually custom build google cloud's static dependency with /MD and then build google cloud as static with /MD then loaded by my function.dll as static.

Any suggestion?

You can mutate the BUILD_SHARED_LIBS flags which will cause the default library type to be shared libraries.

Another way is simply to pass the right argument to the add_library command:

add_library(lib1 SHARED a.cpp b.cpp)
add_library(lib2 STATIC a.cpp b.cpp)
add_library(lib3 a.cpp b.cpp) # use the BUILD_SHARED_LIBS value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM