下面是(示例)代码结构
/src
/poreA
- a1.h
- a1.cpp
/poreB
- b1.h
- b1.cpp
/poreC
- c1.h
- c1.cpp
- c2.h
/maincpp
-communicate.h
-communicate.cpp
-network.h
-network.cpp
-mainfile.cpp
-mainfile.h
每个文件夹(poreA,poreB,poreC)都有自己的make文件
c1.h
#include "a1.h"
// struct definitions are defined here
communicate.h
#include "../poreC/include/c1.h"
#ifndef POREC_H_
#define POREC_H_
network.h
#include "communicate.h"
mainfile.h
#include "network.h"
mainfile.cpp
#include "mainfile.h"
#include "network.h"
当我尝试编译时,
$ gcc -c mainfile.cpp
我得到错误
../../poreC/include/c1.h: fatal error: a1.h, no such file or directory
#include "a1.h"
我试图从类似的帖子中理解这个概念,但无法解决这个问题
如何从外部调用函数。C文件在C?
包含来自另一个目录的头文件
当您在多个目录中包含文件时,您可以告诉gcc使用-I
标志查看那里:
$ # if in ./src..
$ g++ -IporeA -IporeB -IporeC -Imaincpp maincpp/mainfile.cpp
$ # if in ./src/maincpp...
$ g++ -I../poreA -I../poreB -I../poreC mainfile.cpp
通常,小项目可能有一个单一的目录,可能被称为inc
或include
,所有的头文件。这样,gcc就只需要一个-Iinc
标志,而不是每个目录都需要一个。