Cant compile rocksdb, dependencies not found - c++

I am trying to compile a program that uses rocksdb.
According to the example in the official webpage, the only header i should add to my code is db.h.
Now, the file i am compiling is in folder A.
db.h however is in A/rocksdb-master/include/rocksdb/.
So, i add this line to my file:
#include "rocksdb-master/include/rocksdb"
It finds the file, but the problem is that inside db.h, i have this line:
#include "rocksdb/metadata.h"
And when i compile i get this error:
fatal error: rocksdb/metadata.h: No such file or directory
#include "rocksdb/metadata.h"
I mean, it's obvious. db.h is in the same folder as metadata.h, so it's fine that the compiler cant find any rocksdb folder. But i doubt that people who wrote this library don't know that.
Is there any other way to add the path's to compile it?
Why is it that the path from db.h are not relative to where it is located?

You should normally use just the following header in your project:
#include "rocksdb/db.h"
When compiling your own project, you should then add the RocksDB include path to the list of include directories. For example, if the RocksDB source code is in directory ../rocksdb-master, the include path will be ../rocksdb-master/include.
How to add the include path to the compiler flags is indeed compiler-specific. With g++ or clang, it's done by passing -I../rocksdb-master/include to the compiler when compiling your own program. Note that you many need to link against the RocksDB library as well.
And finally, you may need to include some more RocksDB headers if you use some of its advanced concepts, e.g. transactions.

Related

Setting C++ include path via program code line

