如何在Visual Studio中的项目/解决方案之间共享代码?

时间:2022-09-02 09:21:05

I have two solutions which have some common code, so I'd like to extract it out and share it between them. Furthermore, I'd like to be able to release that library independently because it might be useful to others.

我有两个具有公共代码的解决方案,所以我想把它提取出来并在它们之间共享。此外,我希望能够独立地发布该库,因为它可能对其他库有用。

  • What's the best way to do it with Visual Studio 2008?
  • 使用Visual Studio 2008最好的方法是什么?
  • Is a project present in more than one solution?
  • 一个项目是否存在于多个解决方案中?
  • Do I have a separate solution for the separate piece of code?
  • 对于单独的代码块,我有单独的解决方案吗?
  • Can a solution depend on another one?
  • 一个解能依赖于另一个解吗?

15 个解决方案

#1


68  

A project can be referenced by multiple solutions.

一个项目可以被多个解决方案引用。

Put your library or core code into one project, then reference that project in both solutions.

将您的库或核心代码放入一个项目中,然后在两个解决方案中引用该项目。

#2


231  

You can "link" a code file between two projects. Right click your project, choose Add -> Existing item, and then click the down arrow next to the Add button:

您可以在两个项目之间“链接”代码文件。右键单击您的项目,选择Add ->现有项目,然后单击Add按钮旁边的向下箭头:

如何在Visual Studio中的项目/解决方案之间共享代码?

In my experience linking is simpler than creating a library. Linked code results in a single executable with a single version.

根据我的经验,链接比创建库更简单。链接代码产生一个可执行文件和一个版本。

#3


29  

File > Add > Existing Project... will let you add projects to your current solution. Just adding this since none of the above posts point that out. This lets you include the same project in multiple solutions.

文件>添加>现有项目…将会让您将项目添加到当前的解决方案中。因为上面的文章都没有指出这一点。这使您可以在多个解决方案中包含相同的项目。

#4


24  

You can include a project in more than one solution. I don't think a project has a concept of which solution it's part of. However, another alternative is to make the first solution build to some well-known place, and reference the compiled binaries. This has the disadvantage that you'll need to do a bit of work if you want to reference different versions based on whether you're building in release or debug configurations.

您可以在多个解决方案中包含一个项目。我认为一个项目并不知道它属于哪一种解决方案。但是,另一种方法是将第一个解决方案构建到某个已知的位置,并引用已编译的二进制文件。这有一个缺点,如果您希望根据您是在发布版中构建还是在调试配置中构建不同的版本,那么您将需要做一些工作。

I don't believe you can make one solution actually depend on another, but you can perform your automated builds in an appropriate order via custom scripts. Basically treat your common library as if it were another third party dependency like NUnit etc.

我不相信您可以使一个解决方案实际上依赖于另一个解决方案,但是您可以通过自定义脚本以适当的顺序执行您的自动构建。基本上把你的公共库当作另一个第三方依赖,比如NUnit等等。

#5


22  

