如何在一个文件中定义变量并在另一个文件中使用它

发布于 2024-12-10 05:16:10 字数 1765 浏览 0 评论 0原文

这个问题可能非常基本,但我无论如何都没有得到线索......

我有两个文件...... 我正在提到我想做的事情。

文件 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情何以堪。 2024-12-17 05:16:10

您可以将其放入头文件中:

extern int j;

并且仅在 file1.c 中声明“真实”j。如果 file2.c 包含该标头,则它可以使用变量 j

但是,对于全局变量,至少要使用描述性变量名称。而且你应该尽可能避免全局变量,从长远来看,它们是一种负担,IMO。

(您可以考虑在 file1.c 中创建一个返回该值的函数。这样做的优点是确保 jfile1.c< /code>,并且只能在其他地方读取,限制了理解谁“拥有”该变量的复杂性。)

You could put this in a header file:

extern int j;

and only declare the "real" j in file1.c. If file2.c includes that header, then it can use variable j.

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 that j is controlled in file1.c, and only read in other places, limiting the complexity of understanding who "owns" that variable.)

少钕鈤記 2024-12-17 05:16:10

像这样

在 c 文件中,您定义变量 file1.c,您

int j;

...

j = data->alloc_len;

为 file1.h 编写一个标头,

extern int j;

该标头将包含您在 file2.c 中包含的标头

#include "file1.h"

,但我建议使用更具描述性的变量名称。

like this

in the c file you define the variable say file1.c you write

int j;

...

j = data->alloc_len;

a header for your file1.h would contain

extern int j;

this header you include in file2.c

#include "file1.h"

but i would suggest using a more descriptive variable name.

情深如许 2024-12-17 05:16:10

#include "file1.c" 几乎从来都不是问题的正确解决方案。您总是希望在头文件中声明内容,然后在单个源文件中定义它们。

file1.h

extern int j;

file1.c

#include "file1.h"
j = data->alloc_len

file2.c

#include "file1.h"
if (j>0)
    ...

说了这么多,我强烈建议您将值作为参数传递,而不是使用全局状态。全球国家是邪恶的。像躲避瘟疫一样避开它。

#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

extern int j;

file1.c

#include "file1.h"
j = data->alloc_len

file2.c

#include "file1.h"
if (j>0)
    ...

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文