Visual Stuido (1): 跨项目调用 同一个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;
}
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值