Simple GCC Usage
The name GCC consists of the initials of the words "GNU Compiler Collection". It takes its real name from the words GNU C Compiler. The reason for this change is that the GCC package used to consist only of the C compiler. Other languages were later added to the GCC family.
A visit to http://en.wikipedia.org/wiki/GNU_Compiler_Collection can be made for information on this subject. You can follow the latest versions of the GCC package at http://gcc.gnu.org/.
To find out the version of GCC you are using;
# gcc -v
You can use the command. Returning result in my system:
# gcc -v
Reading specs from /usr/lib/gcc-lib/i386-slackware-linux/3.2.2/specs
Configured with: ../gcc-3.2.2/configure --prefix = / usr --enable-shared
--enable-threads = posix --enable -__ cxa_atexit --disable-checking --with-gnu-ld
--verbose --target = i386-slackware-linux --host = i386-slackware-linux
Thread model: posix
gcc version 3.2.2
In order for the compiler to compile applications correctly, some parameters must be given correctly. Let's write our application in its simplest form and move on to our first compilation process.
#include <stdio.h>
int main ()
{
printf ("Sample application");
}
We started with our simplest application above. Let's move on to compiling this application. After writing the application with any text editor, save it as first.c and write the following lines at the command prompt in the console.
# gcc ilk.c
If there is no error in the application after this line or if it does not give a warning message, it is immediately sent to the command prompt. However, when looking at the directory with the source code, it will be seen that a file named a.out has occurred. We have compiled our application without giving a name. In such cases, gcc will use the a.out file name by default. Now, let's compile our application with its own name. For this purpose, -o option is used. The -o option takes the output filename as a parameter:
# gcc first.c -o first
After the command line, an executable file with the first name will now be created in the relevant directory. After this stage, the process of compiling a simple application is now understood. But how should it be compiled if some external libraries are wanted in the application? At this stage, let's make a few changes on our sample simple application;
#include <ncurses.h>
int main ()
{
initscr ();
getch ();
endwin the ();
printf ( "done \ n");
}
There is no getch () function in the GNU C library. However, if ncurses library is used, this command is available. Note that stdio.h was not added to the application. As a preliminary information, stdio.h is not necessary if ncurses.h is used. Let's compile this application with the first standard compilation method we learned below.
# gcc first.c -o first
/tmp/cc4k34cd.o(.text+0x11): In the function `main ':
: Undefined reference to initscr
/tmp/cc4k34cd.o(.text+0x1a): in main 'function:
: Undefined reference to stdscr
/tmp/cc4k34cd.o(.text+0x1f): in main 'function:
: Undefined reference to wgetch
/tmp/cc4k34cd.o(.text+0x27): in main 'function:
: undefined reference to endwin
collect2: ld output status returned with 1
gcc tells us that there are many unidentified objects. In fact, these are not errors related to the code we wrote, but some linking libraries were not found. We know that these functions are available in the ncurses library. All we have to do is introduce the ncurses library to gcc, that is, add the relevant option (-l) to the command line.
# gcc ilk.c -o first -lncurses
After this command, the application will compile without error. Important reminder: If the libraries to be added to the application are in the standard places in the system, this usage is sufficient. Follow me podcast. But where are these standard places? This definition may differ depending on distributions, but header files for all libraries are generally included in / usr / include. If these header files are in a different location, a different option (-I) should be used to indicate their location:
# gcc first.c -o first -I / usr / local / include -lncurses
This line will compile our application without any problem. Here, we have added / usr / local / include directory to search the header files added in our application. From this stage on, we can add the ncurses library into our application and use the ncurses commands we want. In addition, if we want to implement the mysql library, we can put our application as follows:
#include <ncurses.h>
#include <mysql.h>
int main ()
{
initscr ();
getch ();
endwin the ();
printf ( "done \ n");
}