简体   繁体   English

如何让新的 Visual Studio C++ 项目了解现有的共享项目?

[英]How to make a new Visual Studio C++ project aware of an existing shared project?

The new(er) Shared C++ Project template in Visual Studio is much easier to use than previous ways code sharing was tackled. Visual Studio 中新的(er)共享 C++ 项目模板比以前处理代码共享的方法更容易使用。 Adding one to your solution is as simple as "Add -> New Project -> Shared Items Project," and voilà.在您的解决方案中添加一个就像“添加 -> 新项目 -> 共享项目项目”一样简单,然后瞧。 Any code inside that project is visible to all your existing projects just as if it existed in their own root directories.该项目中的任何代码对所有现有项目都是可见的,就好像它存在于它们自己的根目录中一样。

Unfortunately, there is one potential hang-up: If you add new projects to your solution afterwards, they don't automagically see the shared project.不幸的是,有一个潜在的问题:如果您之后将新项目添加到您的解决方案中,他们不会自动看到共享项目。 (Not sure if this is true for all forms of VS C++ project templates, but it certainly occurs in the project types we were working with.) (不确定这是否适用于所有 forms 的 VS C++ 项目模板,但它肯定发生在我们正在使用的项目类型中。)

If this were, say, a C# project, we could just add a reference.如果这是一个 C# 项目,我们可以添加一个参考。 But C++ project properties don't provide such a simple method in this case.但是 C++ 项目属性在这种情况下没有提供这么简单的方法。

As fun as it is running down linker errors, having to amend several of the project's dependency paths defeats the entire purpose of the shared project template.尽管运行 linker 错误很有趣,但必须修改项目的几个依赖路径会破坏共享项目模板的全部目的。 I've searched everywhere but I simply cannot find an explanation of how to make a new project "see" an existing shared project.我到处搜索,但我根本找不到关于如何使新项目“看到”现有共享项目的解释。 Is there a simple way to fix this?有没有简单的方法来解决这个问题?

As it happens, there is a quick and simple way to remedy this problem.碰巧的是,有一种快速而简单的方法可以解决这个问题。 All that's needed is a single change to the project file.所需要的只是对项目文件进行一次更改。

Open up the new project's.vcxproj file (as text) and find the tag that reads <ImportGroup Label="Shared"> (for a new project, this tag will likely be empty).打开新项目的 .vcxproj 文件(作为文本)并找到读取<ImportGroup Label="Shared">的标签(对于新项目,此标签可能为空)。 Then, just add your shared project's path like so:然后,只需像这样添加共享项目的路径:

Turn this:转这个:

  <ImportGroup Label="Shared">
  </ImportGroup>

into this:进入这个:

  <ImportGroup Label="Shared">
    <Import Project="some/project/path.vcxitems" Label="Shared" />
  </ImportGroup>

And that's all you should need to do.这就是您需要做的所有事情。 No mucking around with any external dependency paths or other project settings.无需处理任何外部依赖路径或其他项目设置。 Just this one step.就这一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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