How to include two header files that are contained in the same folder?

  • Thread starter Eclair_de_XII
  • Start date
  • Tags
    files
In summary: This is called an "include guard". Only the first time this file is included is the content processed.And, in case you're curious, the #pragma once directive is not part of the c standard. It is a gcc extension.In summary, when creating a library in C++, it is important to properly link all the dependencies and include guards can be used to prevent issues with multiple inclusions of header files.
  • #1
Eclair_de_XII
1,083
91
TL;DR Summary
Let's say I have a directory called "headers". Contained in that directory are two files, "header_one.h" and "header_two.h". The former defines an "add" function that the latter uses in order to define a "subtract" function. Let's say we have a source file that is one level below "headers", "sample.c" that #include's "header_two.h". How would I compile the program without gcc complaining about not being able to find "headers/header_one.h"?
.headers/header_one.h:
int add(int x, int y){
    return x+y;
}

./headers/header_two.h:
#include "headers/header_one.h"

int subtract(int x,int y){
    return add(x,-y);
}

./sample.c:
//#include "headers/header_one.h"
#include "headers/header_two.h"

int main(void){
    return 0;
}

If I run this, the compiler tells me that "headers/header_one.h" is not found. I don't understand why this is. Is it assuming that I'm #include-ing "header_one.h" from "headers/header_two.h", instead of from "sample.c"?

The reason I bring this up is because I am having great difficulty linking a C++ library to my programs. The problem presented above is one of the workarounds I came up with. But the true problem is as follows:

"""
I just installed a C++ library, but I'm having trouble linking it to a sample program using the GNU C Compiler. The command I'm using in order to compile my program is "g++ main.cc -L './installed/x64_linux/include' -l 'cpr/cpr'; ". But the compiler doesn't seem to be able to compile the sample program that I copy-pasted from the official website of the library.
This would be the script that I used in order to download the package.

Bash:
git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg integrate install
./vcpkg install cpr

This would be the program that I'm trying to compile from the directory "./vcpkg".

./vcpkg/main.cc:
#include <cpr/cpr.h>
//#include "cpr/cpr.h" // Complains about a missing "cpr/api.h" file

int main(int argc, char** argv) {
    cpr::Response r = cpr::Get(cpr::Url{"https://api.github.com/repos/libcpr/cpr/contributors"},
                      cpr::Authentication{"user", "pass", cpr::AuthMode::BASIC},
                      cpr::Parameters{{"anon", "true"}, {"key", "value"}});
    r.status_code;                  // 200
    r.header["content-type"];       // application/json; charset=utf-8
    r.text;                         // JSON text string
}

// https://docs.libcpr.org/
"""
 
Technology news on Phys.org
  • #2
The command I'm using in order to compile my program is "g++ main.cc -L './installed/x64_linux/include' -l 'cpr/cpr';
-L is for specifying a location to look for library files in. Usually your libraries are in a folder called lib or lib64 and header files are in a folder called include. To specify the location of your header files, use -<capital i> <path where your headers folder is>.

e.g. it should look something like this:

g++ main.cc -L ./installed/x64_linux/lib/cpr -lcpr -l ./installed/x64_linux/include

Where the first -l is a lowercase L and the second is an upper case i.

This assumes you have your libaray file in ./installed/x64_linux/lib/cpr.
 
Last edited:
  • #3
Thank you. Your post was most informative. However, for some odd reason, my "ld" program keeps saying that it cannot find the "cpr/cpr" library. I am well-aware that this is not a technical forum for me to ask questions about a specific library, so I think it is reasonable to not ask further about my technical difficulties, which I would be able to resolve if only I would learn C++ properly.
 
  • #4
Thanks for the help. I found out that I had to link all the dependencies of the "cpr" library/header, in addition to "cpr" itself.

g++ main.cc -L ./installed/x64_linux/lib/ -I ./installed/x64_linux/include/ -lcpr -lcurl -lssl -lcrypto -lz -lpthread

It's unwieldy to have to include all these libraries in order to use just the one. I think there should be a better way. I was just linking the libraries as indicated in the library directory, and skimming through the error messages I received in order to obtain a functioning compilation command. Trial-and-error isn't exactly a good technique in this field I should think, but in my exhaustion, I was getting pretty desperate.
 
  • #5
On a second note:
Function declarations are commonly placed in header files (*.h). so:
./headers/header_one.h:
int add(int x, int y);
./headers/header_two.h:
#include "headers/header_one.h"
int subtract(int x,int y);

Then the function definitions are placed in a c file:
./math.c:
#include "headers/header_one.h"
#include "headers/header_two.h"

int add(int x, int y)
{
    return x+y;
}
int subtract(int x,int y)
{
    return add(x,-y);
}

One notable exception to this rule are "inline" functions. But I'm guessing you haven't encountered any of those yet.

On a third note:
Your header_two.h includes header_one.h - a common occurrence in c programming.
But it means that had math.c included both of those, header_two.h would have been included twice.
In general, including a header file twice is inviting trouble, so this is how that is handled:

./headers/header_two.h:
#ifndef _HEADER_TWO_INCLUDED_
#define _HEADER_TWO_INCLUDED_

#include "headers/header_one.h"
int subtract(int x,int y);

#endif // _HEADER_TWO_INCLUDED_

Or, if your compiler supports the #pragma once directive:
./headers/header_two.h:
#pragma once
#include "headers/header_one.h"
int subtract(int x,int y);
 

FAQ: How to include two header files that are contained in the same folder?

How do I include two header files that are contained in the same folder?

To include two header files that are contained in the same folder, you can use the #include directive followed by the name of the header files, separated by a space. For example: #include "file1.h" "file2.h"

Can I include multiple header files in the same folder without using the #include directive multiple times?

Yes, you can use the #include directive to include multiple header files in the same folder by listing them all separated by a space. This will save you from having to use the #include directive multiple times.

What if the two header files have the same name?

If the two header files have the same name, you can use the #include directive followed by the relative path to the header file you want to include. For example: #include "folder1/file.h" "folder2/file.h"

Can I include header files from different folders using this method?

Yes, you can include header files from different folders using the #include directive followed by the relative path to the header file you want to include. For example: #include "../folder1/file.h" "../folder2/file.h"

Is there a limit to the number of header files I can include using this method?

No, there is no limit to the number of header files you can include using the #include directive. However, including too many header files can lead to longer compilation times, so it is important to only include the necessary header files for your program.

Similar threads

Replies
15
Views
2K
Replies
23
Views
7K
Replies
15
Views
5K
Replies
2
Views
479
Replies
5
Views
2K
Replies
1
Views
1K
Replies
49
Views
10K
Back
Top