【发布时间】:2016-10-25 17:55:40
【问题描述】:
假设我在以下文件夹中有一个 C 项目:
C:\microcontroller\stm32\myProject
我的项目中有两个重要的文件夹: - source => 这是我所有的.c 和.h 文件 - build => gcc 会将所有目标文件写入这里
注意:如您所见,反斜杠表示这是在 Windows 电脑上发生的。
下图给出了一个概览:
我不会在这里展示我完整的 makefile,因为那样会让我们走得太远。所有.c => .o 文件的makefile 中的规则都是相似的。让我们只关注一个特定文件的编译:fileA2.c:
--------------------- COMPILATION OF FILE fileA2.c -------------------
Building ./build/folderA/fileA2.o
arm-none-eabi-gcc C:\\microcontroller\\stm32\\myProject\\source\\folderA\\fileA2.c
-o C:\\microcontroller\\stm32\\myProject\\build\\folderA\\fileA2.o
-c
-MMD
-mcpu=cortex-m7
-...
-IC:/microcontroller/stm32/myProject/source/folderA
-IC:/microcontroller/stm32/myProject/source/folderB
请注意,gcc 调用以两个包含标志结束:一个用于文件夹 A,一个用于文件夹 B。如果fileA2.c 有import 语句,这使得gcc 可以使用这些文件夹(fileA1.h、fileA2.h 或fileB1.h)中的任何头文件。
现在让我们考虑fileA2.c 中的源代码。我们假设这个文件需要包含fileA2.h 和fileB1.h。
/*******************************/
/* SOURCE CODE fileA2.c */
/*******************************/
// Some include statements
#include "fileA2.h"
#include "fileB1.h"
// Code
...
这些包含语句完美运行。 gcc 编译器检索给定文件夹中的文件fileA2.h 和fileB1.h。但我注意到以下内容不起作用:
/*******************************/
/* SOURCE CODE fileA2.c */
/*******************************/
// Some include statements
#include "fileA2.h"
#include "folderB/fileB1.h"
// Code
...
最后一个包含语句是文件的“部分路径”。编译时出现错误:fatal error: folderB/fileB1.h: No such file or directory
我怎样才能让 gcc 来处理这个问题?
PS:使用“部分路径”不是我自己的习惯。但是它们在我芯片的芯片供应商的库中出现了很多,所以我不得不忍受它。
【问题讨论】:
-
因为
fileA2.c在folderA中,而folderB在folderA中,这就是找不到包含文件的原因。试试#include "../folderB/fileB1.h" and it will find it normally. This means go to parent directory, then tofolderB, and finally include the filefileB1.h`。