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

  • Thread starter Thread starter Eclair_de_XII
  • Start date Start date
  • Tags Tags
    files
AI Thread Summary
The discussion centers around issues with including header files and linking a C++ library using the GNU C Compiler. A user encounters a "not found" error when trying to include "header_one.h" and seeks clarification on whether the compiler is misinterpreting the include paths. It is highlighted that the correct command for compiling should specify both the library and header file locations, using the appropriate flags for linking. The importance of avoiding multiple inclusions of header files is emphasized, with suggestions for using include guards or the `#pragma once` directive. The conversation reflects the complexities of managing dependencies in C++ and the challenges faced by users when linking libraries.
Eclair_de_XII
Messages
1,082
Reaction score
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"?
[CODE lang="c" title=".headers/header_one.h"]int add(int x, int y){
return x+y;
}[/CODE]

[CODE lang="c" title="./headers/header_two.h"]#include "headers/header_one.h"

int subtract(int x,int y){
return add(x,-y);
}[/CODE]

[CODE lang="c" title="./sample.c"]//#include "headers/header_one.h"
#include "headers/header_two.h"

int main(void){
return 0;
}
[/CODE]

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".

[CODE lang="cpp" title="./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/[/CODE]
"""
 
Technology news on Phys.org
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:
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.
 
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.
 
On a second note:
Function declarations are commonly placed in header files (*.h). so:
[CODE lang="c" title="./headers/header_one.h"]int add(int x, int y);
[/CODE]
[CODE lang="c" title="./headers/header_two.h"]#include "headers/header_one.h"
int subtract(int x,int y);
[/CODE]

Then the function definitions are placed in a c file:
[CODE lang="c" title="./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);
}
[/CODE]

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:

[CODE lang="c" title="./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_
[/CODE]

Or, if your compiler supports the #pragma once directive:
[CODE lang="c" title="./headers/header_two.h"]
#pragma once
#include "headers/header_one.h"
int subtract(int x,int y);
[/CODE]
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Back
Top