c - 函数名称和原型(prototype)中的斜线和点?

标签 c go notation

我是 C 新手,正在查看 Go的源代码树我发现了这个:

https://code.google.com/p/go/source/browse/src/pkg/runtime/race.c

void runtime∕race·Read(int32 goid, void *addr, void *pc);
void runtime∕race·Write(int32 goid, void *addr, void *pc);

void
runtime·raceinit(void)
{
    // ...
}

斜线和点 (·) 是什么意思?这是有效的 C 吗?

最佳答案

IMPORTANT UPDATE:

The ultimate answer is certainly the one you got from Russ Cox, one of Go authors, on the golang-nuts mailing list. That said, I'm leaving some of my earlier notes below, they might help to understand some things.

Also, from reading this answer linked above, I believe the "pseudo-slash" may now be translated to regular / slash too (like the middot is translated to dot) in newer versions of Go C compiler than the one I've tested below - but I don't have time to verify.


文件由Go Language Suite's internal C compiler 编译。 ,源自 Plan 9 C 编译器 (1) (2) has some differences (主要是扩展,AFAIK)到 C 标准。

其中一个扩展是,它允许在标识符中使用 UTF-8 字符。

现在,在 Go 语言套件的 C 编译器中,中间点字符 (·) 以特殊方式处理,因为它在目标文件中被翻译成常规点 (.),由 Go 语言套件的内部链接器解释作为命名空间分隔符。

Example

For the following file example.c (note: it must be saved as UTF-8 without BOM):

void ·Bar1() {}
void foo·bar2() {}
void foo∕baz·bar3() {}

the internal C compiler produces the following symbols:

$ go tool 8c example.c
$ go tool nm example.8
 T "".Bar1
 T foo.bar2
 T foo∕baz.bar3

Now, please note I've given the ·Bar1() a capital B. This is because that way, I can make it visible to regular Go code - because it is translated to the exact same symbol as would result from compiling the following Go code:

package example
func Bar1() {}  // nm will show:  T "".Bar1

现在,关于您在问题中提到的函数,故事更深入了兔子洞。我不太确定我是否在这里,但我会尝试根据我所知道的进行解释。因此,该点以下的每个句子都应该像在结尾处写“AFAIK”一样阅读。

所以,为了更好地理解这个难题,下一个缺失的部分是更多地了解奇怪的 "" 命名空间,以及 Go 套件的链接器如何处理它。 "" 命名空间是我们可能想要称为“空”(因为 "" 对于程序员的意思是“空字符串”)命名空间,或者更好的是“占位符”命名空间。当链接器看到这样的导入时:

import examp "path/to/package/example"
//...
func main() {
    examp.Bar1()
}

然后它需要 $GOPATH/pkg/.../example.a 库文件,并在导入阶段动态替换每个 ""path/to/package/example。所以现在,在链接的程序中,我们会看到这样的符号:

 T path/to/package/example.Bar1

关于c - 函数名称和原型(prototype)中的斜线和点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13475908/

相关文章:

c - 管道和 select() 有问题

Golang - 我可以有一个带有 slice 成员的结构吗?

string - 用数学符号表示我们在编程中使用的字符串

c - 替换整数的最后一位

c - 存储大小未知

go - 试图从 Go 中理解这个函数,为什么要创建一个始终在恒定时间内运行的函数,它是如何工作的?

java - java中如何用科学计数法表示数字?

c - 通过基数和指数显式定义浮点值

将 char 指针数组转换为小写 C

xml - 使用 Go 解析 RDF 三倍。一些项目错误地传递了正则表达式