[Gllug] c++ newbie complier probs

Jonathan Harker jon at jonathanharker.co.uk
Tue Oct 22 22:34:48 UTC 2002


On Wednesday 23 Oct 2002 10:11 am, Sean Burlington wrote:
> argghhhh!!!!!!!!!1
>
>
> spotted my dumb mistake !!!!
>
>
> should have done
> #include <ccc_empl.cpp>
>
> instead of
>
> #include <ccc_empl.h>

#including .cpp files is generally a very bad move. If the ccc stuff is 
written in a half decent style, you should only need to include the .h file. 
Generally, you include .h files (which are usually interfaces) and the 
implementation is tucked away in .cpp files; Your compiler/linker will 
magically link the corresponding .o (or if there isn't one, compile a fresh 
one from the .cpp).

Secondly, there are two ways to #include something in C++, using < > brackets 
or using " " quotes.

#include <iostream>

leaves off the .h and is reserved for standard libraries included with your 
particular C++ implementation like the STL or iostreams, whereas

#include "myclass.h"

is for your own stuff and third party libraries. You're having weird link 
errors because -I only works for the second syntax.

However, you're still not out of the woods! You will probaby need to specify 
the link directory as well. It will build the .o in the cccfiles directory, 
but you haven't told the linker to look there.

-I tells the compiler where to look for your .h files
-L tells the linker where to look for your .o files

Also, you want to be careful about where you put your using namespace 
statements. Always put them AFTER your #includes, thus:

#include <iostream>
#include <iomanip>
#include "ccc_empl.h"

using namespace std;

Otherwise you pollute ccc_empl.h (and the global namespace - eeek!) with std 
names that may clash. using namespace is really only for preproduction, debug 
or transitional code, as it clutters your namespaces. Good practice is to 
replace using namespace statements with individual using declarations, or 
tuck them away inside small units such as classes or functions. So, in your 
code, you're only using std::cout and std::endl, so your using namespace std 
could become

using cout;
using endl;

Anyway, blah blah blah, what you need to do is:

// #include <ccc_empl> -- won't work
// #include <ccc_empl.h> -- still won't work
// #include <ccc_empl.cpp> -- catastrophically bad idea! :-)

#include "ccc_empl.h" // will work! Yay!

then, try this:

g++ -Wall -I/path/to/cccfiles -L/path/to/cccfiles 4.cpp

(You may also want to go -o programname otherwise you'll just get a file 
called a.out)

Hope all that helps! Let me/us know how you get on.

Luv Jon.

-- 
Jonathan Harker
www.jonathanharker.co.uk


-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list