- #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/