There are already many options listed of how to add a path to a C++ compiler so that the #include <...> command works on these paths. However, assume that I have a single file (not an entire project) and I want to add an include path just for this file alone. I'd like to do this via a line of code within the cpp-file (say, as very first line). How is this possible? Why? Because I need to include some header file from another directory which in turn depends also on other header files within that same directory (and I get error messages that these other files could not be found due to the fact that this path was not added to the include-list).
For example:
Let's assume I want to include file_a.h in directory
.../include/extra,
I can do that via
#include <extra/file_a.h>
However, if, e.g., I do not have the extra-directory directly as a sub-directory of include, or the file_a wants to include some other file from somewhere else (maybe even /extra, but it's not a sub directory of include e.g.), then I run into trouble, because then the tracking of directories/dependencies gets hard.
I thought it would be a bad habit to change those directories via compiler, so I thought better solution would be to integrate it into the program, so no matter which compiler I use, it would work anyway without even having to think about afterwards, once specified, which directories I have to add.
Per my understanding you did:
#include <absolute/path/to/header/header.h
or
#include <relative/path/to/header/header.h
But into the header.h some other include are included.
#include <header_1.h>
#include <header_2.h>
[...]
#include <header_n.h>
Those other headers haven't relative/absolute path, so compiler doesn't know how to find them.
To solve this you can use (using gcc) the -I compiler option:
-I dir
Add the directory dir to the list of directories to be searched for header files during preprocessing. [...]
Emphasis mine
So you can use
#include <header.h>
In your file and compile it using
gcc ... -I/path/to/headers ...
When you have to specify one or more include paths in the compile command, you can do it as follows:
g++ -I/path1/to/headers -I/path2/to/headers YourProgram.cpp
Include paths tell the compiler where it finds the files it actually shall include into other files. This is (usually) controlled via compiler options as LPs explained in this answer.
C++ standard does not provide any facilities to do this from within a C++ source file and I am not aware either of any compiler extension of any vendor that would allow doing so, so bad luck...
Now depending on the IDE you are using (hopefully you are using one...), though, you most likely can add include paths for files individually there (would be a strange IDE if it didn't allow...), e. g. with eclipse + GCC, right click the file, select "Properties" -> C/C++ Build -> Tool Settings -> GCC C++ Compiler -> Includes.
Alternatively you could use a make file instead (actually, eclipse in standard settings generates one for you automatically...), which again allows you to set compiler options for each file individually - either written directly by yourself or generated by some other tools facilitating this such as cmake.

No such file (header file) error

I want to include a header file. I am working in a C++ environment, (C++11, Windows OS, Netbeans 7.3.1 IDE, Cygwin_4.x tool collection). I do not know how I setup the environment/IDE (I did it 6 months ago). I do not understand the fundamentals of the C++ build process or Cygwin related issues either, so I might have to fill in the gaps with some other references/documentation (on what specifically, I'm not sure).
My ultimate objective is to be able to include header files using a syntax that does not require the full file path. I want to write something terse, like:
#include "src\stuff\blah.h" //or even better: #include "blah.h"
The only way I can get my program to compile at all is by using the full file path, like this:
#include "C:\NetBeansProjects\Project1\src\stuff\blah.h"
And, I can only compile once using the full path. If I try to rebuild, it will bomb with the *** multiple target patterns. Stop. error. There are workarounds for this error; those being either 1) deleting the build and dist folders between each rebuild (yikes?), or 2) following this 16 step setup process.
I do not want to follow either of those workarounds because they do not appear to deliver what I want. How can I setup my environment to achieve what I want...of being able to include header files without using full paths?
Thanks to DanielKO for this answer.
In my case, I was able to include with the syntax:
#include "../stuff/blah.h"
I did not have to configure anything under the "Code Assistance" section for the C++ compiler.
All of my code is under "src" as the parent directory in my NetBeans project. It seems that the full path of the file is not required, and the only directory that must be referenced is the lowest level subdirectory (in my case, "stuff").
In NetBeans I've added the path to the list of libraries:
Go to Properties->Select C++->Select 'include libraries'->'Add'
Now: Add the path of the project folder with option "absolute"
Go to Properties->Select C++->Select 'Additional library directories'->'Add'
Now: Add the path of the project folder with option "absolute"
It's very obscure to me why the project doesn't recognize "own" header files.

Xcode - header files issue with .cpp file

I have imported my project from windows to mac and Is using xcode to compile the project.
I have managed to fix most error and bugs from the porting process but there is one bug/error I am unable to determine why its not working as it should. This probably due to my lack of knowledge of xcode but was wondering if anyone is able to help me with this problem.
The problem is this;
I have a two sub folders in my project, once called include and another called source. Of course include contains all the header files and source contains all the cpp files and such.
Example:
Root/
Include/
Header.h
Source/
test.cpp
Now in the build settings in the xcode project, I have added $(SRCROOT)/Include/ in the Header Serach Paths. So therefore I presume that xcode will include any header files in the Include folder.
So in the test.cpp class I add #include "Header.h". When I click build I get an error saying "'Header.h' file not found".
Im not entirely sure what I am doing wrong, of course I can do this for example - #include "../Include/Header.h" but that's going to be a pain going through all the cpp files in my project (which is a lot).
I was just wondering if anyone else came across this problem?
Thanks for reading.
In general you need to add the headers to the User Header Search Paths, which if you search the help in XCode will give you the format you need to pass the directory as.

C++ include libraries

Ok, so it's been a while, and i'm having problems with #includes
So I'm doing
#include "someheader.h"
but it's giving me
fatal error: someheader.h: No such file or directory
It's a system wide library I guess you could say.
I'm running arch linux and I installed the library from the repo, and I think the .h files are in /usr/include.
I could just copy all the header files into the folder my code is in but that would be a hack.
What is the "right" way to do this?
Edit: I wasn't correct by saying the .h files were in /usr/include, what I meant was that the library folder was in there
So, Emile Cormier's answer worked to a certain extent.
The problem now is that there are some include in the header file and it seems from the methods I'm trying to access that those includes are not happening
it's giving my the error
undefined reference to Namespace::Class::method()
Edit:
Ok so the final answer is:
#include <library_name/someheader.h>
And compile with
g++ code.cpp -llibrary_name
Sometimes, header files for a library are installed in /usr/include/library_name, so you have to include like this:
#include <library_name/someheader.h>
Use your file manager (or console commands) to locate the header file on your system and see if you should prefix the header's filename with a directory name.
The undefined reference error you're getting is a linker error. You're getting this error because you're not linking in libsynaptics along with your program, thus the linker cannot find the "implementation" of the libsynaptics functions you're using.
If you're compiling from the command-line with GCC, you must add the -lsynaptics option to link in the libsynaptics library. If you're using an IDE, you must find the place where you can specify libraries to link to and add synaptics. If you're using a makefile, you have to modify your list of linker flags so that it adds -lsynaptics.
Also the -L <path_to_library> flag for the search path needs to be added, so the linker can find the library, unless it's installed in one of the standard linker search paths.
See this tutorial on linking to libraries with GCC.
You'd use #include <someheader.h> for header files in system locations.
#include "someheader.h" would try to include the file someheader.h in the directory of your .c file.
In addition to including the header file, you also need to link in the library, which is done with the -l argument:
g++ -Wall youprogram.cpp -lname_of_library
Not doing so is the reason for the "undefined reference .. " linker errors.
The quick fix is to do use:
#include <someheader.h>
assuming that someheader.h is in the standard include locations (to find it use the command locate someheader.h in a shell. If it is in /usr/include it is in a standard location. If it is in a subdirectory of /usr/include you only need to add the part of the directory up to /usr/include in the #include directive (e.g. #include <fancy_lib/someheader.h>)
However, this is only half of the story. You also will need to set up your build system in a way that locates the given library and adds its include path (the path under which it's header files are stored) to the compiler command (for gcc that is -I/path/to/header). That way you can also build with different versions by configuring them in your build system. If the library is not header-only you will also have to add it to the linker dependencies. How this is achieved in your build system is best found out by consulting its documentation.

Question about C++ folder structure for header file

I'm quite new in C++ after few years in Java and eclipse, I got little bit confusing using code::blocks, no autogenerate setter/getter and also implement interface :D.
I wanna ask about code structure in code::blocks, I create new console application, my header will be put to Headers/include folder called Employee.h, then .cpp will be put to src folder.
Now I create main class (I put outside of src folder) who will call the header, I just append the include code like this :
#include "Employee.h"
sure then the errors appeared after compiling:
error : Employee.h: No such file or directory.
how to link the header to the main class properly?
this is my folder structure :
updated :
It works, my include folder needs to be added on build options.
Really thanks
You need to add your include directory to your compiler's include path. This is going to be compiler-specific. e.g., if your structure is:
code
code/src
code/include
and you're running g++ from a terminal in the 'code' directory, you'd need to run (assuming your .cpp is Employee.cpp):
g++ -Iinclude src/Employee.cpp
I suspect you're running some sort of IDE, though: if so, do a search in its help for "include path" and it should tell you how to set it up correctly.
If you want to include your employee.h you must #include "employee.h" not Employee.h. Those are two different files.
You shouldn't be adding include paths to your build options for header files that are actually part of your project. It didn't find the header file from the EmployeeTest.cpp because you didn't use the full relative path.
You need:
#include "include/Employee.h"
You should only be adding include paths to your compiler for additional libraries that aren't added to the typical /usr/local/include or /usr/include directories.