You can wild-card inline using the following technique (which is the way in which @Andomar's solution is saved in the .csproj)

您可以使用以下技术(这是将@Andomar的解决方案保存在.csproj中的方式)内联进行wilcard内联

<Compile Include="..\MySisterProject\**\*.cs">
  <Link>_Inlined\MySisterProject\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>

Put in:

投入:

    <Visible>false</Visible>

If you want to hide the files and/or prevent the wild-card include being expanded if you add or remove an item from a 'virtual existing item' folder like MySisterProject above.

如果您想隐藏文件和/或防止外卡包含展开,如果您添加或删除一个项目从“虚拟现有项目”文件夹,如上面的mysister项目。

#6


17  

You would simply create a separate Class Library project to contain the common code. It need not be part of any solution that uses it. Reference the class library from any project that needs it.

您只需创建一个单独的类库项目来包含公共代码。它不需要是任何使用它的解决方案的一部分。从需要它的任何项目中引用类库。

The only trick at all is that you will need to use a file reference to reference the project, since it will not be part of the solutions that refer to it. This means that the actual output assembly will have to be placed in a location that can be accessed by anyone building a project that references it. This can be done by placing the assembly on a share, for instance.

唯一的诀窍是,您将需要使用一个文件引用来引用这个项目,因为它不会是引用它的解决方案的一部分。这意味着实际的输出程序集必须放置在任何构建引用它的项目的人都可以访问的位置。例如,可以通过将程序集放在共享上实现。

#7


8  

Extract the common code into a class library project and add that class library project to your solutions. Then you can add a reference to the common code from other projects by adding a project reference to that class library. The advantage of having a project reference as opposed to a binary/assembly reference is that if you change your build configuration to debug, release, custom, etc, the common class library project will be built based on that configuration as well.

将公共代码提取到类库项目中,并将该类库项目添加到解决方案中。然后,您可以通过向该类库添加项目引用来添加对其他项目的公共代码的引用。与二进制/汇编引用相比,具有项目引用的优点是,如果您将构建配置更改为debug、release、custom等,那么公共类库项目也将基于该配置构建。

#8


7  

You could include the same project in more than one solution, but you're guaranteed to run into problems sometime down the road (relative paths can become invalid when you move directories around for example)

您可以将相同的项目包含在多个解决方案中,但是您肯定会在以后的某个时候遇到问题(例如,当您移动目录时,相对路径可能会变得无效)

After years of struggling with this, I finally came up with a workable solution, but it requires you to use Subversion for source control (which is not a bad thing)

经过多年的努力,我终于找到了一个可行的解决方案,但它要求您使用Subversion来进行源代码控制(这并不是一件坏事)

At the directory level of your solution, add a svn:externals property pointing to the projects you want to include in your solution. Subversion will pull the project from the repository and store it in a subfolder of your solution file. Your solution file can simply use relative paths to refer to your project.

在解决方案的目录级别上,添加一个svn:externals属性指向要包含在解决方案中的项目。Subversion将从存储库中提取项目并将其存储在解决方案文件的子文件夹中。您的解决方案文件可以简单地使用相对路径来引用您的项目。

If I find some more time, I'll explain this in detail.

如果我再多找点时间,我会详细解释的。

#9


5  

It is a good idea to create a dll class library that contain all common functionality. Each solution can reference this dll indepently regardless of other solutions.

创建一个包含所有公共功能的dll类库是一个好主意。无论其他解决方案是什么,每个解决方案都可以独立地引用这个dll。

Infact , this is how our sources are organized in my work (and I believe in many other places).

事实上,这就是我们的资源在我的工作中是如何组织的(我相信在许多其他地方)。

By the way , Solution can't explicitly depend on another solution.

顺便说一下,解决方案不能显式依赖于另一个解决方案。

#10


4  

If you're attempting to share code between two different project types (I.e.: desktop project and a mobile project), you may look into the shared solutions folder. I have to do that for my current project as the mobile and desktop projects both require identical classes that are only in 1 file. If you go this route, any of the projects that have the file linked can make changes to it and all of the projects will be rebuilt against those changes.

如果您试图在两个不同的项目类型之间共享代码(例如。:桌面项目和移动项目),您可以查看共享解决方案文件夹。我必须为我当前的项目做这个,因为移动和桌面项目都需要相同的类,它们只有一个文件。如果您走这条路,任何链接文件的项目都可以对其进行更改,所有的项目都将根据这些更改进行重新构建。

#11


4  

There is a very good case for using "adding existing file links" when reusing code across projects, and that is when you need to reference and support different versions of dependent libraries.

在跨项目重用代码时,使用“添加现有文件链接”是一个很好的例子,这是您需要引用和支持不同版本的依赖库的时候。

Making multiple assemblies with references to different external assemblies isn't easy to do otherwise without duplicating your code, or utilizing tricks with source code control.

使用对不同外部程序集的引用来创建多个程序集不是一件容易的事情,否则不需要重复您的代码,或者使用源代码控制的技巧。

I believe that it's easiest to maintain one project for development and unit test, then to create 'build' projects using existing file links when you need to create the assemblies which reference different versions of those external assemblies.

我认为,在需要创建引用这些外部程序集的不同版本的程序集时,维护一个用于开发和单元测试的项目,然后使用现有的文件链接创建“构建”项目是最容易的。

#12


2  

One simpler way to include a class file of one project in another projects is by Adding the project in existing solution and then Adding the DLL reference of the new project in the existing project. Finally, you can use the methods of the added class by decalring using directive at the top of the any class.

在另一个项目中包含一个项目的类文件的一种更简单的方法是在现有解决方案中添加项目,然后在现有项目中添加新项目的DLL引用。最后,您可以使用在任何类顶部使用指令的decalring来使用添加类的方法。

#13


2  

Two main steps involved are

所涉及的两个主要步骤是

1- Creating a C++ dll

1-创建一个c++ dll

In visual studio

在visual studio

New->Project->Class Library in c++ template. Name of project here is first_dll in 
visual studio 2010. Now declare your function as public in first_dll.h file and 
write the code in first_dll.cpp file as shown below.

Header File code

头文件代码

// first_dll.h

using namespace System;

namespace first_dll 
{

public ref class Class1
{
public:
    static double sum(int ,int );
    // TODO: Add your methods for this class here.
};
}

Cpp File

Cpp文件

//first_dll.cpp
#include "stdafx.h"

#include "first_dll.h"

namespace first_dll
{

    double Class1:: sum(int x,int y)
    {
        return x+y;
    }

 }

Check This

检查这个

**Project-> Properties -> Configuration/General -> Configuration Type** 

this option should be Dynamic Library(.dll) and build the solution/project now.

这个选项应该是动态库(.dll),现在构建解决方案/项目。

first_dll.dll file is created in Debug folder

first_dll。dll文件在调试文件夹中创建

2- Linking it in C# project

2-在c#项目中链接它

Open C# project

开放的c#项目

Rightclick on project name in solution explorer -> Add -> References -> Browse to path 
where first_dll.dll is created and add the file.

Add this line at top in C# project

在c#项目的顶部添加这一行

Using first_dll; 

Now function from dll can be accessed using below statement in some function

现在,在某些函数中,可以使用下面的语句访问dll中的函数

double var = Class1.sum(4,5);

I created dll in c++ project in VS2010 and used it in VS2013 C# project.It works well.

我在VS2010的c++项目中创建了dll,并在VS2013的c#项目中使用。它的工作原理。

#14


2  

You can host an internal NuGet Server and share the common libraries that will be shared in other projects internally and externally.

您可以托管一个内部NuGet服务器,并共享在其他项目内部和外部共享的公共库。

Further on this read

进一步在这读

#15


0  

As of VisualStudio 2015, if you keep all your code in one solution, you can share code by adding a shared project. Then add a reference to this shared project for each project you want to use the code in, as well as the proper using directives.

从VisualStudio 2015开始,如果您将所有代码保存在一个解决方案中,您可以通过添加共享项目来共享代码。然后为您想要使用代码的每个项目添加对这个共享项目的引用,以及正确的使用指令。

#1


68  

A project can be referenced by multiple solutions.

一个项目可以被多个解决方案引用。

Put your library or core code into one project, then reference that project in both solutions.

将您的库或核心代码放入一个项目中,然后在两个解决方案中引用该项目。

#2


231  

You can "link" a code file between two projects. Right click your project, choose Add -> Existing item, and then click the down arrow next to the Add button:

您可以在两个项目之间“链接”代码文件。右键单击您的项目,选择Add ->现有项目,然后单击Add按钮旁边的向下箭头:

如何在Visual Studio中的项目/解决方案之间共享代码?

In my experience linking is simpler than creating a library. Linked code results in a single executable with a single version.

根据我的经验,链接比创建库更简单。链接代码产生一个可执行文件和一个版本。

#3


29  

File > Add > Existing Project... will let you add projects to your current solution. Just adding this since none of the above posts point that out. This lets you include the same project in multiple solutions.

文件>添加>现有项目…将会让您将项目添加到当前的解决方案中。因为上面的文章都没有指出这一点。这使您可以在多个解决方案中包含相同的项目。

#4


24  

You can include a project in more than one solution. I don't think a project has a concept of which solution it's part of. However, another alternative is to make the first solution build to some well-known place, and reference the compiled binaries. This has the disadvantage that you'll need to do a bit of work if you want to reference different versions based on whether you're building in release or debug configurations.

您可以在多个解决方案中包含一个项目。我认为一个项目并不知道它属于哪一种解决方案。但是,另一种方法是将第一个解决方案构建到某个已知的位置,并引用已编译的二进制文件。这有一个缺点,如果您希望根据您是在发布版中构建还是在调试配置中构建不同的版本,那么您将需要做一些工作。

I don't believe you can make one solution actually depend on another, but you can perform your automated builds in an appropriate order via custom scripts. Basically treat your common library as if it were another third party dependency like NUnit etc.

我不相信您可以使一个解决方案实际上依赖于另一个解决方案,但是您可以通过自定义脚本以适当的顺序执行您的自动构建。基本上把你的公共库当作另一个第三方依赖,比如NUnit等等。

#5


22  

You can wild-card inline using the following technique (which is the way in which @Andomar's solution is saved in the .csproj)

您可以使用以下技术(这是将@Andomar的解决方案保存在.csproj中的方式)内联进行wilcard内联

<Compile Include="..\MySisterProject\**\*.cs">
  <Link>_Inlined\MySisterProject\%(RecursiveDir)%(Filename)%(Extension)</Link>
</Compile>

Put in:

投入:

    <Visible>false</Visible>

If you want to hide the files and/or prevent the wild-card include being expanded if you add or remove an item from a 'virtual existing item' folder like MySisterProject above.

如果您想隐藏文件和/或防止外卡包含展开,如果您添加或删除一个项目从“虚拟现有项目”文件夹,如上面的mysister项目。

#6


17  

You would simply create a separate Class Library project to contain the common code. It need not be part of any solution that uses it. Reference the class library from any project that needs it.

您只需创建一个单独的类库项目来包含公共代码。它不需要是任何使用它的解决方案的一部分。从需要它的任何项目中引用类库。

The only trick at all is that you will need to use a file reference to reference the project, since it will not be part of the solutions that refer to it. This means that the actual output assembly will have to be placed in a location that can be accessed by anyone building a project that references it. This can be done by placing the assembly on a share, for instance.

唯一的诀窍是,您将需要使用一个文件引用来引用这个项目,因为它不会是引用它的解决方案的一部分。这意味着实际的输出程序集必须放置在任何构建引用它的项目的人都可以访问的位置。例如,可以通过将程序集放在共享上实现。

#7


8  

Extract the common code into a class library project and add that class library project to your solutions. Then you can add a reference to the common code from other projects by adding a project reference to that class library. The advantage of having a project reference as opposed to a binary/assembly reference is that if you change your build configuration to debug, release, custom, etc, the common class library project will be built based on that configuration as well.

将公共代码提取到类库项目中,并将该类库项目添加到解决方案中。然后,您可以通过向该类库添加项目引用来添加对其他项目的公共代码的引用。与二进制/汇编引用相比,具有项目引用的优点是,如果您将构建配置更改为debug、release、custom等,那么公共类库项目也将基于该配置构建。

#8


7  

You could include the same project in more than one solution, but you're guaranteed to run into problems sometime down the road (relative paths can become invalid when you move directories around for example)

您可以将相同的项目包含在多个解决方案中,但是您肯定会在以后的某个时候遇到问题(例如,当您移动目录时,相对路径可能会变得无效)

After years of struggling with this, I finally came up with a workable solution, but it requires you to use Subversion for source control (which is not a bad thing)

经过多年的努力,我终于找到了一个可行的解决方案,但它要求您使用Subversion来进行源代码控制(这并不是一件坏事)

At the directory level of your solution, add a svn:externals property pointing to the projects you want to include in your solution. Subversion will pull the project from the repository and store it in a subfolder of your solution file. Your solution file can simply use relative paths to refer to your project.

在解决方案的目录级别上,添加一个svn:externals属性指向要包含在解决方案中的项目。Subversion将从存储库中提取项目并将其存储在解决方案文件的子文件夹中。您的解决方案文件可以简单地使用相对路径来引用您的项目。

If I find some more time, I'll explain this in detail.

如果我再多找点时间,我会详细解释的。

#9


5  

It is a good idea to create a dll class library that contain all common functionality. Each solution can reference this dll indepently regardless of other solutions.

创建一个包含所有公共功能的dll类库是一个好主意。无论其他解决方案是什么,每个解决方案都可以独立地引用这个dll。

Infact , this is how our sources are organized in my work (and I believe in many other places).

事实上,这就是我们的资源在我的工作中是如何组织的(我相信在许多其他地方)。

By the way , Solution can't explicitly depend on another solution.

顺便说一下,解决方案不能显式依赖于另一个解决方案。

#10


4  

If you're attempting to share code between two different project types (I.e.: desktop project and a mobile project), you may look into the shared solutions folder. I have to do that for my current project as the mobile and desktop projects both require identical classes that are only in 1 file. If you go this route, any of the projects that have the file linked can make changes to it and all of the projects will be rebuilt against those changes.

如果您试图在两个不同的项目类型之间共享代码(例如。:桌面项目和移动项目),您可以查看共享解决方案文件夹。我必须为我当前的项目做这个,因为移动和桌面项目都需要相同的类,它们只有一个文件。如果您走这条路,任何链接文件的项目都可以对其进行更改,所有的项目都将根据这些更改进行重新构建。

#11


4  

There is a very good case for using "adding existing file links" when reusing code across projects, and that is when you need to reference and support different versions of dependent libraries.

在跨项目重用代码时,使用“添加现有文件链接”是一个很好的例子,这是您需要引用和支持不同版本的依赖库的时候。

Making multiple assemblies with references to different external assemblies isn't easy to do otherwise without duplicating your code, or utilizing tricks with source code control.

使用对不同外部程序集的引用来创建多个程序集不是一件容易的事情,否则不需要重复您的代码,或者使用源代码控制的技巧。

I believe that it's easiest to maintain one project for development and unit test, then to create 'build' projects using existing file links when you need to create the assemblies which reference different versions of those external assemblies.

我认为,在需要创建引用这些外部程序集的不同版本的程序集时,维护一个用于开发和单元测试的项目,然后使用现有的文件链接创建“构建”项目是最容易的。

#12


2  

One simpler way to include a class file of one project in another projects is by Adding the project in existing solution and then Adding the DLL reference of the new project in the existing project. Finally, you can use the methods of the added class by decalring using directive at the top of the any class.

在另一个项目中包含一个项目的类文件的一种更简单的方法是在现有解决方案中添加项目,然后在现有项目中添加新项目的DLL引用。最后,您可以使用在任何类顶部使用指令的decalring来使用添加类的方法。

#13


2  

Two main steps involved are

所涉及的两个主要步骤是

1- Creating a C++ dll

1-创建一个c++ dll

In visual studio

在visual studio

New->Project->Class Library in c++ template. Name of project here is first_dll in 
visual studio 2010. Now declare your function as public in first_dll.h file and 
write the code in first_dll.cpp file as shown below.

Header File code

头文件代码

// first_dll.h

using namespace System;

namespace first_dll 
{

public ref class Class1
{
public:
    static double sum(int ,int );
    // TODO: Add your methods for this class here.
};
}

Cpp File

Cpp文件

//first_dll.cpp
#include "stdafx.h"

#include "first_dll.h"

namespace first_dll
{

    double Class1:: sum(int x,int y)
    {
        return x+y;
    }

 }

Check This

检查这个

**Project-> Properties -> Configuration/General -> Configuration Type** 

this option should be Dynamic Library(.dll) and build the solution/project now.

这个选项应该是动态库(.dll),现在构建解决方案/项目。

first_dll.dll file is created in Debug folder

first_dll。dll文件在调试文件夹中创建

2- Linking it in C# project

2-在c#项目中链接它

Open C# project

开放的c#项目

Rightclick on project name in solution explorer -> Add -> References -> Browse to path 
where first_dll.dll is created and add the file.

Add this line at top in C# project

在c#项目的顶部添加这一行

Using first_dll; 

Now function from dll can be accessed using below statement in some function

现在,在某些函数中,可以使用下面的语句访问dll中的函数

double var = Class1.sum(4,5);

I created dll in c++ project in VS2010 and used it in VS2013 C# project.It works well.

我在VS2010的c++项目中创建了dll,并在VS2013的c#项目中使用。它的工作原理。

#14


2  

You can host an internal NuGet Server and share the common libraries that will be shared in other projects internally and externally.

您可以托管一个内部NuGet服务器,并共享在其他项目内部和外部共享的公共库。

Further on this read

进一步在这读

#15


0  

As of VisualStudio 2015, if you keep all your code in one solution, you can share code by adding a shared project. Then add a reference to this shared project for each project you want to use the code in, as well as the proper using directives.

从VisualStudio 2015开始,如果您将所有代码保存在一个解决方案中,您可以通过添加共享项目来共享代码。然后为您想要使用代码的每个项目添加对这个共享项目的引用,以及正确的使用指令。