【问题标题】:How to ask Linux g++ to correctly compile header files contained within another header file?如何让 Linux g++ 正确编译另一个头文件中包含的头文件?
【发布时间】:2015-10-07 14:53:27
【问题描述】:

我有一个关于如何修复以下 Linux Makefile 代码的问题:

g++  -g -Wall -I"./" -I"../CameraControlLib" -I"./Cameras" -I"../../Utilities/DataServer"  -o Server.o CameraControlLib/Server.cpp

在这种特殊情况下,DataServerLib.h 包括CameraControlDefs.h,如下所示。 Ubuntu Linux g++ 编译器正确使用 -I 预处理器指令解析包含文件 DataServerLib.h,如下所示。 但是,g++ 编译器告诉我没有像CameraControlDefs.h 这样的文件或目录,即使我将-I 子目录路径指向../../Utilities/DataServer

In file included from CameraControlLib/DataServer.h:4:0,
                 from CameraControlLib/DataServer.cpp:8:
CameraControlLib/DataServerLib.h:9:31: fatal error: CameraControlDefs.h: No such file or directory
 #include "CameraControlDefs.h"
                               ^
compilation terminated.

我知道如果我使用#include "SomeDir/CameraControlDefs.h",那应该会消除编译器错误消息。如果有使用 Linux g++ 选项的全部范围的替代方法,请告诉我。

【问题讨论】:

  • 您是否确认按照您在上一段中的建议更改源文件确实可以消除错误?测试将确保您正确理解问题。
  • CameraControlDefs.h 到底住在哪里?
  • 您是否阅读过GCC 的文档,尤其是关于Invoking GCC 的章节?还有cpp preprocessor....的文档?您真的应该阅读它们(这不会浪费时间)。
  • 然后,您应该使用-H 来了解在预处理阶段发生了什么;注意 -I 想要 directories
  • 避免在#include 指令中使用绝对文件路径:#include "/home/frank/includedir/foo.h"总是是一个错误

标签: c++ linux gcc makefile


【解决方案1】:

下午好, 这是我刚刚使用 g++ 编译器在 Makefile 中测试的解决方案

INCDIRS := $(addprefix -I,$(shell find ../Utilities/DataServer -type d -print))

【讨论】:

    猜你喜欢
    • 2016-04-13
    • 1970-01-01
    • 2011-07-17
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多