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

  • Context:
  • Thread starter Thread starter Eclair_de_XII
  • Start date Start date
  • Tags Tags
    files
Click For Summary
SUMMARY

This discussion addresses the challenges of including multiple header files in C/C++ and linking a C++ library using the GNU C Compiler (g++). The user encountered issues with the compiler not finding "headers/header_one.h" and difficulties linking the "cpr" library after installation via vcpkg. The solution involves using the correct flags for library and header file locations, specifically -L for library paths and -I for include paths, along with linking all necessary dependencies such as -lcurl, -lssl, -lcrypto, and -lz.

PREREQUISITES
  • Understanding of C/C++ programming and compilation processes
  • Familiarity with GNU C Compiler (g++) and its command-line options
  • Knowledge of header file conventions in C/C++
  • Experience with library management using vcpkg
NEXT STEPS
  • Learn about C/C++ header file best practices and include guards
  • Research the use of vcpkg for managing C++ libraries
  • Explore advanced g++ command-line options for optimizing compilation
  • Investigate dependency management techniques for C++ projects
USEFUL FOR

C/C++ developers, software engineers working with libraries, and anyone troubleshooting header file inclusion and linking issues in their projects.

Eclair_de_XII
Messages
1,082
Reaction score
91
TL;DR
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]
 

Similar threads

Replies
3
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K
Replies
5
Views
6K
  • · Replies 23 ·
Replies
23
Views
9K
  • · Replies 15 ·
Replies
15
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 49 ·
2
Replies
49
Views
12K
  • · Replies 13 ·
Replies
13
Views
21K