“添加引用”对话框列出了可以引用的库。 “项目”选项卡列出了当前解决方案中的所有项目,以及它们包含的所有库。 在“项目”选项卡上,选中“MathFuncsDll”旁边的复选框,然后选中“确定”按钮。
若要引用 DLL 的头文件,必须修改包含的目录路径。 为此,请在“属性页”对话框中展开“配置属性”节点,然后展开“C/C++”节点,并选择“常规”。 在“附加包含目录”旁边,指定 MathFuncsDll.h 头文件所在位置的路径。 可以使用相对路径(例如 ..\MathFuncsDll\),然后选择“确定”按钮。
现在即可在此应用程序中使用 MyMathFuncs 类。 使用以下代码替换“MyExecRefsDll.cpp”的内容:
// MyExecRefsDll.cpp // compile with: /EHsc /link MathFuncsDll.lib #include#include "MathFuncsDll.h" using namespace std; int main() { double a = 7.4; int b = 99; cout << "a + b = " << MathFuncs::MyMathFuncs::Add(a, b) << endl; cout << "a - b = " << MathFuncs::MyMathFuncs::Subtract(a, b) << endl; cout << "a * b = " << MathFuncs::MyMathFuncs::Multiply(a, b) << endl; cout << "a / b = " << MathFuncs::MyMathFuncs::Divide(a, b) << endl; try { cout << "a / 0 = " << MathFuncs::MyMathFuncs::Divide(a, 0) << endl; } catch (const invalid_argument &e) { cout << "Caught exception: " << e.what() << endl; } return 0; }
通过在菜单上栏上依次选择“生成”、“生成解决方案”来生成可执行文件。
运行应用程序
确保选择“MyExecRefsDll”作为默认项目。 在“解决方案资源管理器”中,选择 MyExecRefsDll,然后在菜单栏上依次选择“项目”、“设为启动项目”。
若要运行项目,请在菜单栏上依次选择“调试”、“开始执行(不调试)”。 输出应该与下面的内容类似:
a + b = 106.4 a - b = -91.6 a * b = 732.6 a / b = 0.0747475 捕获异常: b 不能为零 !
参考:http://msdn.microsoft.com/zh-cn/library/ms235636.aspx