I found the following code at lines 153-154 in the libelf.h
of the libelf library:
我在libelf库的libelf.h中的第153-154行找到了以下代码:
/* Descriptor for the ELF file. */
typedef struct Elf Elf;
I was looking for a struct definition of Elf
but did not find it.
我正在寻找Elf的结构定义,但没有找到它。
Later in the code, Elf
is used, e.g.
稍后在代码中使用Elf,例如
/* Return descriptor for ELF file to work according to CMD. */
extern Elf *elf_begin (int __fildes, Elf_Cmd __cmd, Elf *__ref);
In the thread Why should we typedef a struct so often in C?, user "unwind" says:
在线程中我们为什么要在C?中经常键入一个结构,用户“展开”说:
Also note that while your example (and mine) omitted naming the
struct
itself, actually naming it is also useful for when you want to provide an > opaque type. Then you'd have code like this in the header, for instance:另请注意,虽然您的示例(和我的)省略了命名结构本身,但实际命名它对于您想要提供> opaque类型时也很有用。然后你在标题中有这样的代码,例如:
typedef struct Point Point;
typedef struct Point Point;
Point * point_new(int x, int y);
Point * point_new(int x,int y);
and then provide the
struct
declaration in the implementation file.然后在实现文件中提供struct声明。
Yet, I couldnt find a definition of the struct Elf
in any c file either.
然而,我无法在任何c文件中找到struct Elf的定义。
What am I missing? What is the purpose of a typedef struct Name_xy Name_xy;
without struct definition? Or is this impossible and I just did not find the struct definition?
我错过了什么? typedef结构的目的是什么Name_xy Name_xy;没有结构定义?或者这是不可能的,我只是没有找到结构定义?
Edit:
First, thank you for the numerous great replies. As my question was two-fold (at least), there are two answers:
首先,感谢您的无数精彩回复。由于我的问题是双重的(至少),有两个答案:
- I did not find the definition because I did not have the
lib/private.h
file (thanks @molbdnilo to point out that the definition is in there). I installed the sources ofelfutils
and not oflibelf
. It seems that theprivate.h
is not included in theelfutils
source package. - The answers from @Acrasidae, @sfjac, and @Matt McNabb explain the conceptual background (opaque type, encapsulation, minimising dependencies ...).
我没有找到定义,因为我没有lib / private.h文件(感谢@molbdnilo指出定义在那里)。我安装了elfutils的来源而不是libelf。似乎private.h不包含在elfutils源包中。
@Acrasidae,@ sfjac和@Matt McNabb的答案解释了概念背景(opaque类型,封装,最小化依赖性......)。
5 个解决方案
#1
In C++, struct Elf
should be sufficient to declare the type, which is all that is necessary to return a pointer to that type. This is often done to hide implementation from users of the header file that will not use the Elf
functionality themselves but may just pass on the pointer. Coding in this fashion on eliminates unnecessary dependencies on header files that aren't actually used. This approach to minimizing header dependencies can have a big impact on compile times in large C++ projects.
在C ++中,struct Elf应该足以声明类型,这是返回指向该类型的指针所必需的。这通常是为了隐藏头文件用户的实现,这些用户不会自己使用Elf功能,但可能只是传递指针。以这种方式编码可以消除对实际未使用的头文件的不必要的依赖性。这种最小化头依赖关系的方法会对大型C ++项目中的编译时间产生很大影响。
As others have mentioned, typedef struct Elf Elf
is a C thing, allowing you to elide the struct
in future declarations.
正如其他人所提到的,typedef struct Elf Elfis是一个C事物,允许你在将来的声明中忽略结构。
#2
typedef struct Elf Elf;
is a short way of writing:
typedef struct Elf Elf;是一种简短的写作方式:
struct Elf;
typedef struct Elf Elf;
These lines do different things. The first one is usually called a forward declaration. It means that we can later have code such as:
这些行做了不同的事情。第一个通常称为前向声明。这意味着我们以后可以使用以下代码:
// function prototype
struct Elf *elf_begin( stuff.... );
which prototypes a function that returns a pointer to struct Elf
even though we do not actually know what is contained in the body of struct Elf
. This is sometimes called opaque type and another instance of it is FILE *
in the C standard library. There might be no definition of struct Elf
anywhere.
它构建了一个函数,它返回一个指向struct Elf的指针,即使我们实际上并不知道struct Elf体中包含的内容。这有时称为opaque类型,它的另一个实例是C标准库中的FILE *。任何地方都可能没有struct Elf的定义。
The second part, typedef struct Elf Elf;
is, as you say in your question, there to mainly avoid having to type struct
all the time.
第二部分,typedef struct Elf Elf;就像你在你的问题中所说的那样,主要是为了避免必须一直输入struct。
#3
Is it defined in decl.h?
它是在decl.h中定义的吗?
For example:
http://www.opensource.apple.com/source/dtrace/dtrace-96/head/libelf.h http://www.opensource.apple.com/source/dtrace/dtrace-118/libelf/decl.h
#4
The possible explanation of this could be your project has some pre-compiled library files which contains definition of struct Elf
.
可能的解释可能是您的项目有一些预编译的库文件,其中包含struct Elf的定义。
#5
What am I missing? What is the purpose of a typedef struct Name_xy Name_xy; without struct definition? Or is this impossible and I just did not find the struct definition?
我错过了什么? typedef结构的目的是什么Name_xy Name_xy;没有结构定义?或者这是不可能的,我只是没有找到结构定义?
In libelf.h, you have a declaration of an opaque type : Elf.
在libelf.h中,您有一个opaque类型的声明:Elf。
However, you do not find the definition of the Elf structure, and you are wondering why.
但是,您没有找到Elf结构的定义,您想知道为什么。
First of all, there is a definition somewhere, which looks like struct Elf{...};, however, you can not access to the definition because developers do not want you to access it. The only way you can use the contents of the Elf structure is because there is this declaration, typedef struct Elf Elf;, in libelf.h. By the way, struct Elf; is exactly the same, but with the typedef we know that there is more chance that the type is opaque.
首先,有一个定义,看起来像struct Elf {...};但是,你无法访问定义,因为开发人员不希望你访问它。你可以使用Elf结构内容的唯一方法是因为libelf.h中有这个声明,typedef struct Elf Elf;。顺便说一下,struct Elf;是完全相同的,但是使用typedef,我们知道该类型更不可能是不透明的。
A more illustrated example :
一个更加图解的例子:
The header file :
头文件:
/* libelf.h */
typedef struct Elf Elf;
extern Elf *elf_begin (int __fildes, Elf_Cmd __cmd, Elf *__ref);
etc...
The implementation file :
实施文件:
/* libelf.c */
struct Elf {
char * something;
FILE * whatever;
};
Elf *elf_begin (int __fildes, Elf_Cmd __cmd, Elf *__ref){
/*doing something;*/
/* ... */
}
etc...
This is exactly the principle of Encapsulation. The contents of this type are known and accessible only to the implementation of the API functions, clients (you) cannot directly access its contents. For developers, they have to compile a shared library, so you may find something like libelf.so somewhere (I do not know what the libelf do so I cannot help you on this). About the purpose of encapsulation, I advice you to read this.
这正是Encapsulation的原理。此类型的内容是已知的,只有API函数的实现才可访问,客户端(您)无法直接访问其内容。对于开发人员来说,他们必须编译一个共享库,所以你可能会在某个地方找到像libelf.so这样的东西(我不知道自己做了什么,所以我无法帮助你)。关于封装的目的,我建议你阅读本文。
I have nothing else in mind, but if you have more questions, do not hesitate.
我没有别的想法,但如果你有更多问题,请不要犹豫。
#1
In C++, struct Elf
should be sufficient to declare the type, which is all that is necessary to return a pointer to that type. This is often done to hide implementation from users of the header file that will not use the Elf
functionality themselves but may just pass on the pointer. Coding in this fashion on eliminates unnecessary dependencies on header files that aren't actually used. This approach to minimizing header dependencies can have a big impact on compile times in large C++ projects.
在C ++中,struct Elf应该足以声明类型,这是返回指向该类型的指针所必需的。这通常是为了隐藏头文件用户的实现,这些用户不会自己使用Elf功能,但可能只是传递指针。以这种方式编码可以消除对实际未使用的头文件的不必要的依赖性。这种最小化头依赖关系的方法会对大型C ++项目中的编译时间产生很大影响。
As others have mentioned, typedef struct Elf Elf
is a C thing, allowing you to elide the struct
in future declarations.
正如其他人所提到的,typedef struct Elf Elfis是一个C事物,允许你在将来的声明中忽略结构。
#2
typedef struct Elf Elf;
is a short way of writing:
typedef struct Elf Elf;是一种简短的写作方式:
struct Elf;
typedef struct Elf Elf;
These lines do different things. The first one is usually called a forward declaration. It means that we can later have code such as:
这些行做了不同的事情。第一个通常称为前向声明。这意味着我们以后可以使用以下代码:
// function prototype
struct Elf *elf_begin( stuff.... );
which prototypes a function that returns a pointer to struct Elf
even though we do not actually know what is contained in the body of struct Elf
. This is sometimes called opaque type and another instance of it is FILE *
in the C standard library. There might be no definition of struct Elf
anywhere.
它构建了一个函数,它返回一个指向struct Elf的指针,即使我们实际上并不知道struct Elf体中包含的内容。这有时称为opaque类型,它的另一个实例是C标准库中的FILE *。任何地方都可能没有struct Elf的定义。
The second part, typedef struct Elf Elf;
is, as you say in your question, there to mainly avoid having to type struct
all the time.
第二部分,typedef struct Elf Elf;就像你在你的问题中所说的那样,主要是为了避免必须一直输入struct。
#3
Is it defined in decl.h?
它是在decl.h中定义的吗?
For example:
http://www.opensource.apple.com/source/dtrace/dtrace-96/head/libelf.h http://www.opensource.apple.com/source/dtrace/dtrace-118/libelf/decl.h
#4
The possible explanation of this could be your project has some pre-compiled library files which contains definition of struct Elf
.
可能的解释可能是您的项目有一些预编译的库文件,其中包含struct Elf的定义。
#5
What am I missing? What is the purpose of a typedef struct Name_xy Name_xy; without struct definition? Or is this impossible and I just did not find the struct definition?
我错过了什么? typedef结构的目的是什么Name_xy Name_xy;没有结构定义?或者这是不可能的,我只是没有找到结构定义?
In libelf.h, you have a declaration of an opaque type : Elf.
在libelf.h中,您有一个opaque类型的声明:Elf。
However, you do not find the definition of the Elf structure, and you are wondering why.
但是,您没有找到Elf结构的定义,您想知道为什么。
First of all, there is a definition somewhere, which looks like struct Elf{...};, however, you can not access to the definition because developers do not want you to access it. The only way you can use the contents of the Elf structure is because there is this declaration, typedef struct Elf Elf;, in libelf.h. By the way, struct Elf; is exactly the same, but with the typedef we know that there is more chance that the type is opaque.
首先,有一个定义,看起来像struct Elf {...};但是,你无法访问定义,因为开发人员不希望你访问它。你可以使用Elf结构内容的唯一方法是因为libelf.h中有这个声明,typedef struct Elf Elf;。顺便说一下,struct Elf;是完全相同的,但是使用typedef,我们知道该类型更不可能是不透明的。
A more illustrated example :
一个更加图解的例子:
The header file :
头文件:
/* libelf.h */
typedef struct Elf Elf;
extern Elf *elf_begin (int __fildes, Elf_Cmd __cmd, Elf *__ref);
etc...
The implementation file :
实施文件:
/* libelf.c */
struct Elf {
char * something;
FILE * whatever;
};
Elf *elf_begin (int __fildes, Elf_Cmd __cmd, Elf *__ref){
/*doing something;*/
/* ... */
}
etc...
This is exactly the principle of Encapsulation. The contents of this type are known and accessible only to the implementation of the API functions, clients (you) cannot directly access its contents. For developers, they have to compile a shared library, so you may find something like libelf.so somewhere (I do not know what the libelf do so I cannot help you on this). About the purpose of encapsulation, I advice you to read this.
这正是Encapsulation的原理。此类型的内容是已知的,只有API函数的实现才可访问,客户端(您)无法直接访问其内容。对于开发人员来说,他们必须编译一个共享库,所以你可能会在某个地方找到像libelf.so这样的东西(我不知道自己做了什么,所以我无法帮助你)。关于封装的目的,我建议你阅读本文。
I have nothing else in mind, but if you have more questions, do not hesitate.
我没有别的想法,但如果你有更多问题,请不要犹豫。