【问题标题】:How to add dir.h library on linux c如何在 linux c 上添加 dir.h 库
【发布时间】:2017-04-14 02:55:29
【问题描述】:

我无法运行我的程序,因为我无法在 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;
}

【问题讨论】:

  • 你能提供你的编译器的输出吗?特别是它引发的与 dir.h 相关的错误。
  • 请同时包含编译器命令本身(这样我们可以看到您提供给编译器的包含指令)

标签: linux directory console c


【解决方案1】:

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

#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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2014-07-24
    • 2021-11-24
    • 2010-12-22
    • 2023-03-19
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多