where to place the header and lib files - c++

I have program which I need for a graphic interface. In the installation it says I should copy the "some_header.h" in my favorite include directory and the "libsomething.so" in my favorite lib directory. I know copied them to /usr/include/program-name and to /usr/lib/program-name respectively. When I know try to compile a c++ program using this graphic program, I get the error 'fatal error: some_header.h: No such file or directory, #include "some_header.h"'
What do I have to do, so the file is found?

compile your application with:
g++ -I/usr/include/program-name -L/usr/lib/program-name -lsomething .......
and this should sort out the directories.
-I is for include directories
-L is for library directories
-l is to use the given library
Another solution for the header file is to put it in the directory where your source is and then #include "some_header.h" is supposed to work out of the box.
Update
Of course, you always can put your files in standard system directories, such as: /usr/lib and /usr/include

Related

I am getting compiler errors while compiling my code in unix

i have two folders named Source and powerserver in my home directory. In source direectory i have some code related to project and it uses a .h header file from power server directory. when am compiling using make -f somename.lnx32 I am getting the below error.
error: cst.h no such file or directory.
I am new to this i dont know how to access that .h file ?
GCC looks in several different places for headers. On a normal Unix system, if you do not instruct it otherwise, it will search in default directories.
You can add directories to this list with the -I command-line option. All the directories named by -I are searched, in left-to-right order, before the default directories.
Try gcc -c -I/path_to_powerserver_directory to include header files in GCC search path.
Currently this particular header file is not found to gcc in it's search paths.
It's tagged Linux so i guess you use GCC.
#include <someheader.h> means to search someheader.h in system directories (ie. /usr/include), which you can override using the -Ipath option with GCC.
But in your case, you should be using #include "someheader.h" with the correct user path in your Makefile with the -iquote option.

GCC compiler cannot find hpp files

I am trying to install the hep-mc library listed here: https://github.com/cschwan/hep-mc for use on compute using the instructions listed in the documentation here: https://github.com/cschwan/hep-mc#installation . To compile one of the example files, I typed this into the terminal:
g++ -L/usr/local/hep-mc/include vegas_mpi_ex.cpp -o vegas_mpi
but I get these error messages:
mpi_vegas_ex.cpp:1:22: error: hep/mc.hpp: No such file or directory
mpi_vegas_ex.cpp:2:26: error: hep/mc-mpi.hpp: No such file or directory
mpi_vegas_ex.cpp:8:17: error: mpi.h: No such file or directory
in the beginning of my code, the declarations are like this:
#include "hep/mc.hpp"
#include "hep/mc-mpi.hpp"
#include <mpi.h>
The tutorial states that I should point the compiler to the location of the "include" folder that contains all the .hpp files, which I have done. Do you guys have any idea as to what I'm doing wrong?
It should also be noted that the compiler cannot find the mpi.h directory even though I have loaded the openmpi module.
-L sets paths where the linker searches for libraries to link. The option you're looking for is -I, which sets the paths where the compiler searches for #included files.
g++ -L/usr/local/hep-mc/include vegas_mpi_ex.cpp -o vegas_mpi
Oops!
g++ -I/usr/local/hep-mc/include vegas_mpi_ex.cpp -o vegas_mpi
-L specifies the path to library files; -I specifies the path to includes.
This is confusing because in terms of project management and distribution, we consider "the library" to include both binaries and header files, as well as documentation and all sorts of goodies. But at a technical level that is not what "library" means.

include<apis/api1/api.h> throws No such file or directory

#include<apis/api1/api.h>
throws No such file or directory
i even tried moving api.h and api.cc to the main project directory and using
#include<api.h>
does the same thing even though it is in the exact same directory that the other classes use
i tried adding /apis/api1 to the compiler search path
that just crashes the compiler can someone tell me what to type into the compilers compilation line
#include <api.h>
is the way you include a system header. (That is, the header for a library installed on your system.) It does not search in the directory of the source file or in directories specified in -I command line arguments.
#include "api.h"
is the way you include your own headers. (But it will also search library header locations if it doesn't find the header locally.)

How to include external library?

Hi i would like to include a external library call NTL. its in the path as followed:
#include "WinNTL-5_4_2/include/NTL/tools.h"
My file is key.cpp and its reside in the same folder as NTL . but when i compile, it came up with another error which is ..
In file included from keygen.cpp:6:0:
WinNTL-5_4_2/include/NTL/tools.h:5:24: fatal error: NTL/ctools.h: No such file or directory
compilation terminated.
As its need another file call ctools.h, how do i includes tools.h to make ctools.h work also
I tried type
me#ubuntu:~/GG$ g++ keygen.cpp -o keygen -l WinNTL-5_4_2/include/
keygen.cpp:6:23: fatal error: NTL/tools.h: No such file or directory
but it doesnt work still.
Based on the error message, you should try changing your include to:
#include "NTL/tools.h"
and make sure that WinNTL-5_4_2/include is in your compiler's search path for include files.
You need to add the root directory as part of your project's search path for include files. It depends on your environment exactly how to do this, but there are usually two search paths -- one for include files and another for compiled libraries. Set that with the directory that contains the WinNTL-5_4_2 directory and you should be golden.
You had to add "fullpath/WinNTL-5_4_2/include" in you include path of your compiler (either by -I for gcc or in the include path list of a visual studio project)
Expecting that ctools.h exists in ".../WinNTL-5_4_2/include/NTL"

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.