Reputation: 468
I have an MSVS solution with two projects: DLL one generates library.lib
and library.dll
, static library one generates library_st.lib
. conanfile.py
packages those three objects into package.
I have another MSVS solution (conanfile.txt
only) which [requires]
first project. Conan generates .props file which links to library.lib
, and I can't find a way to link to library_st.lib
with it.
I tried passing shared=False
to a library and changing its package()
method to save only static library. I don't know how to check the exact binary package files but my solution still tries to link to 'library.lib' after that change.
I guess my question is two-part:
How to create dynamic and static library with Conan and MSVS 2017?
How to choose which library I link to when package has several .lib
files in it?
Upvotes: 2
Views: 8303
Reputation: 3887
How to create dynamic and static library with Conan and MSVS 2017?
If you are using CMake + MSVC, you just need to add the option shared. CMake helper will translate that option to BUILD_SHARED_LIBS definition when configuring your project.
However, if you are using only Visual Studio, it depends how your project is configured. Conan MSBuild is able to select what you want, including target, architecture and build type. You could use different targets, one for each configuration.
How to choose which library I link to when package has several .lib files in it?
By options. When creating a package, Conan will export all library names that you want by self.cpp_info.libs. You can call tools.collect_libs(self), which will list all libraries in the package folder or, you can list what you want e.g. [library_st, library]. If you need to choose which library you want to link, you should need to add an option with the library to be listed, otherwise you will need to ignore CONAN_PKG:: or CONAN_LIBS when linking:
from conans import Conanfile, MSBuild
class ExampleConan(Conanfile):
name = "example"
version = "0.1.0"
settings = "os", "arch", "build_type", "compiler"
options = {"shared": [True, False], "st": [True, False]}
default_options = {"shared": False, "st": False}
exports = "*"
_msvc_archs = {"x86": "x86", "x86_64": "x64"}
def build(self):
msbuild = MSBuild(self)
msbuild.build("Example.sln", platforms=self._msvc_archs)
def package(self):
library_folder = os.path.join(self._msvc_archs[self.settings.arch.value], self.settings.build_type.value)
self.copy("*.lib", src=library_folder, dst="lib")
if self.options.shared:
self.copy("*.dll", src=library_folder, dst="bin")
def package_info(self):
self.cpp_info.libs = ["library_st"] if self.options.st else ["library"]
IMO you are trying to build two projects in the same package, which sounds wrong. I would say you should create separated packages for each one. You could create one individual recipe for each project. You should remember that each option will introduce a new point for a package ID.
Upvotes: 3