简体   繁体   中英

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. 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.)

If this were, say, a C# project, we could just add a reference. But C++ project properties don't provide such a simple method in this case.

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. 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). 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.

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