compile c and c++ source code
January 27th, 2007 mysurface Posted in Developer, g++, gcc | Hits: 31897 |
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]
Live Chat!









January 27th, 2007 at 4:58 am
Thanks for the tips, it’s useful for gcc beginners.
January 27th, 2007 at 5:28 pm
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 ;)
January 27th, 2007 at 11:44 pm
[...] To compile D source code, you can uses GDC, it works just like gcc and g++. [...]
March 14th, 2007 at 9:24 pm
[...] More examples of gcc/g++ compiler shows here. [...]
March 30th, 2007 at 1:45 am
[...] 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. [...]
April 3rd, 2007 at 12:11 am
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
April 3rd, 2007 at 5:50 pm
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}April 3rd, 2007 at 7:14 pm
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
April 3rd, 2007 at 8:57 pm
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?
April 10th, 2007 at 2:48 am
[...] republication of website content required direct track back link / You can leave a response, or trackback from your own site. Leave aReply [...]
April 23rd, 2007 at 11:02 am
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