Conan有不同的助手来管理基于Visual Studio和MSBuild的项目。 此操作方法说明了如何将它们放在一起以创建和使用完全基于Visual Studio的包。 此方法使用的是VS2015,但也可以使用其他版本。
创建包
开始克隆现有的示例存储库,其中包含一个简单的“ Hello World”库和应用程序:
$ git clone https://github.com/memsharded/hello_vs
$ cd hello_vs
它包含一个带有源代码的src文件夹和一个带有Visual Studio 2015解决方案的build文件夹,其中包含2个项目:HelloLib静态库和Greet应用程序。 打开它:
$ build\HelloLib\HelloLib.sln
您应该能够选择Greet子项目-> Set as Startup Project。 然后使用Ctrl + F5生成并运行该应用程序。 (调试->启动而不调试)
$ Hello World Debug!
# Switch IDE to Release mode, repeat
$ Hello World Release!
因为hello.cpp文件包含#ifdef _DEBUG,以便在调试和发布消息之间切换。
在存储库中,已经有一个conanfile.py配方:
from conans import ConanFile, MSBuild
class HelloConan(ConanFile):
name = "hello"
version = "0.1"
license = "MIT"
url = "https://github.com/memsharded/hello_vs"
settings = "os", "compiler", "build_type", "arch"
exports_sources = "src/*", "build/*"
def build(self):
msbuild = MSBuild(self)
msbuild.build("build/HelloLib/HelloLib.sln")
def package(self):
self.copy("*.h", dst="include", src="src")
self.copy("*.lib", dst="lib", keep_path=False)
def package_info(self):
self.cpp_info.libs = ["HelloLib"]
此配方使用MSBuild()构建助手来构建sln项目。 如果我们的食谱有要求,MSBUILD帮助程序还将负责从需求中注入所有必需的信息,包括目录,库名,定义,标志等,以使我们的项目能够找到声明的依赖项。
该配方还包含一个带有简单示例消费应用程序的test_package文件夹。 在此示例中,使用中的应用程序正在使用CMake进行构建,但它也可以使用Visual Studio。 我们之所以选择CMake,是因为它是用conan new生成的默认值,并且还表明了从Visual Studio项目创建的软件包也可以被CMake等其他构建系统使用。
一旦我们要创建一个程序包,建议关闭VS IDE,从VS中清除临时生成文件以避免出现问题,然后创建并测试该程序包。 这里使用系统默认值,假设它们是Visual Studio 14,发行版,x86_64:
# close VS
$ git clean -xdf
$ conan create . memsharded/testing
...
> Hello World Release!
除了关闭IDE并运行命令:git clean之外,我们还可以在exports_sources字段中配置一个更智能的过滤器,这样就不会将临时构建文件导出到配方中。
可以重复此过程以创建和测试用于不同配置的软件包:
$ conan create . memsharded/testing -s arch=x86
$ conan create . memsharded/testing -s compiler="Visual Studio" -s compiler.runtime=MDd -s build_type=Debug
$ conan create . memsharded/testing -s compiler="Visual Studio" -s compiler.runtime=MDd -s build_type=Debug -s arch=x86
:::info
Note
不必指定editor.runtime设置。 如果未明确定义,Conan将自动将runtime = MDd用于build_type == Debug,将runtime = MD用于build_type == Release。
:::
您可以列出创建的不同二进制包:
$ conan search hello/0.1@memsharded/testing
上传二进制文件
您在本地创建的程序包已经可以上载到Conan遥控器。 如果您使用git clone中的原始用户名“ memsharded”创建了它们,则可能需要创建柯南副本以将其置于您自己的用户名中。 当然,您也可以在柯南创建中直接使用您的用户名。
另一种选择是在远程服务器中配置权限,以允许使用不同的用户名上传软件包。 默认情况下,Artifactory会这样做,但Conan服务器不会:权限必须在server.conf文件的[write_permissions]部分中给出。
重用包
要直接从Visual Studio使用现有包,Conan提供了visual_studio生成器。 让我们克隆一个现有的“ Chat”项目,该项目由使用前一个“ Hello World”包的ChatLib静态库和一个调用ChatLib库函数的MyChat应用程序组成。
$ git clone https://github.com/memsharded/chat_vs
$ cd chat_vs
如上所述,存储库在build文件夹中包含Visual Studio解决方案。 但是,如果您尝试打开它,它将无法加载。 这是因为它期望找到包含有关依赖项的必需信息的文件,因此有必要首先获取该文件。 赶紧跑:
$ conan install .
您将看到它创建了两个文件,一个包含依赖关系当前配置的conaninfo.txt文件,一个包含Visual Studio属性(例如
现在,您可以打开IDE并构建并运行该应用程序(顺便说一句,根据构建类型的不同,chat函数只是调用hello()函数两次或三次):
$ build\ChatLib\ChatLib.sln
# Switch to Release
# MyChat -> Set as Startup Project
# Ctrl + F5 (Debug -> Run without debugging)
> Hello World Release!
> Hello World Release!
如果您想链接到Hello程序包的调试版本,只需安装它并更改IDE构建类型:
$ conan install . -s build_type=Debug -s compiler="Visual Studio" -s compiler.runtime=MDd
# Switch to Debug
# Ctrl + F5 (Debug -> Run without debugging)
> Hello World Debug!
> Hello World Debug!
> Hello World Debug!
现在,您可以关闭IDE并清理临时文件:
# close VS IDE
$ git clean -xdf
再次,存储库中有一个conanfile.py软件包配方,以及一个test_package。 配方几乎与上面的相同,只有两个细微的差别:
requires = "hello/0.1@memsharded/testing"
...
generators = "visual_studio"
这将使我们能够创建和测试ChatLib库的软件包:
$ conan create . memsharded/testing
> Hello World Release!
> Hello World Release!
您还可以针对不同的构建类型和体系结构重复该过程。
其他配置
上面的示例对VS2017照常工作,因为VS支持从早期版本升级。 MSBuild()已经实现了此类功能,因此可以使用VS2017构建和测试程序包。
$ conan create . demo/testing -s compiler="Visual Studio" -s compiler.version=15
如果必须为旧版本的Visual Studio构建,则也可以。 在这种情况下,您的构建文件夹中可能会有不同的解决方案项目。 然后,配方只需选择正确的配方即可,例如:
def build(self):
# assuming HelloLibVS12, HelloLibVS14 subfolders
sln_file = "build/HelloLibVS%s/HelloLib.sln" % self.settings.compiler.version
msbuild = MSBuild(self)
msbuild.build(sln_file)
最后,我们只使用了一个conanbuildinfo.props文件,该文件在全局级别加载。 您还可以定义多个conanbuildinfo.props文件,每个配置一个(发行/调试,x86 / x86_64),然后相应地加载它们。
:::info
Note
到目前为止,visual_studio生成器是单配置的(包含调试或发布工件的软件包,这是通常推荐的方法)。 它不支持多配置程序包(包含调试和发行工件的程序包)。 请报告并提供反馈(在github中提交问题)以在必要时请求此功能。
:::