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

用Visual Studio 2008最好的方法是什么? 一个项目是否存在于多个解决方案中? 对于这段单独的代码,我有单独的解决方案吗? 一个解决方案能依赖于另一个解决方案吗?


当前回答

File > Add > Existing Project…将允许您向当前解决方案添加项目。只是加上这个,因为上面的帖子都没有指出这一点。这允许您在多个解决方案中包含相同的项目。

其他回答

现在您可以使用共享项目了

Shared Project is a great way of sharing common code across multiple application We already have experienced with the Shared Project type in Visual Studio 2013 as part of Windows 8.1 Universal App Development, But with Visual Studio 2015, it is a Standalone New Project Template; and we can use it with other types of app like Console, Desktop, Phone, Store App etc.. This types of project is extremely helpful when we want to share a common code, logic as well as components across multiple applications with in single platform. This also allows accessing the platform-specific API ’s, assets etc.

更多信息请看这个

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

再往下读

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

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

您可以在多个解决方案中包含一个项目。我不认为一个项目有属于哪个解决方案的概念。然而,另一种替代方法是将第一个解决方案构建到一些众所周知的地方,并引用已编译的二进制文件。这样做的缺点是,如果您想根据您构建的是发布配置还是调试配置来引用不同的版本,那么您将需要做一些工作。

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

涉及的两个主要步骤是

1-创建c++ dll

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

文件代码

// 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文件

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

#include "first_dll.h"

namespace first_dll
{

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

 }

检查这个

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

这个选项应该是Dynamic Library(.dll),现在就构建解决方案/项目。

在Debug文件夹中创建first_dll.dll文件

2-在c#项目中链接它

开放c#项目

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

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

Using first_dll; 

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

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

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