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

Discussion Overview

The discussion revolves around issues related to including header files in C/C++ programs and linking a C++ library using the GNU C Compiler. Participants explore the correct usage of include directives, library linking commands, and the organization of function declarations and definitions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a problem with the compiler not finding "headers/header_one.h" and questions whether the inclusion is being interpreted incorrectly.
  • Another participant suggests using the -I option to specify the location of header files, contrasting it with the -L option for library files.
  • A participant expresses frustration with the linker not finding the "cpr/cpr" library and acknowledges their own limitations in learning C++.
  • One participant shares a solution involving linking multiple dependencies of the "cpr" library, expressing concern about the unwieldiness of this approach.
  • A later reply discusses the common practice of placing function declarations in header files and provides examples of how to prevent multiple inclusions of the same header file using include guards.

Areas of Agreement / Disagreement

Participants present various approaches and solutions, but there is no consensus on the best method for linking libraries or organizing header files. The discussion remains unresolved regarding the optimal practices for these issues.

Contextual Notes

Some participants mention specific commands and practices that may depend on the structure of their projects and the configuration of their development environments. There are also references to potential issues with including headers multiple times, which may lead to complications if not handled properly.

Who May Find This Useful

This discussion may be useful for programmers working with C/C++ who are facing challenges with header file management and library linking, particularly those new to the languages or the GNU C Compiler.

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