因为本人自己需要在同一个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;
}