【发布时间】:2016-07-14 20:23:24
【问题描述】:
我用 C 语言创建了一个可以找到矩阵行列式的程序
void fun1(); /*regardless of parameters they take*/
void fun2();
void fun3();
void fun4();
void fun5();
int global_var1; /*they are used among the above functions*/
int global_var2;
int global_var3;
int determinant;
int main(void){
int x,y;
for(x=0; x<something ; x++){
for (y=0 ; y<another_thing; y++){
fun1();
fun2();
fun3();
}
fun4();
fun5();
}
printf("Determinant is: %d\n", determinant);
return 0;
}
void fun1(){/*code goes here to process the matrix*/;}
void fun2(){/*code goes here to process the matrix*/;}
void fun3(){/*code goes here to process the matrix*/;}
void fun4(){/*code goes here to process the matrix*/;}
void fun5(){/*code goes here to process the matrix*/;}
现在我需要使用这个程序在另一个项目中找到给定矩阵的行列式。
我创建了一个头文件,将其命名为"matrix.h",并将int main(void) 替换为int find_determinant(),以便在新项目中使用。
第二个程序的原型:
#include "matrix.h"
int main(void){
find_determinant(); /*regardless of para it takes, it works perfectly*/
return 0;}
它工作正常,没什么问题,但唯一的问题是如果我想把这个头文件"matrix.h给某人在他的程序中使用它,他可以知道其他函数的签名(对他一个人没用并且令人困惑)用于帮助找到 int find_determinant() 中的行列式。
我的问题是:
如何隐藏(使它们无法访问)这些函数和全局变量,并在包含 #include "matrix.h" 的第二个 C 文件/程序中仅显示 int find_determinant() 函数?
【问题讨论】:
-
1.不要将仅在内部使用的内容放在标题中。 2. 不需要用户访问的全局变量和函数(仅供内部使用)应标记为
static。 3. do 需要被外部访问的全局变量应该在头文件中用extern声明,并在.c文件中定义。 4. 你不应该使用这么多的全局变量,应该让函数通过函数参数和返回值传递它们需要的数据。 -
A similar question here 但我确信我看到了更多相关信息。提供函数来访问您想要隐藏的内容。