How to include external library? - c++

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"

Related

Can't link a static binary to #include it

Good day,
I have a file that I'm trying to compile and within it has an #include to a statically linked binary.
#!/bin/bash
g++ -Wall -std=c++17 Message.cpp ../textmagic-rest-cpp/lib/libtextmagic.a
I am getting the following error: fatal error: libtextmagic.h: No such file or directory
The relative path that I provided is correct under the assumption that the current working directory is the directory in which the script is called/ran. I might be linking the binary incorrectly and I've searched around the internet but the other posts/resources did not help me.
Note that the script is run in the same directory as Message.cpp.
g++ has the -I and -L flags that do that for you. Your flag will look like this: -I/ThePathToYourHeaders and -L/ThePathToYourLib. I don't know if g++ supports relative paths there but absolut paths are guaranteed to work there.
Also you probably need to add a linker flag. For your project it will be -ltextmagic. It is just the name of the .a file you want to link with, without the lib in front of the filename.
The #include directive needs to "read" the header file you give it as argument, and that is not included in the static library.
You can either include using a relative path to the source file or pass the location of the header file to the compiler using the -I argument.

Forcibly included header not found

I'm trying to set up precompiled headers in a large C++ project compiled with GCC 5.4.
There is a file "PrecompiledHeaders.h", which includes all the other relevant headers. I've added the compile flag -include PrecompiledHeaders.h, but when compiling, the header is not found:
cc1plus: fatal error: PrecompiledHeaders.h: No such file or directory
compilation terminated.
CMakeFiles/Project.dir/build.make:62: recipe for target 'CMakeFiles/Project.dir/NetworkGameState.cpp.o' failed
But I'm sure it exists, in the same directory as all other h and cpp files. What's more, manually adding #include "PrecompiledHeaders.h" to the top of "NetworkGameState.cpp" does not produce an error.
What could go wrong?
This is a CMake, out-of-source build, by the way.
It's likely an issue with the path. From the GCC 5.4 manual (emphasis mine):
-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.
You need to either supply the full (relative or absolute) path to the header as the argument of the -include flag, or add its parent directory to the search chain using e.g. -I or -iquote.

where to place the header and lib files

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

c/c++ : header file not found

Some header files are present in /src/dir1/ (eg: a.h, b.h, c.h etc). My source file is present in /src/dir2/file.cpp. I used some header files that are present in /src/dir1/ but during compilation I got errors like header file not found.
Then I changed the include path like #include "../src/dir1/a.h", then error is gone in file.cpp but I get not found error in the headers files that are present in /src/dir1. Because I included the header file say a.h, that a.h included some other header files that are present in /src/dir1/ (say b.h and c.h present in a.h).
How to add the header file (a.h) in /src/dir2/file.cpp so that it should not ask to modify the include path in the header files that are present in /src/dir1/?
Note: I am using scons to build.
You can add directories to the include file search path using the -I command line parameter of gcc:
gcc -I/src/dir1 file.cpp
SCons FAQ:
How do I get SCons to find my #include files?
If your program has #include files in various directories, SCons must somehow be told in which directories it should look for the #include files. You do this by setting the CPPPATH variable to the list of directories that contain .h files that you want to search for:
env = Environment(CPPPATH='inc')
env.Program('foo', 'foo.c')
SCons will add to the compilation command line(s) the right -I options, or whatever similar options are appropriate for the C or C++ compiler you're using. This makes your SCons-based build configuration portable.
Note specifically that you should not set the include directories directly in the CCFLAGS variable, as you might initially expect:
env = Environment(CCFLAGS='-Iinc') # THIS IS INCORRECT!
env.Program('foo', 'foo.c')
This will make the program compile correctly, but SCons will not find the dependencies in the "inc" subdirectory and the program will not be rebuilt if any of those #include files change.
It's not found because it's not there. You have one extra level of indirection. A file in "/src/foo/" would include a file in "/src/bar/" with "include ../bar/the_file"
In other words, in your example, there is no "../src/" relative to dir1 or dir2. The relationship is "dir1/../dir2" or "dir1/../../src/dir2"
To see this for yourself, make dir1 your current directory (chdir /src/dir1) and compare the difference betwee "ls .." and "ls ../src". The second ls will not work but the first one will.
Make sense? hope that helps

Unable to include header files in C++

In netbeans I am creating a new folder and adding header files to it.
Now when I include the header file within the newly created folder to another file by using:
#include "folder1/myheaderFile.h"
The compiler complains that it is unable to find the header file.
The error is:
main.cpp:31:39: fatal error: folder1/myheaderFile.h: No such file or directory
Is there some way out as I want to include the header files within a folder in my #include?
EDIT: Do i need to make a makefile for every folder?
Another EDIT:
When I right clicked on the error its showing
unresolved directive
#include
Analyzed system include paths:
/usr/include/C++/4.6
/usr/include/C++/4.6/x84_64_linux_gnu
/usr/include/C++/4.6/backward
/usr/lib/gnu/x86_64-linux-gnu/4.6/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu
/usr/include
Here's for your convenience:
The include file paths you have specified are for system-wide headers. Is the header you are including yours or downloaded/installed system-wide? Do you see the path of the header in the output?
If you are including the header which is in a folder, from another folder, then you need to traverse back, i.e: #include "../folder/header.h"
If this is a system folder, such as the ones residing in /usr/local/include in my system, all you have to do is
#include <header.h>
or if it resides in a sub-folder (quite often),
#include <Libname/header.h>
As long as you have set the include paths pointing at it, it should work.
To setup the include paths and directories, see example: http://zetcode.com/articles/netbeanscdevelopment/ near the end of the page.
Remember that when you hardcode paths, you need to take into consideration the current path of the file which is including the header.
Alternatively, you can use cmake & make (don't know what Netbeans uses), where you define everything your self.
You can test with full path, i.e:
#include "/home/user/project/folder/header.h
or you can test from command line and set the include path.
Hope it helps :)