新建win控制台应用程序完成简单加法
学vs2015的目的是为了使用python调试一个C++的摄像头demo,理论上是可行的,
但实在是无处下手,于是决定从基础下手,先写一个简单的c++程序,尝试使用python调用。
有不足或错误的地方,烦请指正。
新建项目
vs2015右上角,文件>>新建>>项目
下面的名称自选,未知位置自选
点击下一步  下图中有空项目选项,选择后即项目中不会出现stdafx.h/.cpp文件和targetver.h文件。自行选择。  选好后,点击完成,新项目建成。如下图。  我们在右侧头文件上单击右键,添加>>新建项>>头文件(.h)。名字自选。 在源文件中看到刚刚自动生成的Console…om2.cpp文件,将其名字改为与自己想要的。 此处按我自己想法改动过,得到结果如下:  接下来写头文件test.h
#pragma once
_declspec(dllexport) int myAdd(int a, int b);
_declspec(dllexport) int myMax(int a, int b);
然后在test_dll.cpp中写对应函数:
// test_dll.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdlib.h"
#include "test.h"
#include "iostream"
using namespace std;
int myAdd(int a, int b)
{
return a + b;
}
int myMax(int a, int b)
{
return a > b ? a : b;
}
int main()
{
int i, j;
int a1 = 1;
int b1 = 2;
i = myAdd(a1 , b1);
j = myMax(a1 , b1);
cout << i << endl;
cout << j << endl;
system("pause");
return 0;
}
生成dll和exe
 此处记得选择X64;然后点击生成>>生成解决方案 输出成功则成功。 有时会出现不生成dll或exe的情况。  右键单机工程,选择属性,在途中位置可以选择生成exe,dll等。 
运行程序
可以在vs中直接点击本地windows调试器

可以在文件夹中找到.exe的文件,双击。
最终得到结果都如下:  本文方法,程序主要参考以下网址,或有遗漏,请见谅。
链接: VS2015 创建并使用自己的DLL(一个简单的示例). 链接: 【C++】第1章 在VS2015中用C++编写控制台应用程序.
|