【C语言航路外传】如何隐藏代码及声明和定义的在工程中真正的使用场景

在之前函数第二幕的时候我们提到过函数的声明和定义,其中有一种方式是这样使用的,如下代码所示

#include<stdio.h>
int Add(int x,int y);
int main()
{
	int a = 0;
	int b = 0;
	scanf("%d %d", &a, &b);
	int ret = Add(a, b);
	printf("%d", ret);
	return 0;
}
int Add(int x,int y)
{
	return x + y;
}

这时候,有人就觉得这个也没有什么大用处啊,直接都放在开头不行吗?何必多此一举呢?,其实这种声明定义函数的方式在实际工程中并非这样使用的。在真正的项目中我们是这样使用的

我们在多放入一个头文件和一个.c文件,add.h和add.c。

 然后我们把加法函数,放在add.c中

 把函数的声明放在add.h中

 这样的话,如果我们想在我们的工程中使用Add这个函数,我们只需要在test.c中#include "add.h"即可,如下图所示

 这样我们试着运行一下,代码仍然正常运行

 上面的这种操作中:

我们把add.h和add.c称作一个模块,test.c是一个模块

 add.h和add.c为加法模块,test.c为测试模块,也就是test.c就是为了测试一下加法模块

在加法模块中,我们一般是在头文件中声明函数,.c文件中是实现函数,然后测试模块,只需要引用一下头文件,编译器就会自动将三个文件组合到一块。有点类似于使用库函数,只不过这个库函数是由我们自己制作的。但是要注意,使用c语言提供的库函数,是引用 <>这个括号,而我们自己造的库函数是需要使用 '' "的。

那么这样写有什么好处呢?

1.这样写可以是我们的程序模块化

比如说我们有一个加减乘除的程序,我们利用这种方式可以进行分工,一个人写一个模块即可。这样的话,每个人只需要开发自己的模块就可以,最后我们一调用他们的模块就可以了。我们这样就实现了模块发开发了。

2.这样写也可以隐藏我们的代码

我们之前说过,想要调用同一个项目的不同源文件的函数,我们可以使用一个extern声明一下函数就可以,那为什么我们又要搞一个头文件,然后引用呢?,原因是是由头文件可以做到相关代码的隐藏。

我们举一个例子吧,假如说张三写了一个代码,他想把这个代码卖给一个公司,但是又不能直接给源代码,因为明年没法收费了。那该怎么解决呢?

假如说这是我们所写的代码

 我们是这样做的

我们点击这个

 改为静态库

 应用确定后

我们运行一下程序,得到下列错误信息

 这时候我们就生成了我们的静态库了

我们在上面提示的路径中找到我们的文件

 这时候我们就可以把这个.lib文件和我们的头文件一块卖出去了,这个是没有人看得懂的,我们用记事本打开看一下

 买家将买到的静态库和头文件放到文件中

我们直接使用是不行的

 会出现无法解析的外部命令,这时候我们只需要输入一条指令,导入静态库即可

 这样代码就能正常运行了


总结:

本次外传主要讲解了定义和声明在工程中的重要作用,以及如何隐藏自己的代码。当然静态库的使用并不是本节课的重点,主要是为了演示定义和声明的重要作用,以后会详细介绍关于静态库的使用的

  • 19
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论
MICROSOFT FOUNDATION CLASS LIBRARY : ShieldPower AppWizard has created this ShieldPower application for you. This application not only demonstrates the basics of using the Microsoft Foundation classes but is also a starting point for writing your application. This file contains a summary of what you will find in each of the files that make up your ShieldPower application. ShieldPower.dsp This file (the project file) contains information at the project level and is used to build a single project or subproject. Other users can share the project (.dsp) file, but they should export the makefiles locally. ShieldPower.h This is the main header file for the application. It includes other project specific headers (including Resource.h) and declares the CShieldPowerApp application class. ShieldPower.cpp This is the main application source file that contains the application class CShieldPowerApp. ShieldPower.rc This is a listing of all of the Microsoft Windows resources that the program uses. It includes the icons, bitmaps, and cursors that are stored in the RES subdirectory. This file can be directly edited in Microsoft Visual C++. ShieldPower.clw This file contains information used by ClassWizard to edit existing classes or add new classes. ClassWizard also uses this file to store information needed to create and edit message maps and dialog data maps and to create prototype member functions. res\ShieldPower.ico This is an icon file, which is used as the application's icon. This icon is included by the main resource file ShieldPower.rc. res\ShieldPower.rc2 This file contains resources that are not edited by Microsoft Visual C++. You should place all resources not editable by the resource editor in this file. ///////////////////////////////////////////////////////////////////////////// AppWizard creates one dialog class: ShieldPowerDlg.h, ShieldPowerDlg.cpp - the dialog These files contain your CShieldPowerDlg class. This class defines the behavior of your application's main dialog. The dialog's template is in ShieldPower.rc, which can be edited in Microsoft Visual C++. ///////////////////////////////////////////////////////////////////////////// Other standard files: StdAfx.h, StdAfx.cpp These files are used to build a precompiled header (PCH) file named ShieldPower.pch and a precompiled types file named StdAfx.obj. Resource.h This is the standard header file, which defines new resource IDs. Microsoft Visual C++ reads and updates this file. ///////////////////////////////////////////////////////////////////////////// Other notes: AppWizard uses "TODO:" to indicate parts of the source code you should add to or customize. If your application uses MFC in a shared DLL, and your application is in a language other than the operating system's current language, you will need to copy the corresponding localized resources MFC42XXX.DLL from the Microsoft Visual C++ CD-ROM onto the system or system32 directory, and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation. For example, MFC42DEU.DLL contains resources translated to German.) If you don't do this, some of the UI elements of your application will remain in the language of the operating system. /////////////////////////////////////////////////////////////////////////////

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青色_忘川

你的鼓励是我创作的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值