在源文件之间共享静态变量或函数。 [英] Sharing static variables or function between source files.

查看:79
本文介绍了在源文件之间共享静态变量或函数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何在一个

源文件中声明一个静态变量或函数,该文件将在其他源文件中使用?

Hi,
How can we declare a static variable or function declared in one
source file, which is to be used in other source file?

推荐答案

shantanu< sh ********** @ gmail.comwrote:
shantanu <sh**********@gmail.comwrote:

我们如何声明在一个

源文件中声明的静态变量或函数,该文件将在其他源文件中使用?
How can we declare a static variable or function declared in one
source file, which is to be used in other source file?



1)不要声明它们是静态的。


2)向每个源文件添加extern声明除外你在哪里定义这个函数:


Ac


void foo(){

/ * ... * /

}


Bc


extern void foo;


void bar(){

/ * ... * /

foo();

}


如何正确调用链接器以确保其工作原理为另一个新闻组的主题




-

C. Benson Manica |我*应该*知道我在说什么 - 如果我

cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。

1) Don''t declare them to be static.

2) Add extern declarations to every source file except the one where you
define the function:

A.c

void foo() {
/* ... */
}

B.c

extern void foo;

void bar() {
/* ... */
foo();
}

How to correctly invoke your linker to make sure this works is a topic
for a different newsgroup.

--
C. Benson Manica | I *should* know what I''m talking about - if I
cbmanica(at)gmail.com | don''t, I need to know. Flames welcome.


文章< 11 ********************* @ 45g2000cws.googlegroups.c om>,

shantanu< sh ********** @ gmail.comwrote:
In article <11*********************@45g2000cws.googlegroups.c om>,
shantanu <sh**********@gmail.comwrote:

我们如何申报在一个源文件中声明的静态变量或函数,它将在其他源文件中使用?
How can we declare a static variable or function declared in one
source file, which is to be used in other source file?



你不是。如果你需要在另一个源文件中使用它,请不要将
声明为静态,或者找到一种获取指针的方法。


例如,你可以添加一个例程


SomeType MyStaticVariable;


SomeType * get_pointer_to_my_static_variable(void){

return& MyStaticVariable;

}


然后使用空格,get_pointer_to_my_static_variable()和

使用返回的指针那个。

-

一切都是虚荣心。 - 传道书

You don''t. If you need to use it in another source file, do not
declare it as static, or else find a way to get a pointer to it.

For example, you could add a routine which was

SomeType MyStaticVariable;

SomeType *get_pointer_to_my_static_variable(void) {
return &MyStaticVariable;
}

Then to use the space, get_pointer_to_my_static_variable() and
use the pointer returned by that.
--
All is vanity. -- Ecclesiastes


2006年11月27日19:11:25 -0800,shantanu < sh ********** @ gmail.com>

在comp.lang.c中写道:
On 27 Nov 2006 19:11:25 -0800, "shantanu" <sh**********@gmail.com>
wrote in comp.lang.c:



我们如何在一个

源文件中声明一个静态变量或函数,该文件将在其他源文件中使用?
Hi,
How can we declare a static variable or function declared in one
source file, which is to be used in other source file?



如果您想与其他翻译单元轻松共享,请将其定义为

,不带静态关键字。

是否回答你的问题取决于你的意思是静态,

因为关键字在C中有几个不同的含义。所有对象

在文件范围定义有静态存储时间。添加静态

关键字只会更改它们从外部到内部的链接。


如果您指的是使用static关键字在文件范围定义的对象,或者

使用static关键字定义的函数,唯一的访问方式或

从其他翻译单元调用它们是通过在
中初始化或由包含定义的翻译单元。


-

Jack Klein

主页: http://JK-Technology.Com



comp的常见问题解答。 lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/ c ++ - faq-lite /

alt.comp.lang.learn.c-c ++
http://www.c ontrib.andrew.cmu.edu/~a...FAQ-acllc.html

If you want to easily share it with other translation units, define it
without the static keyword.

Whether that answers your question depends on what you mean by static,
since the keyword has several different meanings in C. All objects
defined at file scope have static storage duration. Adding the static
keyword only changes their linkage from external to internal.

If you mean objects defined at file scope with the static keyword, or
functions defined with the static keyword, the only way to access or
call them from other translation units is via a pointer initialized in
or provided by the translation unit that contains the definition.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于在源文件之间共享静态变量或函数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