Re: About #include statements and other related questions
Vicent Giner-Bosch wrote:
(1) Which is the difference between a ".cpp" file and a ".h" file? Why
is it more frequent including ".h" files than ".cpp" ones?
It's rather the other way round. If a file is supposed to be included, you
give it a name that ends in .h (other names are also used sometimes, like
e.g. .hpp or .hxx). It's basically a convention.
(2) Which is the difference between #include <something>, #include
<something.h>, #include "something.h"? Are they interchangeable?
Include with <> and "" use different search paths to find the header file.
When including a system header or library header, use <>. When including a
header from your own project, use "".
There is not really a difference between #include <something> and #include
<something.h> except that the first includes a header named something, the
other includes one named something.h. But since they include different
headers, they are not interchangeable.
(3) I have ".cpp" example files that contain #include
<something.h> and I have to change it by #include "something.h"
in order to make it work -- I mean, if I try to compile the ".cpp"
file, I get an error when I have #include <something.h> ("No such
file or directory"), and I don't get any error or warning when I
change it by #include "something.h". It always happens the same,
with every project (even with examples, as I said), no matter which
files I am including. Is that an expected behavior?? Is it maybe
something related with MS Visual C++ options or settings??
There are compiler options that can be used to add directories to the header
search path. If you add the current directory (".") then you can probably
also use <> to include your own headers, but the preferred way is to use ""
for those.
(4) The biggest problem/question here:
I am working with the GNU Scientific Library (GSL), specifically with
GSL 1.11 "prepared" for Windows by David Geldreich (http://
david.geldreich.free.fr/dev.html). I downloaded and unzipped the
"binaries for Windows" package. I copied the "gsl" folder (the one
which is in the "include" folder, and that contains lots of ".h"
files) in the same directory where my main ".cpp" file is.
Is there no way to correctly "install" it?
In my main ".cpp" file I have #include "gsl/gsl_cdf.h", because
#include <gsl/gsl_cdf.h> didn't work for me, as I said at (3).
The best option would be to put all the stuff that belongs to the library
into a subdirectory of your project, then add its header directory to the
compiler's search path.
I compile the main file (Ctrl+F7), and everything is OK.
Then, I try to build the main file (F7), called "OPCA_01.cpp", and I
get this error:
""" Linking...
OPCA_01.obj : error LNK2001: unresolved external symbol
_gsl_cdf_ugaussian_P
Debug/OPCA_01.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe. """
In my program, I call a function called "gsl_cdf_ugaussian_P", which
is defined in the file "gsl_cdf.h", which is in the "gsl" folder.
My guess is that it's not defined in that file, but rather only declared.
I don't understand why I get that estrange error. I didn't typewrite
"_gsl_cdf_ugaussian_P" in any moment.
It's a "mangled" name - a name that the compiler uses internally to refer to
that function.
So, can you help me to understand what am I doing wrong???
You probably need to link to the library. The header file only tells the
compiler which functions there are and how you have to call them (argument
list, return value, ...), but it doesn't contain the actual function code.
That code is put in a library which you have to link your program to.