compilation error when including directory containing headers - c++

I have a directory maths which is a library that is comprised solely of header files.
I am trying to compile my program by running the following command in my home directory:
g++ -I ../maths prog1.cpp prog2.cpp test.cpp -o et -lboost_date_time -lgsl -lgslcblas
but I get the following compilation error:
prog1.cpp:4:23: fatal error: maths/Dense: No such file or directory
compilation terminated.
prog2.cpp:6:23: fatal error: maths/Dense: No such file or directory
compilation terminated.
maths is located in the same directory(i.e. my home directory) as the .cpp files and I am running the compilation line from my home as well.
prog1.cpp and prog2.cpp have the following headers
#include<maths/Dense> on lines 4 and 6 respectively, hence I am getting the error.
how do I fix it.

You can either change your include path to -I.. or your includes to #include <Dense>
Wait, if maths is in the same directory as your source files and that is your current directory, you can either change your include path to -I. or your includes to #include "Dense"

maths is located in the same directory(i.e. my home directory) as the .cpp files
Your include path is given as -I ../maths. You need -I ./maths – or simpler, -I maths since maths is a subdirectory of the current directory, not of the parent directory. Right?
Then in your C++ file, use #include <Dense>. If you want to use #include <maths/Dense> you need to adapt the include path. However, using -I. may lead to massive problems1, I strongly advise against this.
Instead, it’s common practice to have an include subdirectory that is included. So your folder structure should preferably look as follows:
./
+ include/
| + maths/
| + Dense
|
+ your_file.cpp
Then use -I include, and in your C++ file, #include <maths/Dense>.
1) Consider what happens if you’ve got a file ./map.cpp from which you generate an executable called ./map. As soon as you use #include <map> anywhere in your code, this will try to include ./map instead of the map standard header.

Related

How to get G++ to find import files

I'm trying to compile a C++ file on a Mac that uses opencv2.
I downloaded opencv, found an opencv2 folder in it, and copied it into the folder where main.cpp (the program I'm trying to run is).
The folder structure is:
main.cpp
BRIEF.h
opencv2
core.hpp
core
cvdef.h
My current command (after reading this and this) is g++ -I/opencv2/core.hpp main.cpp, run from within the same folder main.cpp is in.
main.cpp has #include "BRIEF.h" which works fine.
BRIEF.h has #include "opencv2/core.hpp" which works fine.
core.hpp has #include "opencv2/core/cvdef.h" which fails saying the file was not found.
How do I use G++ such that the file can be found?
As you said, you are only including this file/opencv2/core.hpp so it is normal that the compiler fails finding the other files.
In your code you are including your files starting from your project root directory, so you have to include the project root directory for the compiler to find these files:
g++ -I$(PROJECT_DIR) $(PROJECT_DIR)/main.cpp
or if you run g++ from the root directory, you can use ., which will expand into the current path (your project root directory):
g++ -I. main.cpp

Linking a shared library from a large github project

I'm trying to use the following github project https://github.com/apache/parquet-cpp. I was able to build it and the .so files are available in parquet-cpp/build/latest. I copied the .so files(both of libparquet as well as libarrow which had been built) in a separate directory and wrote a simple hello world, simply importing the library as:
#include <arrow/io/file.h>
#include <parquet/api/reader.h>
#include <parquet/api/writer.h>
Now I ran
g++ -Wall test.cpp -L. -lparquet -larrow
However this throws an error as unable to find .h files of arrow/parquet. What am I doing wrong?
You forgot to include the path for the header files in the compilation instruction. You need to find directory containing parquet/api/reader.h and include it in the compilation command
g++ -Wall -I path_containing_header test.cpp -L. -lparquet -larrow
You may include several directories with multiple -I instruction.

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.

Compilation failing - no #include - boost

I'm trying to compile a third-party library, but g++ is complaining about the following line:
typedef boost::shared_ptr<MessageConsumer> MessageConsumerPtr;
The strange thing is, there is no #include directive in the file - and it is clearly supposed to be this way; there are about 60 files with the same (or very similar) issues. Clearly if there was an #include directive referencing the relevant boost header this would compile cleanly.
My question is: how can I get g++ to somehow automagically find the relevant symbol (in all instances of this issue, it is a namespace that can't be found - usually std:: or boost::) by either automatically processing the relevant header (or some other mechanism).
Thanks.
Edit
My current g++ call looks like:
g++ -fPIC -O3 -DUSING_PCH -D_REENTRANT -I/usr/include/boost -I./ -c MessageInterpreter.cpp -o MessageInterpreter.o
You can use the -include command line option:
g++ -include boost/shared_ptr.hpp ...
From the man page:
-include file
Process file as if "#include "file"" appeared as the first line of
the primary source file. However, the first directory searched for
file is the preprocessor's working directory instead of the
directory containing the main source file. If not found there, it
is searched for in the remainder of the "#include "..."" search
chain as normal.
Create your own wrapper .h file that includes the boost .h and then the broken .h .
Or (very fragile) ensure that you precede every use of the broken .h with boost .h .
Perhaps the third-party library is designed in such a way that you should always include a certain "main" header file in order to get the dependencies right.
Otherwise, you can add #include <boost/shared_ptr.hpp> before including the third-party header file that is giving the error message.