【发布时间】:2020-05-09 10:03:06
【问题描述】:
我必须创建一个项目,该项目要求我创建一个程序,该程序将通过递归下降到目录并启动进程来编译 C 项目,这些进程通过调用 GCC 编译代码文件并查看目录并为每个目录启动一个新进程当前目录中的“.c”文件,该进程将在 .c 文件上调用 gcc 以生成 .o 文件。
到目前为止,我已经编写了这段代码来首先列出目录 我无法检查哪些文件是 .c 以及如何将它们转换为 .o
can someone help me with some relevant information/links that I can refer to?
void listdir(const char *name, int indent)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
printf("%*s[%s]\n", indent, "", entry->d_name);
listdir(path, indent + 2);
} else {
printf("%*s- %s\n", indent, "", entry->d_name);
}
}
closedir(dir);
}
int main(void) {
listdir(".", 0);
return 0;
}
【问题讨论】:
-
你要写
make file吗? -
如果你知道
strcmp(),你应该能够找到一个标准函数来检查以“.c”结尾的文件名,不是吗? -
这是家庭作业还是什么?因为如果不是这样,那么真的没有理由重新发明轮子,因为
make已经可以做你想要的(以及更多) -
这是一个家庭作业,虽然我找不到关于这个主题的任何相关信息。