如何在一个文件中定义变量并在另一个文件中使用它
这个问题可能非常基本,但我无论如何都没有得到线索......
我有两个文件...... 我正在提到我想做的事情。
文件 1
...
j = data->alloc_len;
...
文件 2
...
for(i=0;i<j;i++)
...
从上面可以清楚地看出,我想为一个文件中的变量赋值,并想在其他文件中使用该值。
我在 file2.c 中尝试了 #include "file1.c"
但它给出了很多重新声明错误。
我尝试创建一个单独的头文件,其中只有一行 int j
,然后使用 extern
将其包含在两个文件中,但再次没有任何好处。虽然我认为头文件是用于我可以在其中创建一个值并将其分配给一个文件中的变量,然后可以通过包含此文件将该值传播到所有其他文件。
也许我错了,但我很快就需要它,所以请帮助我......提前谢谢。
限制 -
只能通过 file1.c
赋值,因为 data
结构仅在此处声明和定义。我无法向头文件中的变量 j
。
编辑:
虽然我提到过,但我认为我无法清除我的问题。我尝试过使用它的头文件。
出于调试目的,我尝试了这个..
sample.h
extern int j;
file1.c
#include "sample.h"
int j=245;
file2.c
#include "sample.h"
printf("%d",j);
但出现错误。
我得到的错误是:
Couldn't open module sample.so, tried:
sample.so: cannot open shared object file: No such file or directory
./sample.so.so: cannot open shared object file: No such file or directory
/usr/local/lib/sendip/sample.so.so:
cannot open shared object file: No such file or
directory
/usr/local/lib/sendip/sample.so: undefined symbol: j
*没有一个文件实际上包含主函数*
实际上这是一个非常大的项目,我正在使用Makefile
并且所有文件都将在运行时链接时间。
简而言之,执行过程可以理解为有一个 main.c
文件,其中包含 main
,该文件依次调用 file1.c
并在其中调用 file1.c
。转调用 file2.c
关于描述性名称,我想说它们只是为了在此处显示,否则我已经在使用描述性名称了。
The question may be very basic but I am not getting clue anyways ...
I have two files ...
and I am mentioning what I want to do .
file 1
...
j = data->alloc_len;
...
file 2
...
for(i=0;i<j;i++)
...
Its clear from above I want to assign value to a variable in one file and want to use that value in other file.
I tried #include "file1.c"
in file2.c but it is giving lot of re-declaration errors.
I tried creating a seperate header file which only have one line int j
and then included it in both files using extern
but no benefit again.Although I think header files are meant for where i can create and assign a value to a variable in one file and then this value can be propogated to all other files by including this one.
May be I am wrong but I need it soon so please help me ...Thnx in advance.
Limitation -
The value can be assigned only through file1.c
because data
structure is declared and defined here only.I can not provide a value to variable j
in a header file .
EDIT :
Although I mentioned but I think I could not clear my question.I have tried using it header files.
For debugging purpose I tried this ..
sample.h
extern int j;
file1.c
#include "sample.h"
int j=245;
file2.c
#include "sample.h"
printf("%d",j);
but its getting error .
Error I am getting is :
Couldn't open module sample.so, tried:
sample.so: cannot open shared object file: No such file or directory
./sample.so.so: cannot open shared object file: No such file or directory
/usr/local/lib/sendip/sample.so.so:
cannot open shared object file: No such file or
directory
/usr/local/lib/sendip/sample.so: undefined symbol: j
*none of the file contains main function actually *
Actually it is a very large project and I am using Makefile
and all files will be linked at run time.
In short,the execution could be understood as there is a main.c
file which contains main
which in turns call file1.c
and which in turn calls file2.c
About descriptive names I would say they are just for showing here otherwise I am already using descriptive name.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以将其放入头文件中:
并且仅在 file1.c 中声明“真实”
j
。如果 file2.c 包含该标头,则它可以使用变量j
。但是,对于全局变量,至少要使用描述性变量名称。而且你应该尽可能避免全局变量,从长远来看,它们是一种负担,IMO。
(您可以考虑在
file1.c
中创建一个返回该值的函数。这样做的优点是确保j
在file1.c< /code>,并且只能在其他地方读取,限制了理解谁“拥有”该变量的复杂性。)
You could put this in a header file:
and only declare the "real"
j
in file1.c. If file2.c includes that header, then it can use variablej
.But, use descriptive variable names a the very least for globals. And you should avoid globals as much as you can, they are a liability in the long term, IMO.
(You could consider something like making a function in
file1.c
that returns that value. This has the advantage of assuring thatj
is controlled infile1.c
, and only read in other places, limiting the complexity of understanding who "owns" that variable.)像这样
在 c 文件中,您定义变量 file1.c,您
为 file1.h 编写一个标头,
该标头将包含您在 file2.c 中包含的标头
,但我建议使用更具描述性的变量名称。
like this
in the c file you define the variable say file1.c you write
a header for your file1.h would contain
this header you include in file2.c
but i would suggest using a more descriptive variable name.
#include "file1.c"
几乎从来都不是问题的正确解决方案。您总是希望在头文件中声明内容,然后在单个源文件中定义它们。file1.h
file1.c
file2.c
说了这么多,我强烈建议您将值作为参数传递,而不是使用全局状态。全球国家是邪恶的。像躲避瘟疫一样避开它。
#include "file1.c"
is almost never the correct solution to a problem. Invariable you want to be declaring things in header files and then defining them in just a single source file.file1.h
file1.c
file2.c
Having said all of this, I would strongly council you to pass values as parameters rather than use global state. Global state is evil. Avoid it like the plague.