如何从共享库中包含头文件?

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

当前状态是什么?

我的文件夹结构:

--home
   |---an_awesome_lib
   |    |---Core
   |         |---src
   |         |    |---header_of_lib.h
   |         |
   |         |---build
   |              |---libawesome.so
   |
   |---framework
        |---project
              |---my_program_header.h

在my_program_header.h中,我有以下一行:

#include <an_awesome_lib/Core/src/header_of_lib.h>

我告诉g ++:

-I</home/an_awesome_lib/Core/src/header_of_ib.h> -lawesome -L/home/an_awesome_lib/build

问题描述

[当我写#include<home/an_awesome_lib/Core/src/header_of_lib.h>时,编译器不会抛出错误。但是我不想这样做。我要……像#include <an_awesome_lib/Core/src/header_of_lib.h>,这是一个简单的问题,已经经常被问到。但是,我看不到给g ++的路径与代码中#include中的路径之间的关系。

我在哪里找到了一些信息:

我读过那里,但没有完整的解释将所有内容放在一起。

What is the difference between #include <filename> and #include "filename"?

https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html

How do I include a path to libraries in g++

Using (relative) paths to shortcut in include statements in C++


我想要的

我希望您有一个一般的解释,可以将其放在上下文中。以下要点仅是一些建议,目的是使我想知道的更加清楚。

  • 我什么时候需要告诉整个路径?
  • 什么时候相对路径足够?
  • 头文件中的路径与给g ++的路径有什么关系?他们有恋爱关系吗?
  • 是否有多种解决方案,其中一种限制更多,一种开放性?
c++ include shared-libraries
1个回答
1
投票

-I指定一个包含目录,而不是文件。 GCC文档reads

-I dir

...

将目录dir添加到要在预处理期间搜索头文件的目录列表中。

and

使用预处理指令#include包括用户和系统头文件。它有两个变体:

#include <file>

此变体用于系统头文件。它在系统目录的标准列表中搜索名为file的文件。您可以使用-I选项在此列表之前添加目录。

...


如果您想写

#include <an_awesome_lib/Core/src/header_of_lib.h>

然后您应将/home指定为包含目录:

-I/home

但是,包括整个home目录不是一个好主意。我建议您更改很棒的库的目录结构。例如,您可以模仿Boost并在an_awesome_lib中创建an_awesome_lib目录:

an_awesome_lib
  |--- an_awesome_lib

然后使用-I/home/an_awesome_lib

或者您可以在include/an_awesome_lib中创建an_awesome_lib

an_awesome_lib
  |--- include
         |--- an_awesome_lib

然后使用-I/home/an_awesome_lib/include

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