首页 > 学院 > 开发设计 > 正文

Visual Stuido (1): 跨项目调用 同一个solution下面不同project之间的方法调用

2019-11-11 05:10:32
字体:
来源:转载
供稿:网友

因为本人自己需要在同一个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;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表