1

What is the current state?

My folder structure:

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

In my_program_header.h I have the following line:

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

I tell g++:

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

Problem Description

When I write #include<home/an_awesome_lib/Core/src/header_of_lib.h> the compiler does not throw an error. But I do not want to do it. I want sth. like #include <an_awesome_lib/Core/src/header_of_lib.h> This is a simple problem and it has been asked very often. However I do not see the relationship between the paths given to g++ and the path in #include in the code.

Where I found some information:

I read there but there was no complete explanation which puts this all together.

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++


What I want

I hope you have a general explanation which puts this in a context. The following bullet points are just some suggestions to make it more clear what I would like to know.

  • When do I need to tell the whole path?
  • When is a relative path sufficient?
  • How does the path in the header file relate to the path given to g++? Are they in a relationship?
  • Are there multiple solutions, one which restricts it a bit more, one which is more open?
Evg
  • 25,259
  • 5
  • 41
  • 83
egjada
  • 119
  • 2
  • 12
  • What do you mean by "When I tell #include the complete path it works"? Do you mean your compiler does not throw any error? What path are you using in your g++ command? – RoQuOTriX Dec 06 '19 at 10:31
  • 2
    You can do `-I/home` and then write `#include `. But it would be better to create a directory `include/an_awesome_lib` inside `an_awesome_lib`, and then use `-I/home/an_awesome_lib/include`. Take a look at Boost directory structure, for example. – Evg Dec 06 '19 at 10:34

1 Answers1

4

-I specifies an include directory, not a file. GCC documentation reads:

-I dir

...

Add the directory dir to the list of directories to be searched for header files during preprocessing.

and:

Both user and system header files are included using the preprocessing directive #include. It has two variants:

#include <file>

This variant is used for system header files. It searches for a file named file in a standard list of system directories. You can prepend directories to this list with the -I option.

...


If you want to be able to write

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

then you should specify /home as an include directory:

-I/home

However, including the whole home directory is not a good idea. I suggest you change the directory structure of your awesome library. For example, you could mimic Boost and create an_awesome_lib directory inside an_awesome_lib:

an_awesome_lib
  |--- an_awesome_lib

and then use -I/home/an_awesome_lib.

Or you can create include/an_awesome_lib inside an_awesome_lib:

an_awesome_lib
  |--- include
         |--- an_awesome_lib

and then use -I/home/an_awesome_lib/include.

Evg
  • 25,259
  • 5
  • 41
  • 83