compile c and c++ source code

January 27th, 2007 mysurface Posted in Developer, g++, gcc | Hits: 166538 | 50 Comments »

There are plenty of c and c++ compiler under unix based operating system, but the most famous one should be GNU gcc compiler. A well known open source kernel Linux is compiled by gcc as well. This post is to introduces you how you can use gcc to compile c and c++ source code, if you are looking at how to compile projects or packages which consist of configure and make files, please read Installing from tarballs.

The simplest way to compile and get to run the compiled binary, do as bellow:

gcc hello.c

or if your source code is written in c++


By default, gcc will create an output binaries, a.out. To execute the binary,


Okay, now I want gcc to create output file with the filename i specified,

gcc -o hello hello.c

I like to do this, combine with curly brackets

gcc -o hello{,.c}

For some projects, it need more then one source code for compilation, specified them all

gcc -o hello{,.c} {dolly,polly,molly,suzie,lorry}.c

Okay, some projects uses dynamic libraries. Projects that uses ncurses library, for example ncurses matrix.

To compile, you need to tell the compiler you are going to use ncurses library.

g++ -o matrix{,.cc} -lncurses

If ncurses library is installed, it probably installed at /lib with lib headings the file name, such as /lib/ -l indicate that the library is in /lib (default lib directory).

What if your library is not in default directory?
list the directories for -l to search by using -L (capital EL)

gcc -o something{,.c} -L. -lyobe

-L. (minus EL dot) , the dot indicate the library is in the current directory, probably a library libyobe is in current directory.

How about the header files?
The library usually come with header files with extension .h, let say yobe.h is in yobe_inc directory.

gcc -o something{,.c} -L. -lyobe -I yobe_inc

Based on the manual of gcc, -I is depreciated, you can use -iquote. It is more make sense somehow, -iquote search for your includes only when you specified your includes in ” ” such as


Okay so,

gcc -o something{,.c} -L. -lyobe -iquote yobe_inc

I think it is more than enough for introductory gcc example post. let me add one to finalize the post, you can check for compatibility of your source code whether it is competible with ANSI C or not by doing this:

 gcc -o last{,.c} -ansi

[tags] programming, c , c++ , linux, compiler [/tags]

