Does somebody know where can I find gcc and gdb tutorials for beginners - Page 2
FedoraForum.org - Fedora Support Forums and Community
Page 2 of 3 FirstFirst 1 2 3 LastLast
Results 16 to 30 of 32
  1. #16
    Join Date
    Feb 2005
    Location
    London, UK
    Posts
    688

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    Quote Originally Posted by ocratato
    I left the second link off my list as it failed to mention the back trace command, which is the one I use about 99% of the time.
    I use "break if var!=val" or similar quite a lot. You know those annoying situations where your program will run green for a while and then randomly go red for no apparent reason, being able to pause the program at the moment it flicks to red ready for investigation is really useful.

  2. #17
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,295

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    I always use the following two macros as the entry into my source or header file
    #ifndef debug
    #define debug(M, ...)
    #endif
    #ifndef debug1
    #define debug1(M, ...) fprintf(stderr, "DEBUG %s %s %d: " M "\n",__FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__ );
    #endif


    In the code, I when I want to see the debug contents, I just replace debug(...) by debug1(...)
    sample below

    Code:
    char *get_labelVal(dictionary *ini, const char *inkey)
    {
         char label[64];
         *label=LABELSTMT;       
         debug("inkey=%s",inkey);                  //set debug to debug1. to display the line of text 
         strncpy(label+1,inkey,sizeof(label)-1);
         return (char *)dictionary_get(ini,label,twelvestars);
    }
    If you use debug(...); nothing gets compiled. If you change debug to debug1, the contents of debug1( ) get compiled.
    During development, I lace the code at critical points (not inside loops), with debug( .... )
    Turning on debug1() at critical points is better than using a global debug macro for the entire source.
    I used to have multiple macros, but found I didn't need them if I just used the above two.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  3. #18
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,971

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    I think we are drifting a bit too far off the topic of tutorials for gcc and gdb.

    Perhaps a new thread on debugging techniques might be useful.

  4. #19
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,295

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    Hi Tangent144.

    gdb is a great tool, and you have the tutorial references given that are great.

    But sometimes, you want to examine a program that was compiled using maximum optimization. An optimizing compiler may not work well with gdb. gdb is going to help tell you where your program failed, but what if the problem failure occurs some minutes after handling lots of data, such as after processing a thousand or more transactions/records.

    Its good to have an accompaniment to gdb, From my "developer's toolbox" I presented (response #17) the two macros that I use as a gdb alternative or gdb accompaniment. I would also add to your collection, the libc.pdf manual available from gnu.org
    Last edited by lsatenstein; 20th January 2018 at 11:55 PM.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  5. #20
    Join Date
    Jun 2017
    Location
    Brazil
    Posts
    31

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    I am an absolute beginner and I am reading Stephen Prata's "C Primer Plus" (6th edition) to get a basic comprehension of C. However, the book has a program doesn't compile and I would like to debug it; but I don't understand gdb and the tutorials. But I found a book about gdb in Amazon. It's a two volume set, and it's compatible with gdb 7.x. So, I think to buy it anyway.

  6. #21
    Join Date
    Feb 2005
    Location
    London, UK
    Posts
    688

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    Quote Originally Posted by tangent144
    However, the book has a program doesn't compile and I would like to debug it; but I don't understand gdb and the tutorials.
    If the program won't compile the compiler (gcc) should tell you why, for example "this function expects an integer but you've passed a string to it". GDB is used for the next step of debugging, i.e: "the program compiles & it runs, but it's giving me some weird results, lets dig in to it and see what's going on". I usually use Valgrind as a next/other step for issues like "it compiles, it runs and it's consistently giving me the expected results... but it's memory use is growing over time. Let's see where".

    What error is gcc giving you?

  7. #22
    Join Date
    Jun 2017
    Location
    Brazil
    Posts
    31

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    menuette.c: In function 'main':
    menuette.c:29:8: error: static declaration of 'count' follows non-static declaration
    void count(void)
    menuette.c:10:8: note: previous declaration of 'count' was here
    void count(void);
    menuette.c:79:1: error: expected delcaration or statement at end of input
    }
    At top level:
    menuette.c:65:6: error: 'get_int' defined but not used [-Werror=unused-function]
    int get_int(void)
    menuette.c:55:7: error: 'get_first' defined but not used [-Werror=unused-function]
    char get_first(void)
    menuette.c:40:7: error "get_choice' defined but not used [-Werror=unused-function]
    char get_choice(void)
    menuette.c:29:8: error: 'count' defined but not used [-Werror=unused-function]
    void count(void)
    cc1: all warningd being treated as errors

  8. #23
    Join Date
    Feb 2005
    Location
    London, UK
    Posts
    688

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    Quote Originally Posted by tangent144
    menuette.c: In function 'main':
    menuette.c:29:8: error: static declaration of 'count' follows non-static declaration
    void count(void)
    menuette.c:10:8: note: previous declaration of 'count' was here
    void count(void);
    menuette.c:79:1: error: expected delcaration or statement at end of input
    }
    I think you might have copied the code from the book wrong, those blue lines look fine apart from the fact they're "IN FUNCTION MAIN" see red line, i.e: you're creating a function within the main function. They should be on their own, compare this code which compiles fine:

    Code:
    haydn@darkstar tmp]$ cat test.c 
    #include <stdio.h>
    
    void count(void);
    
    void count(void) {
      fprintf(stderr, "Count\n");
    }
    
    void main() {
      count();
      fprintf(stderr, "Done\n");
    }
    [haydn@darkstar tmp]$ gcc -o foo test.c 
    [haydn@darkstar tmp]$

    With this code which doesn't compile with the same error you've given and the in function main line. The only difference is I've moved the "void main() {" line up to above the other function definitions:

    Code:
    [haydn@darkstar tmp]$ cat testB.c 
    #include <stdio.h>
    
    void main() {
      void count(void);
    
      void count(void) {
        fprintf(stderr, "Count\n");
      }
    
      count();
      fprintf(stderr, "Done\n");
    }
    [haydn@darkstar tmp]$ gcc -o foo testB.c 
    testB.c: In function ‘main’:
    testB.c:6:8: error: static declaration of ‘count’ follows non-static declaration
       void count(void) {
            ^~~~~
    testB.c:4:8: note: previous declaration of ‘count’ was here
       void count(void);
            ^~~~~
    [haydn@darkstar tmp]$

  9. #24
    Join Date
    Jun 2017
    Location
    Brazil
    Posts
    31

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    I fixed some problems, but the program doesn't compile yet.
    The error message is:
    menuette.c: In functiob 'main':
    menuette.c:79: error expected declaration or statement at the end of input
    }

  10. #25
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,971

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    Its quite difficult to determine what the error might be without being able to see the code.
    If you paste the code into your post we might be able to guide you.
    (Don't forget to put "CODE" tags around your code to preserve the indenting etc. - use the # symbol on the 'Advanced" dialog)

  11. #26
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,295

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    You cannot nest function definitions. Put your count function outside of main.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  12. #27
    Join Date
    Feb 2005
    Location
    London, UK
    Posts
    688

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    Quote Originally Posted by tangent144
    I fixed some problems, but the program doesn't compile yet.
    The error message is:
    menuette.c: In functiob 'main':
    menuette.c:79: error expected declaration or statement at the end of input
    }
    This will be due to a missing "}", if you've indented the code nicely it should be fairly easy to spot, if you haven't I would recommend indenting the code before trying to find the missing } as you'll probably find it while doing so anyway. Using the code above again as an example:

    Code:
    [haydn@darkstar tmp]$ cat test.c 
    #include <stdio.h>
    
    void count(void);
    
    void count(void) {
      fprintf(stderr, "Count\n");
    // }
    // The line above is now a comment so is "missing"
    
    void main() {
      count();
      fprintf(stderr, "Done\n");
    }
    [haydn@darkstar tmp]$ gcc -o foo test.c 
    test.c: In function ‘count’:
    test.c:12:1: error: expected declaration or statement at end of input
     }
     ^
    [haydn@darkstar tmp]$

    Quote Originally Posted by ocratato
    Its quite difficult to determine what the error might be without being able to see the code.
    Oh I don't know, I'm kind of enjoying it, it's like a coding crossword or a scripting soduko...

  13. #28
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,295

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    #include <stdio.h>

    void count(void);

    void count(void) {
    fprintf(stderr, "Count\n");
    }

    void main() {
    count();
    fprintf(stderr, "Done\n");
    }

    Look at the above that should compile easily. I get Count on one line and Done on the other.

    Here is the explanation

    After the stdio.h include, there is a prototype
    void count(void);

    This prototype is telling the compiler that if you use the count function,
    it is expecting no input data and will produce no valid return

    The count function prints a line of text to the terminal. C has two output possibilities, the regular output (stdout)
    and the error output (stderr). Later on in your learning you will discover how to direct outputs to files.

    Main is the main program. The C compiler knows about main and the compiler is told, here is where the program starts.
    You can only have one main function in a program.

    You did not create a prototype statement for main, but as I stated, the compiler knows about main (from the stdio.h input file). If you use main differently than what the compiler knows, you may get an error message or a warning.

    Since your main is not like the one it knows, it produces a warning message

    I copied your program and named it x.c when I compile as gcc x.c
    I will get a file a.out that is the executable.

    If I compile, telling the compiler to do maximum optimization and produce all potential warnings and errors, I would enter
    gcc -O3 -Wall -Wextra x.c


    x.c:9:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    void main() {

    Study this response and ask your questions. We will do our best for you. To run the program after it is compiled as above, type ./a.out
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  14. #29
    Join Date
    Feb 2005
    Location
    London, UK
    Posts
    688

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    Quote Originally Posted by lsatenstein
    Here is the explanation
    Hi Leslie, you realise the code above was something I stuck together to generate the error questioned by OP? Not the actual code that is causing OP issues?

  15. #30
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    5,295

    Re: Does somebody know where can I find gcc and gdb tutorials for beginners

    Quote Originally Posted by HaydnH
    Hi Leslie, you realise the code above was something I stuck together to generate the error questioned by OP? Not the actual code that is causing OP issues?
    Haydn, When I check for unread Posts, I switch to the forum topic, and then to the first unread. That let me to see the sample you prepared.

    I thought, the "student" needs an explanation for a simple "hello world" program. I chose to give him one, with some explanations.
    I also provided two macros that he could use to do debugging, and libc.pdf (all the function calls available).
    Now it is up to the individual to move forward.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

Page 2 of 3 FirstFirst 1 2 3 LastLast

Similar Threads

  1. Best books for beginners
    By EuGenie in forum Using Fedora
    Replies: 7
    Last Post: 8th July 2013, 12:49 PM
  2. Replies: 1
    Last Post: 28th March 2009, 02:59 AM
  3. Replies: 0
    Last Post: 28th March 2009, 02:37 AM
  4. beginners C++ compiler
    By fengshaun in forum Programming & Packaging
    Replies: 16
    Last Post: 29th December 2007, 07:31 PM
  5. Beginners help
    By 357mag in forum Using Fedora
    Replies: 3
    Last Post: 9th February 2005, 09:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •