about summary refs log blame commit diff stats
path: root/dev/c/index.html
blob: 3e8651f9ad3b7add24cb4838a1105fe2f8ed5dcd (plain) (tree)
1
2
3
4
5
6
7
8
9


                          

                                  

           
                                                     
 
                            
 
                                       
 
                                        
 

                                
 




                                   
 
                       
 


                                    
 
                   
 



                    
 
                                            
 


                                                          
 

                          
 




                           
 

                                                                  
 


                                     
 
                                                           
 


                                
 























                                                                   
 















































































                                                                      

             


                             

              
                                        

             


                           
              
 

                                                                           
 






                                                                                               

           
<!DOCTYPE html>
<html dir="ltr" lang="en">
    <head>
	<meta charset='utf-8'>
	<title>C &amp; GDB</title>
    </head>
    <body>
	<a href="../index.html">Development Index</a>

	<h1>C &amp; GDB</h1>

	<h2 id="hello">Hello World</h2>

	<p>Create file hello.c with;</p>

	<pre>
	#include &lt;stdio.h&gt;

	int main() {
	    printf("Hello World!");
	    return 0;
	}
	</pre>

	<p>Compile;</p>

	<pre>
	$ gcc -Wall hello.c -o hello
	</pre>

	<p>Run;</p>

	<pre>
	$./hello
	Hello World!
	</pre>

	<h2 ="sources">Multiple Sources</h2>

	<p>To organize code in multiple files split above
	example in main.c, hello.c and hello.h. Content of
	main.c;<p>

	<pre>
	#include "hello.h"

	int main() {
	    hello("world");
	    return 0;
	}
	</pre>

	<p>Header file contains declaration of the function hello,
	content of hello.h;</p>

	<pre>
	void hello(const char* name);
	</pre>

	<p>Implementation of hello function in hello.c;</p>

	<pre>
	#include &lt;stdio.h&gt;
	#include "hello.h"

	void hello(const char* name) {
	    printf("Hello, %s!\n", name);
	}
	</pre>

	<p>Compile;</p>

	<pre>
	$ gcc -Wall main.c hello.c -o hello
	</pre>

	<h2 id="makefile">Makefile</h2>

	<p>Make reads a Makefile by default on current directory,
	Makefile defines targets, for example executables and their
	dependencies, for example object files and source files.<p>

	<p>Create Makefile;</p>

	<pre>
	CC=gcc
	CFLAGS=-Wall

	hello: main.o hello.o

	clean:
		rm -f hello main.o hello.o
	</pre>

	<pre>
	$ touch NEWS README AUTHORS ChangeLog
	</pre>

	<h2 id="debug">Debug</h2>

	<p>To use gdb you need to compile program with -g flag. Change
	Makefile</p>

	<pre>
	CC=gcc
	CFLAGS=-Wall -g

	hello: main.o hello.o

	clean:
		rm -f hello main.o hello.o
	</pre>

	<pre>
	$ gdb hello
	</pre>

	<p>Set break point;</p>

	<pre>
	(gdb) break main
	</pre>

	<p>To start the program you can type run, this way gdb
	will try to run the program until the end. If program
	crash, gdb will stop it for debuging. Start program;</p>

	<pre>
	(gdb) run
	</pre>

	<p>Step in next line;</p>

	<pre>
	(gdb) s
	</pre>

	<p>Execute next line;</p>

	<pre>
	(gdb) n
	</pre>

        <p>To start gdb in TUI mode press;</p>

        <dl>
            <dt>Ctrl-x A</dt>
            <dd>Enter or leave TUI.</dd>
            <dt>Ctrl-x 1</dt>
            <dd>TUI with only one window.</dd>
            <dt>Ctrl-x 2</dt>
            <dd>TUI with more than two windows.</dd>
            <dt>Ctrl-x o</dt>
            <dd>Change active window.</dd>
            <dt>Ctrl-x s</dt>
            <dd>TUI single key mode.</dd>
            <dt>Ctrl-L s</dt>
            <dd>Refresh screen.</dd>
            <dt>Up</dt>
            <dd>Scroll</dd>
            <dt>Down</dt>
            <dd>Scroll</dd>
            <dt>Left</dt>
            <dd>Scroll</dd>
            <dt>Right</dt>
            <dd>Scroll</dd>
        </dl>

        <p>Run again and step in hello function, to print
        variable "name" value and type;</p>

        <pre>
        (gdb) print name
        $1 = 0x4005b0 "world"
        (gdb)
        </pre>

        <p>Print variable name type;</p>

        <pre>
        (gdb) ptype name
        type = const char *
        (gdb)
        </pre>

        <p>Variable is a <a href="elements.html#const">string constant</a>,
        continue reading <a href="elements.html">Elements</a>.</p>

	<a href="../index.html">Development Index</a>
	<p>
	This is part of the c9-doc Manual.
	Copyright (C) 2016
	c9 team.
	See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
	for copying conditions.</p>
    </body>
</html>