linux - 如何在 linux c 上添加 dir.h 库

标签 linux directory console c

我无法运行我的程序,因为我无法在 c 上添加 dir.h

我的头文件:

#ifndef _DIR
#define _DIR

typedef struct Path {
char         *name;         /* Name of directory */
int           refCount;     /* Number of paths with this directory */
int       hits;         /* the number of times a file in this
             * directory has been found */
Hash_Table    files;        /* Hash table of files in directory */
} Path;*/

void Dir_Init(void);
void Dir_InitDot(void);
void Dir_End(void);
Boolean Dir_HasWildcards(char *);
void Dir_Expand(char *, Lst, Lst);
char *Dir_FindFile(char *, Lst);
int Dir_MTime(GNode *);
void Dir_AddDir(Lst, char *);
char *Dir_MakeFlags(char *, Lst);
void Dir_ClearPath(Lst);
void Dir_Concat(Lst, Lst);
void Dir_PrintDirectories(void);
void Dir_PrintPath(Lst);
void Dir_Destroy(void *);
void * Dir_CopyDir(void *);

#endif /* _DIR */

这是我的代码

#include <stdio.h>
#include <dirent.h>
#include "dir.h"
#include <sys/types.h>
#include <stdlib.h>
int main(void)
{
printf("%d drives", _setdisk(2));
return 0;
}

最佳答案

头文件的包含顺序应该是头文件所需的定义已经定义好了。建议:

#include <sys/types.h>
#include <dirent.h>
#include "dir.h"

注意:dirent.h 需要在 sys/types.h 中找到的定义

dir.h 需要在dirent.h

中找到的定义

所以他们应该按照适当的顺序

注意:以下划线开头后跟大写字母的名称是为环境“保留”的,为避免名称冲突,您的代码不应使用该布局。建议:

#ifndef DIR_H
#define DIR_H

请注意名称全部大写,因为这是编写 #define 名称的预期方式。另请注意,词根 DIRH 由下划线分隔,因为这是在 #define 名称中分隔词根的预期方式.

请注意,最好将struct定义与typedef分开,并且在typedef结束后有一个无关的*/ .最好(尽管现代编译器可以处理)编写易于人类阅读的内容,而不会造成不必要的混淆。对名称使用“lowerCamelCase”也是一个很好的编程习惯。建议:

struct pathStruct 
{
    char         *name;       /* Name of directory */
    int           refCount;   /* Number of paths with this directory */
    int           hits;       /* The number of times a file in this
                               * directory has been found */
    Hash_Table    files;      /* Hash table of files in directory */
};

typedef struct pathStruct Path;

关于linux - 如何在 linux c 上添加 dir.h 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43410994/

相关文章:

linux - 将 bash 脚本的输出写入带有日期的文件

linux - pdo 驱动程序在 linux 中没有显示任何值

ruby-on-rails - Ruby:遍历目录和文件

java - 递归搜索Java中的目录

java - Eclipse 在用户输入时崩溃

c# - Console.Clear() 闪烁

PHP ZipArchive : replace formatted text in . docx?

c - linux下如何获取usb的urb信息

linux - 时钟()的实现

c - 如何修复 "recursively print subdirectories in c"