50 Responses to “compile c and c++ source code”

  1. Thanks for the tips, it’s useful for gcc beginners.

  2. gcc is my favorite way to compile. I don’t know why, but it doesn’t screw up compiling header files as much as visual studio does. (And who wants to use MS anyway, right?)

    One last thing, if you have a project in the works that you need to compile multiple times, it’s all about the makefile ;)

  3. […] To compile D source code, you can uses GDC, it works just like gcc and g++. […]

  4. […] More examples of gcc/g++ compiler shows here. […]

  5. […] For more g++ examples, check out To print string in colors without ncurses, here. To print string at any position without ncurses, here. […]

  6. hi why iam gettign the following message when I do compile
    In file included from /usr/local/lib/gcc/sparc-sun-solaris2.8/4.1.2/../../../../include/c++/4.1.2/backward/iostream.h:31,
    from g.c:1:
    /usr/local/lib/gcc/sparc-sun-solaris2.8/4.1.2/../../../../include/c++/4.1.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section of the C++ standard. Examples include substituting the header for the header for C++ includes, or instead of the deprecated header . To disable this warning use -Wno-deprecated.

  7. It is just a warning message, it state that your include headers are deprecated or antiquated. To know more about that search “c++ deprecated or antiquated header” at google.

    It do suggest you to ignore the warning with using option -Wno-deprecated
    gcc -Wno-deprecated -o g{,.c}

  8. Iam having troube with gcc 4.1.2 in .Iam doing simlpe program and when do I compile and run the output Iam gettign the followign error ext.out: fatal: open failed: No such file or directory

    When I do ldd ext.out Iam getting the following => (file not found) => /usr/lib/ => (file not found) => /usr/lib/ => /usr/lib/
    and I have path for and referenced in LD_LIBRARY_PATH

    also linked to

    what is the problem going on

  9. It seems to me your glibc have facing problem. Probably your symlink (i assume is a symlink) is not link appropriately or maybe some other reason which i couldn’t guess with just seeing few lines or error you have pasted.

    It seems that you are using sun os, why don’t you ask the support from them?

  10. […] republication of website content required direct track back link / You can leave a response, or trackback from your own site. Leave aReply […]

  11. A related article “Getting Familiar with GCC Parameters” at

  12. how do i run it and where do i store the source code
    i am unable to open the directory stored itself in terminal

  13. In function ‘int main()’: error: ‘cout’ was not declared in this scope

  14. error: iostream.h: No such file or directory

  15. @jagan

    I think you need to use g++, because iostream.h is c++ not c. For c++ compile and link with g++.

  16. g++ error: iostream.h: No such file or directory error: ‘::main’ must return ‘int’ In function ‘int main()’: error: ‘cout’ was not declared in this scope

  17. i use4d g++ only but i got the same error

  18. @jagan

    Its the content of your introduces the compilation error.


    using namespace std;

  19. thxs very much!!!!!!!!
    i hav finally compiled and it was just in time that u informed me .

  20. Thanks! This site is really helpful especially for gcc beginners.

  21. Thanks for this helpful source code!

  22. Hi,
    I am getting the following error what might be the reason?

    bash: ./a.out: No such file or directory

  23. how do i run it and where do i store the source code
    i am unable to open the directory stored itself in terminal

  24. Thank helpful lil examples

  25. I simply couldn’t leave your website prior to suggesting that I actually loved the standard info an individual provide to your visitors? Is gonna be back ceaselessly to inspect new posts

  26. Nice post. I was watching continuously this site and I’m impressed! Extremely useful info especially the last part I care for such info much. I was looking for this specific info for a long time. Thank you and best of luck.

  27. error:’::main’ must return ‘int'(c++ in ubuntu)

  28. Thanks for the example and source code, exactly what I was looking for, very informative!!

  29. We’ve examine quite a few just right products in this article. Worth book-marking to get returning to. I personally astonish the fact that lot try out you add to make such a fantastic informative website.

  30. Hurrah! Finally I got a blog from where I know how to
    truly take helpful data regarding my study and knowledge.

  31. Just want to say thanks to everyone helping out here, for putting together a support community to help newbies & transplants. I’m hoping to (*finally*) start working on that LFS project I’ve wanted for a couple of years now! I’ll feel a LOT more confident doing so with this forum as a resource, and I will certainly exhaust available info-search options =first=.

    Thank you, again!

  32. Nice post. I was checking constantly this blog and
    I’m impressed! Extremely helpful info specifically the last part :
    ) I care for such information a lot. I was looking for this certain information for a long time.
    Thank you and best of luck.

  33. I do not even know how I ended up here, but I thought this post was great.
    I don’t know who you are but certainly you’re going to a famous blogger if
    you aren’t already ;) Cheers!

  34. I’ve been exploring for a bit for any high quality articles or
    weblog posts in this sort of space . Exploring in Yahoo I finally
    stumbled upon this website. Studying this information So i’m happy to express
    that I’ve a very excellent uncanny feeling I came upon exactly what I needed.

    I so much for sure will make sure to do not omit this
    website and provides it a glance regularly.

  35. Do you have a spam problem on this website; I also am a blogger, and I was
    wondering your situation; we have developed some nice methods
    and we are looking to exchange strategies with others, why not shoot
    me an email if interested.

  36. Please let me know if you’re looking for a article writer for your site.
    You have some really great posts and I believe I would be a good asset.
    If you ever want to take some of the load off, I’d
    really like to write some content for your blog in exchange for a link back to mine.
    Please shoot me an email if interested. Cheers!

  37. It’s going to be finish of mine day, but before ending I am reading
    this wonderful paragraph to improve my knowledge.

    Here is my blog post locksmiths state of Pennsylvania map

  38. Simply prefer to say a person’s content is just as astonishing. A resolution in your send is just nice and i can think you are expert on this subject matter. Fine with your permission let me to grasp the feast to settle up-to-date having certain article. Many thanks Just one, Thousand, 500 and also be sure to carry on the gratifying do the job.

  39. Have you ever thought about adding a little bit more than just your
    articles? I mean, what you say is important and all.

    However think about if you added some great
    visuals or videos to give your posts more, “pop”!
    Your content is excellent but with images and videos, this site
    could definitely be one of the greatest in its niche.
    Excellent blog!

    My blog post web page (Jere)

  40. Hello to every one, the contents present
    at this site are really awesome for people experience, well, keep up the
    good work fellows.

  41. When you compare these statistics with previous years’ data – 237 reported piracy attacks this year and 75 in 2012 – there’s little
    doubt left that the menace of piracy at the very least off the
    coast of Somalia is apparently on a sharp decline.
    The Ordinance split the sub-section 3k into two- sub-section 3(k) and 3(ka).
    The only viable competitor to the telltale new innovations is
    IMAX which may play film content with an extreme 10000.

  42. Thanks for the auspicious writeup. It if truth be told
    was a entertainment account it. Look complicated
    to far brought agreeable from you! By the way,
    how can we keep in touch?

  43. And not all of us are lucky enough to have enough garage space (or even a garage at all) at home to store an automobile or other vehicle.
    This article investigates the different purposes and relates these to the features available on the market today.
    Bow-tied, pencil-thin moustached maitre d’s named Enzo have been exchanged for eyelid-pierced, soul-patched waiters named Elijah or Chloe.

  44. Conflict of Clans hack tools are fairly secret applications that almost all most likely the builders of the sport aren’t even conscious of their existence.

  45. Some songs are based on history, such as ‘City Of Steel’
    (an objective synopsis of the World War II battle of Stalingrad) or ‘Bloodsoaked Mission’ (about
    a schoolmate that was killed by an IED while training Afghan police officers).
    This article investigates the different purposes and relates
    these to the features available on the market today.

    If parking the car is to be the sole purpose of your metal garage, the size of
    the garage will be the main factor in looking for one
    to buy.

  46. Thanks to my father who shared with me about this web
    site, this webpage is really amazing.

  47. It’s the best time to make some plans for the future
    and it is time to be happy. I’ve read this post and if I could I wish to suggest you some interesting things or tips.
    Perhaps you can write next articles referring to this article.
    I desire to read even more things about it!

  48. Right away I am going to do my breakfast, when having my
    breakfast coming over again to read further news.

  49. Good post. I learn something new and challenging on blogs I stumbleupon everyday.
    It’s always exciting to read content from other authors and use something from their
    web sites.

  50. Hello, I desire to subscribe for this website to get
    newest updates, so where can i do it please assist.

    my web site situs qq online (Cirandas.Net)

Leave a Reply