compile c and c++ source code

January 27th, 2007 mysurface Posted in Developer, g++, gcc | Hits: 113901 | 31 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++

g++ hello.cc

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

./a.out

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 matrix.cc, 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/libncurses.so.5. -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

#include"yobe.h"

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]

31 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 linux.byexamples.com. 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 17.4.1.2 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.
    Thanks

  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

    ld.so.1: ext.out: fatal: libstdc++.so.6: open failed: No such file or directory
    Killed

    When I do ldd ext.out Iam getting the following
    libstdc++.so.6 => (file not found)
    libm.so.1 => /usr/lib/libm.so.1
    libgcc_s.so.1 => (file not found)
    libc.so.1 => /usr/lib/libc.so.1
    libdl.so.1 => /usr/lib/libdl.so.1
    /usr/platform/SUNW,Sun-Fire-V490/lib/libc_psr.so.1
    and I have path for libstdc++.so.6 and libgcc_s.so.1 referenced in LD_LIBRARY_PATH

    also libstdc++.so.6 linked to libstdc++.so.6.0.8

    what is the problem going on

  9. It seems to me your glibc have facing problem. Probably your symlink (i assume libstdc++.so.6 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 http://www.onlamp.com/pub/a/onlamp/2007/04/03/getting-familiar-with-gcc-parameters.html

  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. hello.cc: In function ‘int main()’:
    hello.cc:4: 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++ hello.cc
    hello.cc:1:21: error: iostream.h: No such file or directory
    hello.cc:2: error: ‘::main’ must return ‘int’
    hello.cc: In function ‘int main()’:
    hello.cc:4: 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 hello.cc introduces the compilation error.

    #include<iostream>

    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!
    –tb01

Leave a Reply