gcc can't find header file - c++

I have this main in a project in c++ but I can't compile it in gcc due to this error:
In file included from main.cpp:11:
../framework/application.h:8:10: fatal error: includes.h: El fitxer o directori no existeix
8 | #include "includes.h"
| ^~~~~~~~~~~~
What it says is that the file "includes.h" doesn't exist. It exist but is in another directory and I don't know how to compile using a header from another directory.
I tried:
gcc -I ../framework/ main.cpp
gcc -I../framework/ main.cpp
gcc -I "../framework/" main.cpp

Testing on my machine with a similar directory structure using the I directive worked for me. Ensure that your path is correct and I would even try using an absolute path as a test.

Related

Error compiling C++ source utilizing the Boost.Math library

I'm trying to use a couple of functions from the Boost Math library in some C++ code using the G++ compiler but I've been unsuccessful. This is on macOS.
I downloaded and extracted the Boost tar.gz from here and placed it into my source folder.
Within my C++ I've tried
#include "boost_1_63_0/boost/math/distributions/chi_squared.hpp" and
#include <boost_1_63_0/boost/math/distributions/chi_squared.hpp>.
The quotation version partially works but the chi_squared.hpp file includes fwd.hpp using the bracket (#include <...>) notation and that breaks my compilation with error In file included from main.cpp:9: ./boost_1_63_0/boost/math/distributions/chi_squared.hpp:12:10: fatal error: 'boost/math/distributions/fwd.hpp' file not found #include <boost/math/distributions/fwd.hpp>.
To compile I've used an assortment of commands, all unsuccessfully:
g++ -L /boost_1_63_0/boost/math/distributions main.cpp
g++ -I"/boost_1_63_0/boost/math/" main.cpp
g++ -I "/boost_1_63_0/boost/math/" main.cpp
g++ main.cpp -lboost_math
What is the correct include statement and G++ command that I need to use?
Resolved using
#include "/Users/[me]/[project_dir]/boost_1_63_0/boost/math/distributions/chi_squared.hpp"
and
g++ -I/Users/[me]/[project_dir]/boost_1_63_0/ main.cpp

gcc can't find libcurl header file in the directory it is located

I am trying to compile a cpp using with the following command:
g++ -IC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\include\curl -LC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\lib program.cpp
this program has a header file which uses libcurl. the curl library is at -
C:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\include\curl\curl.h
gcc gives the following error even though curl.h is in the path -I
mylibrary.h:26:10: fatal error: curl/curl.h: No such file or directory
#include <curl/curl.h>
what am I doing wrong?
The error message means the file curl/curl.h could not be found in the search path specified by -I. You updated the question with the path to the file so the command should be:
g++ -IC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\include -LC:\Users\XXX\libcurl-7.56.0\libcurl-7.56.0\lib program.cpp
Also, can you please try using \ instead of / as path separator in your mylibrary.h file:
#include <curl\curl.h>

Problem with C++ compiler on macOS (building cmake project)

I have a 16inch MacBook pro with latest updates (macOS Catalina 10.15.2 (19C57)), the latest version of XCode and Command line tools for macOS (the tools being installed in the usual path /Library/Developer/CommandLineTools/).. Not sure if this is relevant to my issue or not, but I also have the latest version of JetBrains’s CLion installed on my machine (running without issues).
Here is the problem: When trying to build a project with cmake, the build FAILS, giving me this error message:
In file included from /Users/basavyr/Pipeline/DevWorkspace/Github/xrootd/bindings/python/src/PyXRootDFile.cc:25:
In file included from /Users/basavyr/Pipeline/DevWorkspace/Github/xrootd/bindings/python/src/PyXRootD.hh:28:
In file included from /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/Python.h:38:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string.h:61:15: fatal error:
'string.h' file not found
#include_next <string.h>
1 error generated.
error: command '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc' failed with exit status 1
make[2]: *** [bindings/python/python_bindings] Error 1
make[1]: *** [bindings/python/CMakeFiles/python_target.dir/all] Error 2
make: *** [all] Error 2
and this is probably related to my C++ compiler not having the correct path to the include files. To see what the issue could be, I’ve made a simple C++ file and I tried to compile it.
main.cpp
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
int main()
{
std::string name = "NAMEX";
std::cout << name;
}
First, I tried with the usual “g++” and it worked just fine:
basavyr#Roberts-MacBook-Pro stringTestCLANG % g++ main.cpp
basavyr#Roberts-MacBook-Pro stringTestCLANG % ./a.out
NAMEX% basavyr#Roberts-MacBook-Pro stringTestCLANG %
Then, I tried with /Applications/Xcode.app/Contents/Developer/usr/bin/g++ main.cpp and it worked just fine.
Checking the version of “g++” on my machine gives me this:
g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.0 (clang-1100.0.33.17)
Target: x86_64-apple-darwin19.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
This is interesting, because I was expecting that the default compiler is installed in /usr/bin/ instead. Going into that “InstalledDir” folder I saw that there is no “g++” binary, but only “clang++” and “c++”, so I tried to compile with both and it turns out that both compilers are giving the same error as the one in the project that I trying to build:
basavyr#Roberts-MacBook-Pro stringTestCLANG % /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ main.cpp
In file included from main.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:505:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string_view:176:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__string:57:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:642:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cstring:61:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string.h:61:15: fatal error:
'string.h' file not found
#include_next <string.h>
^~~~~~~~~~
1 error generated.
basavyr#Roberts-MacBook-Pro stringTestCLANG %
Since I also have Command Line Tools installed, I tried to compile with both “g++” and “clang++” from /Library/Developer/CommandLineTools/usr/bin/ and the first one compiles successfully, while the second one throws the same error with regards to the string header file.
basavyr#Roberts-MacBook-Pro stringTestCLANG % /Library/Developer/CommandLineTools/usr/bin/c++ main.cpp
In file included from main.cpp:1:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iostream:38:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/ios:216:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__locale:15:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string:505:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string_view:176:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/__string:57:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:642:
In file included from /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/cstring:61:
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/string.h:61:15: fatal error: 'string.h' file not found
#include_next <string.h>
^~~~~~~~~~
1 error generated.
So, I assume that when I build that project with cmake, it is using one of these compilers (from the XCodeDefault.xctoolchain folder) and it has issues with the string header.
Q1: How can I solve this issue? Q2: And also, why does my “g++” installation directory points to this XCodeDefault.xctoolchain instead of “/usr/bin/’ or “/Applications/Xcode.app/Contents/Developer/usr/bin/”. Is there a way to change that in macOS?
Thank you in advance

compiling C++ mac, look for header files not found

Based on this tutorial:
http://syskall.com/how-to-roll-out-your-own-javascript-api-with/index.html/
I am trying to compile a C++ program on a mac, however the includes in my C++ file are not being found. I have the following directory structure:
myProj/
|-- deps/ # third party code
| `-- v8
`-- src/
`-- myProj.cpp
in the myProj.cpp, I have several includes:
#include <include/v8.h>
so when i go to compile, I use the following:
g++ src/jsnotify.cpp -Ideps/v8/include
the deps/v8/include directory clearly has v8.h, but it still shows up as not found. is -I the correct flag for mac? I am also having trouble in linking:
g++ src/jsnotify.cpp -Ideps/v8/ -Ldeps/v8/ -lv8 -lpthread -v
the -lv8 causes:
ld: library not found for -lv8
clang: error: linker command failed with exit code 1
Look at exactly what you're telling the compiler:
#include <include/v8.h>
"open the file "include/v8.h"
g++ src/jsnotify.cpp -Ideps/v8/include
"When trying to find files to include, search in deps/v8/include"
So, the obvious question: does deps/v8/include contain include/v8.h? In other words, do you have the file deps/v8/include/include/v8.h?
As you have it, the pre-processor is trying to resolve #include <include/v8.h> to deps/v8/include/include/v8.h.
Change your include to be:
#include <v8.h>
Or change your compiler command line to:
g++ src/jsnotify.cpp -Ideps/v8
Either option is likely to work - but if v8.h also specifies additional include files specified by prepending the "include" path (e.g. #include <include/foo.h>) then the second option is more likely to work.

Missing header file

I am new to C++ programming and trying to add a library (Yepp) to my cpp file.
I am trying to compile and it says it cannot find a header file from the external library. The external library, yeppp, has a .so file which I placed in a lib folder in the root directory.
I am building with the following command:
clang++ -O3 test.cpp -o test -L lib/ -lyeppp
Here's the error:
test.cpp:7:10: fatal error: 'yepCore.h' file not found
#include <yepCore.h>
You need to tell the compiler where to find the header file. Use the -I option.