全球和出口 [英] Globals and Exports

查看:81
本文介绍了全球和出口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到在C中,任何模块中的函数都会自动导出。所以

,不可能在两个模块中使用相同的函数名称(即。

源文件)。


既然我知道,我就可以解决它;但有没有办法避免

问题(没有大量重命名)?


更严重的是,在文件范围内声明的变量似乎也是

自动导出。但在这种情况下,编译器/链接器并不警告我在两个或多个模块中使用相同的名称。 (这是什么

大惊小怪的''全局变量''?


有什么方法可以解决这个问题吗? (就像一些关键字会在模块中呈现一个

变量。)


- Bartc

解决方案

2008年5月5日9:53,Bartc写道:


我注意到在C中,任何模块中的函数都会自动导出。所以

,不可能在两个模块中使用相同的函数名称(即。

源文件)。


既然我知道,我就可以解决它;但有没有办法避免

问题(缺少大量重命名)?



查看编译器的选项。例如,gcc有一个非常好的b $ b b b复杂的可见性模型:在最简单的情况下,你可以使用

__attribute((visibility(hidden)))作为限定符,或编译

-fvisibility = hidden。


更严重的是,在文件范围内声明的变量似乎也是

自动导出。但在这种情况下,编译器/链接器并不警告我在两个或多个模块中使用相同的名称。 (这是什么

大惊小怪的''全局变量''?


有什么方法可以解决这个问题吗? (就像某个关键字会在模块本地呈现

变量。)



static。




" Antoninus Twink" < no **** @ nospam.invalidwrote in message

news:sl ******************* @ nospam.invalid ...


2008年5月5日9:53,Bartc写道:


>更严重的是,在文件范围内声明的变量似乎也会自动导出。但在这种情况下,编译器/链接器并没有警告我在两个或多个模块中使用了相同的名称。 (这是关于全局变量的大惊小怪吗?)

有什么方法可以解决这个问题吗? (就像某个关键字会在模块中呈现局部变量。)



static。



那么简单..


-

Bartc


Bartc写道,On 05/05/08 10:53:


我注意到在C中,任何模块中的函数都会自动导出。所以

,不可能在两个模块中使用相同的函数名称(即

源文件)。



你的C课本应该告诉你这个。如果没有,你需要重读它

和/或更换它。


现在我知道了,我可以解决它;但有没有办法避免

问题(没有大量重命名)?


更严重的是,在文件范围内声明的变量似乎也是

自动导出。



是。


但在这种情况下,编译器/链接器不会发出警告/>
我在两个或多个模块中使用相同的名称。



不需要。但有些可以制作,所以你应该阅读

你的实现文档。


(这是什么

大惊小怪的是''全局变量''?)



高耦合使得很难理解程序。您必须阅读

每个函数(或者您在整个代码库中搜索工具)

找出变量何时以及如何更改以及它们的使用位置,

然后你发现你有一个问题,因为你不能在不破坏更多的情况下更改一块

代码。


越大项目问题越大。


有什么方法可以解决这个问题吗? (就像某个关键字会在模块中呈现一个

变量。)



查看教科书中静态的多种用法。

-

Flash Gordon


I noticed that in C, functions in any module are automatically exported. So
that it''s not possible to use the same function name in two modules (ie.
source files).

Now that I know that, I get work around it; but is there a way to avoid the
problem (short of lots of renaming)?

More seriously, variables declared at file scope also seem to be
automatically exported. But in this case, the compiler/linker doesn''t warn
me that the same name is being used in two or more modules. (Is this what
the fuss is about with ''global variables''?)

Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)

-- Bartc

解决方案

On 5 May 2008 at 9:53, Bartc wrote:

I noticed that in C, functions in any module are automatically exported. So
that it''s not possible to use the same function name in two modules (ie.
source files).

Now that I know that, I get work around it; but is there a way to avoid the
problem (short of lots of renaming)?

Check out your compiler''s options. For example, gcc has quite a
sophisticated visibility model: in the simplest case, you can use
__attribute ((visibility("hidden"))) as a qualifier, or compile with
-fvisibility=hidden.

More seriously, variables declared at file scope also seem to be
automatically exported. But in this case, the compiler/linker doesn''t warn
me that the same name is being used in two or more modules. (Is this what
the fuss is about with ''global variables''?)

Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)

static.



"Antoninus Twink" <no****@nospam.invalidwrote in message
news:sl*******************@nospam.invalid...

On 5 May 2008 at 9:53, Bartc wrote:

>More seriously, variables declared at file scope also seem to be
automatically exported. But in this case, the compiler/linker doesn''t
warn
me that the same name is being used in two or more modules. (Is this what
the fuss is about with ''global variables''?)

Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)


static.


That simple..

--
Bartc


Bartc wrote, On 05/05/08 10:53:

I noticed that in C, functions in any module are automatically exported. So
that it''s not possible to use the same function name in two modules (ie.
source files).

Your C text book should have told you this. If not you need to reread it
and/or replace it.

Now that I know that, I get work around it; but is there a way to avoid the
problem (short of lots of renaming)?

More seriously, variables declared at file scope also seem to be
automatically exported.

Yes.

But in this case, the compiler/linker doesn''t warn
me that the same name is being used in two or more modules.

It is not required to. Some can be made to however, so you should read
the documentation for your implementation.

(Is this what
the fuss is about with ''global variables''?)

High coupling making it hard to understand programs. You have to read
every function (or you search facilities on the entire code base) to
find out when and how the variables are changed and where they are used,
then you find you have a problem because you cannot change one piece of
code without breaking lots more.

The larger the project the larger the problem.

Is there any way I can fix this? (Like some keyword that will render a
variable local to a module.)

Look up the many uses of static in your text book.
--
Flash Gordon


这篇关于全球和出口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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