VS将project编译为dll或lib文件供solution下其他project调用

因为本人自己需要在同一个solution下面做多个project,我用的是VS2015, 语言C++。
首先创建两个project,其中HelloMultiProject作为启动的project,它是exe类型;AnotherProject是dll类型的,如图:
这里写图片描述

有两种方式来从一个project调用另一个project的方法(或者类)。

1. 如果AnotherProject的配置Configuration Type是Dynamic Library(.dll)

使用dllexportdllimport
AnotherProject的配置:
这里写图片描述
HelloMultiProject不需要配置。

// Another project

//FindMax.h
#pragma once
#include "stdafx.h"
_declspec(dllexport) int findMax(int i, int j);

//FindMax.cpp
#include "stdafx.h"
#include "FindMax.h"
int findMax(int i, int j)
{
   return i > j ? i : j;
}

// HelloMultiProject
// HelloMultiProject.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

_declspec(dllimport) int findMax(int i, int j);

int main()
{
   findMax(1, 2);
    return 0;
}

2. 如果AnotherProject的配置Configuration Type是Static Library(.lib)

AnotherProject的配置:
这里写图片描述

HelloMultiProject的配置:
这里写图片描述

// Another project

//FindMax.h
#pragma once
#include "stdafx.h"
int findMax(int i, int j);

//FindMax.cpp
#include "stdafx.h"
#include "FindMax.h"
int findMax(int i, int j)
{
   return i > j ? i : j;
}

// HelloMultiProject
// HelloMultiProject.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "FindMax.h"

int main()
{
   findMax(1, 2);
    return 0;
}

转载自https://blog.csdn.net/anyicheng2015/article/details/54894799

转载按:第一种情况,如果AnotherProject的配置Configuration Type是Dynamic Library(.dll)时,还可以不写

_declspec(dllimport) int findMax(int i, int j);

改为添加头文件引用:

#include "FindMax.h"

因为

#include "FindMax.h",这个虽然不是必须的,没有这个文件也可以,没有的话自己写上“_declspec(dllimport) int findMax(int a, int b);”也可以调用dll中的函数了,但是,如果有的话,使用者什么也不用做,引用上这个头文件dll.h之后就可以放心地使用dll中的函数了

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值