C 控制台应用程序 - 文件/头组织和编译

问题描述 投票:0回答:1

我很难将多个

.c
.h
文件组织到我的控制台应用程序项目中 - 而且,我有点不确定应该如何编写编译命令。我将列出我的文件和其中的(相关)代码:

main.c:

#include <stdio.h>
#include <stdlib.h>
#include <structs.h>
#include <search.h>

int main () {
    ...
    search();
    ...
    return 0;
}

结构.c:

#include <structs.h>

typedef struct {
    ...
} Brand;

typedef struct {
    ...
} Car;

结构.h:

#ifndef STRUCTS_H
#define STRUCTS_H

typedef struct {
    ...
} Brand;

typedef struct {
    ...
} Car;

#endif

搜索.c:

#include <search.h>

Car *search () {
    ...
    Car cars[255];
    ...
    return cars;
}

搜索.h:

#ifndef SEARCH_H
#define SEARCH_H

#include <stdio.h>
#include <stdlib.h>
#include <structs.h>

Car *search ();

#endif

我正在编写的编译命令是

gcc -o main structs.h search.h main.c
。终端显示以下错误:

search.h:6:10: fatal error: structs.h: No such file or directory
main.c:6:10: fatal error: structs.h: No such file or directory

我到处研究,一遍又一遍地观看 C 教程,在 StackOverflow 上查看了一些类似的问题 - 我尝试的一切只是将此问题切换到其他问题(其他文件“丢失”)。预先感谢,并对马虎的英语表示歉意!

c struct compilation console console-application
1个回答
0
投票

包含本地包含文件的常用方法是将它们放在引号中而不是尖括号中。

所以你的主要内容应该从:

开始
#include <stdio.h>
#include <stdlib.h>

#include "structs.h" 
#include "search.h"

编译器将检查当前文件夹中的包含文件,并且:

cc -o main main.c search.c 

会起作用的。另一种向编译器指示包含文件所在位置的方法是

-I
选项。

cc -o main -I . main.c search.c

其他东西:

“structs.h”不是一个好名字,它没有告诉任何查看代码的人头文件的用途。 “carinfo.h”怎么样?

© www.soinside.com 2019 - 2024. All rights reserved.