On Saturday 26 July 2003 16:19, allan espinosa wrote:
> how does compiling a c and c++ program differ? 
> can i issue flags to g++ or gcc when compiling c++ programs so i will not
> have to wait for 2-4 minutes of building?

generally, C will compile faster than C++.  C++ is a much more complex
language.  if you use templates, it because *much* more complex.

lower optimization levels might give you an improvement at
compiling.  while debugging, you should use no optimization anyway.
use the optimization flags only when you've got the basic bugs
out of your code.  at that point you can start looking for bugs that
optimization inserts into your program.  and if there are none, or
when you've fixed all of those too, then it's time to compile your
program for distribution (all necessary optimizations and without
debugging symbols).

in my experience, if you use C++ as just a better C, then it compiles
fast enough.  it'll still be a bit slower than C, but not by very much.
if you're doing highly object oriented code though, and if you're
using templates a lot, well, compile time is the price you pay for
the advantages that the language gives you.

let's see:  compiled hello world with printf.  in C it takes .16 sec.
in C++ it takes .33-.34 sec.  these are all after several test runs to
make sure that disk access doesn't affect it, everything it needs to
compile is in linux' memory cache.

to compile hello world using iostreams/cout, it takes 1.79 seconds.

there are some things you can do.  i had a link to a document that
discussed how to optimize your headers to minimize includes at
compile time.  unfortunately, i can't find it.  some of the same suggestions
are in:

http://www.gipsysoft.com/articles/fcomp/fcomp.shtml

i.e.,

1. don't include files that you don't need.

2. if a class has pointers to some other class or struct, use forward
declarations instead of including the header file that defines the
needed class.

3. regarding #1, in the C++ program that implements the functions
declared in the corresponding header (e.g., x.cpp, implementing
functions x.hpp), include x.hpp at the very top of x.cpp, before
ANY other includes.  that way, you get compiler warnings about
things that need to included (or forward declared) in x.hpp.  if you
can, try to set up x.hpp so that it includes NOTHING.  after including
it at the very top of x.cpp, you might get compile errors about undeclared
classes, structs or functions(used in inline functions).  only *then* do
you include the headers needed in x.hpp.  that way, you get the
absolute minimum includes.  that can speed up compile times a lot.

consider:

#include <stdio.h>
int main(){printf("hello world\n"):  // compiles in .34-.35 secs in C++ mode

adding a #include <string> makes it compile in 1.05-1.08 seconds. if
you could remove unnecessary #include <string>, you would save
half a second for each file where you removed the include.

basically, C++ is just a more complex language and will take
longer to compile.  the harder you make it for the compiler (e.g., you
use exceptions, include lots of classes, and use templates) the longer
compiling will take.

heh, the link given above suggests getting a faster hardware (faster 
disk, faster CPU, SMP, more RAM, etc).  however, this is probably
not what you want to hear :).

good luck.

tiger
-- 
Gerald Timothy Quimpo  gquimpo*hotmail.com tiger*sni*ph
http://bopolissimus.sni.ph     an xcdngl nntrstng jrnl
Public Key: "gpg --keyserver pgp.mit.edu --recv-keys 672F4C78"

  my name is Inigo Montoya.  You killed my father.  Prepare to die.

--
Philippine Linux Users' Group (PLUG) Mailing List
[EMAIL PROTECTED] (#PLUG @ irc.free.net.ph)
Official Website: http://plug.linux.org.ph
Searchable Archives: http://marc.free.net.ph
.
To leave, go to http://lists.q-linux.com/mailman/listinfo/plug
.
Are you a Linux newbie? To join the newbie list, go to
http://lists.q-linux.com/mailman/listinfo/ph-linux-newbie

Reply via email to