i just started with c++ programming.
For my new work, i have to download, install and take use of an external library. It is called ICE.
It was composed as a .tar file, so i decomposed it inside my home-directory "/home/foo/ice".
Now, there is the directory: "/home/foo/ice/src", within all the .h headers, i need for the program.
But can i tell the compiler, where he can find all these new headers?
I mean only with #include, he obviously doesn't know.
What i need:
#include <image.h>
"image.h" is inside "/home/foo/ice/src"
Greetings
If you have gcc compiler you can use -I option.
From the manual :
-I dir: Add the directory dir to the list of directories to be searched for header files.
So for you it should be something like this:
g++ myprog.cpp -I /home/foo/ice/src -o myprog
But it is better to install the library, you should have some readme.txt or INSTALL file about how to do this..
You asked about linking a library, but your description shows that you have problems with the include path, which klm123 answered already.
The library paths for linking is another option, usually -Llibpath
It may help to check the options of you compiler, here for example the Directory Options of GCC
Related
Hi everybody I recently created a C++ project in which I put my code into header.h and header.cpp files and successfully create a static library called header.a. Now I put my header.h file into /usr/local/include position of my system and header.a into /usr/local/lib in order to "install" my library into the machine. Now, if I want to use this library, I have to do the following steps:
Suppose I am using a main.cpp program, I include this line at the top of the program:
include <header.h>
Then, I compile with this command:
g++ main.cpp /usr/local/lib/header.a
And all it's ok. But I want to find a way to "store" permanently the header.a library into the system in order to use it like a normal standard C++ header, simplifing the compilation in this way:
g++ main.cpp
Is there a way to do this? Many thanks to all.
You can't, and no system library will be linked automatically without being told to do so.
You can however add the path /usr/local/lib to the default paths for the linker to look for libraries (IIRC it's not in there by default for Ubuntu), which means you only need to add the -l (lower-case L) option to link with the library.
But do note that the library should be having a lib prefix. Like in libheader.a.
Then link with -lheader:
g++ main.cpp -lheader
There also the -L option to add a path to the list of paths that the linker searches, if you have other non-standard paths, of if you can't edit the system configuration to use /usr/local/lib:
g++ main.cpp -L/usr/local/lib -lheader
The library file still needs the lib prefix.
I am trying to incorporate a C library into some Rcpp code.
I can use the C library in a C++ program easily. I 'make' the C library, which creates the .a and .dll files in the /lib folder. I can then use the package by including the header in the program and running something like this from command line:
cc myfile.cpp -o myfile -Ipath.to.header path.to.lib.a -lz
This essentially tells the compiler to take the .cpp program, include headers from -I, and to link to two libraries.
Getting this to work with Rcpp shouldn't be overly difficult if I understand makevars correctly (which I unfortunately don't seem to).
I add the library to a folder in my package, and in src I add a makevars and makevars.win that look like this:
PKG_CFLAGS=
# specify header location
PKG_CPPFLAGS=-Ipath.to.lib/include
# specify libs to link to
PKG_LIBS=path.to.lib/lib/file.a -lz
# make library
path.to.lib/lib/file.a:
cd path.to.lib;$(MAKE)
This correctly 'makes' the .a and .dll files for the library, however none of the Rcpp magic runs (i.e. in the build I never see the g++ system call that compiles the files in src), so "no Dll was created".
I am fairly certain this is a problem in my makevars target that makes the library. When I remove that portion from the makevars, and 'make' the library from the command line myself before building the package, I get the proper g++ calls with my -I and -l statements, but I get errors about undefined references.
I notice that the -l statements are only included in the final g++ call where the final .dll is made, but isn't included in the earlier g++ calls where the files with the library headers are compiled.
So I have two problems:
How do I fix my makevars so that it 'makes' the library, but doesn't stop Rcpp from compiling the files in src?
How do I deal with the undefined references? The library is clearly not header-only, so I am guessing it needs the -l statement in the earlier g++ calls, but that may not even be possible.
The best approach is to avoid complicated src/Makevars file altogether.
One easy-ish approach around this: use configure to build your static library, then once you actually build just refer to it in src/Makevars.
I use that scheme in Rblpapi (where we copy an externally supplied library in) and in nloptr where we download nlopt sources and build it 'when needed' (ie when no libnlopt is on the system).
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.
I'm trying to use libjson within a C++ project and the docs tell me to just "add libjson's source to your project, comment JSON_LIBRARY in the JSONOptions.h file and any C++ compiler should compile it."
Being quite new to C++ and all that, how exactly am I supposed to do that (not using any IDE)? Should I just #include the libjson.h file and that's it? Shouldn't I reference libjson somehow in my call to g++ when compiling my project?
thx in advance
If you go into the libjson library folder, you will see a makefile. Navigate to that directory in a terminal and type:
make
then
make install
Then, in your code
#include <libjson.h>
or, depending on your include path:
#include <libjson/libjson.h>
That should be all that you need to do.
If you need additional help, you can post in the help forum at sourceforge (I am the author of libjson)
You have to:
One,
#include <libjson.h>
in order to get access to the functions and data types the library offers, then
Two, link against the libjsonz library:
g++ -o myprogram myprogram.c -ljson
(the -ljson flag has to come last or you'll get a linker error with never versions of GCC.)
EDIT: if you need to build the library, you typically have a configure script or a Makefile. See how to use them.
if you install json you should find include file at /usr/local/include
so
#include <json/json.h>
gcc exasmple.c -ljson
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.