std Namespace
g++ and CC -E option runs your code through the
preprocessor and nothing else.
#include "include-file-name"
#include <include-file-name>
< > and "" have different search paths.
< > usually searches only the system directories.
" " searches other directories first, then system directories.
" " searches . (the current directory).
-I Option-I compiler command-line option manipulates the "..."
search paths.
<...> search paths.
g++ and CC.
g++ -Imylib ...
./mylib, then ., then ...
-I options are cumulative.
g++ -Iliba -Ilibb ... searches liba, then libb then ...
< > search path
too.
g++ support for this is too ugly to talk about.
#ifndef idiom for include files.
#ifndef _includefile_h_ #define _includefile_h_ // Included material here. #endif
static, non-extern variables in include files
is (usually) a mistake.
* cat a.cc
#include "i.h"
void a() { }
* cat b.cc
#include "i.h"
int main() { }
* cat i.h
#ifndef _i_h_defined_
#define _i_h_defined_
int i;
#endif
* g++ -c a.cc
* g++ -c b.cc
* g++ a.o b.o
b.o: multiple definition of i
a.o: first defined here
ld returned 1 exit status
*
|
|
.h, .hh, .H, .hxx
.h
.h standard includes.
<string> is not <string.h> is not <cstring>
/ vs. \.
std Namespacestd namespace.
std::.
using declarations make this less cumbersome.
std:: is useful documentation.
std:: or explicitly eliminate it.
std name space.
max() and std::max() can live in harmony.
using namespace x.
| Bad: | Good: |
|---|---|
#ifndef _t_h_defined_ #define _t_h_defined_ #include <vector>
|
#ifndef _t_h_defined_
#define _t_h_defined_
#include <vector>
class board {
std::vector<int> b;
// and so on
};
#endif
|
<iostream>, <iomanip>, <fstream>
<vector>, <list>, <map>, <iterator>,
<algorithm>, <utility>, <queue>, <stack>,
<deque>, <set>, <bitset>, <functional>,
<memory>
<exception>, <stdexcept>
<string>, <sstream>
<strstream> (it's deprecated).
<locale>, <limits>, <typeinfo>
std namespace, sometimes.
.h and prepend a 'c'.
assert.h, use
#include <cassert>
.h forms from C++ is nonstandard.
c-prepended headers make the proper C++ extern
declarations, .h headers do not.
<cassert>.
assert((0 <= i) and (i < N));
assert()'s a macro, no std::.
<cctype>.
ch = std::toupper(ch);
<cmath>.
double x = std::log(M_PI);
M_PI is a macro, log() is a function; std::M_PI doesn't compile.
<climits>.
cout << "Biggest int is " << INT_MAX << "\n"
std::INT_MAX doesn't compile either.
<ctime>.
strcasecmp(), strings.h, X Window System includes.
#include source (.cc) files, but the STL does.
c prefix too.
This page last modified on 22 January 2004.