因为本人自己需要在同一个solution下面做多个project,我用的是VS2015, 语言C++。
首先创建两个project,其中HelloMultiProject作为启动的project,它是exe类型;AnotherProject是dll类型的,如图:
有两种方式来从一个project调用另一个project的方法(或者类)。
1. 如果AnotherProject的配置Configuration Type是Dynamic Library(.dll)
使用dllexport和dllimport
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.youkuaiyun.com/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中的函数了