Page 1 of 5 123 ... LastLast
Results 1 to 10 of 43

Thread: FAQ: Compiling your first C or C++ programs

  1. #1
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Exclamation FAQ: Compiling your first C or C++ programs

    So you're willing to write your own C or C++ programs, or simply compile someone else's code? This guide will get you started...

    This FAQ assumes you're trying to compile error-free code. To actually learn the C or C++ languages, see the How to start programming thread.
    If you encounter compilation problems, you may want to check this thread: Troubleshooting common C and C++ compilation errors.




    0. Make sure the build-essential package is installed

    This package installs the essential tools needed to build C and C++ programs. Don't try to install individual tools by yourself, experts made that package for a good reason...
    Code:
    sudo apt-get install build-essential



    1.a Compiling your first C program

    Copy & paste this code into a new file. Save the file as main.c
    Code:
    #include <stdio.h>
    
    int main()
    {
      printf("Hello World!\n");
      return 0;
    }
    Open a terminal, go to the directory where you saved main.c, and type:
    Code:
    gcc main.c -o test
    If all went fine, nothing is printed and you get back to the shell. Now, run it:
    Code:
    ./test
    "Hello World!" gets printed in the terminal.

    Explanations:
    • gcc is the C compiler
    • obviously, main.c means that the compiler must compile this file. If you want to compile several source files into a single executable, just list them all here.
    • -o test means "output the result of the compilation to an executable file named test". If this part is omitted, the executable will be named a.out by default.
    • ./test runs the newly compiled executable. The ./ is needed because of the way the shell works (the current directory . is not in the PATH by default).




    1.b Compiling your first C++ program

    Most of what I just said regarding the compilation of C programs also apply to C++ programs.
    The two main differences (besides the language itself) are:
    • g++ is the compiler that you must use.
    • you should use a .cpp file extension rather than a .c one

    Code:
    #include <iostream>
    
    int main()
    {
      std::cout << "Hello World!" << std::endl;
      return 0;
    }
    Code:
    g++ main.cpp -o test
    ./test



    2. Paranoid programming is good for your health!

    Many beginners want to avoid compiler errors and warnings at any cost, which often leads them to ignore the "cryptic" messages the compiler outputs and to tinker anxiously with the relevant line until the error goes away.
    Don't fall in that trap! The compiler is your friend, the messages it outputs are kind advices rather than punishments. You should really take the time to try and understand those messages, this is a very good way to improve your code's quality.

    As a matter of fact, most seasoned programmers set the compiler's warning level as high as possible, so that it catches the little errors they make when they aren't paying much attention.

    Here are a few command line switches to enable some warnings and language features (check the manual for detailed information):
    • -std=gnu99 (C only) enforces the latest C standard (1999), plus GNU extensions (must be explicitely specified)
    • -std=gnu++98 (C++ only) enforces the 1998 C++ standard (but the STL now supports most of C++0x TR1 anyway), plus GNU extensions (this is the default for C++ code)
    • -Wall enables most warnings (very useful)
    • -Wextra enables more warnings (very useful)
    • -Wwrite-strings warns when you misuse plain old C string constants (aka. deprecated cast from const char* to char*) (very useful) (see this discussion for more in-depth information)
    • -pedantic issues warnings when strict ISO compatibility is not met (the name says it all: pedantic, but it can be useful once in a while)
    • -Woverloaded-virtual (C++ only) warns when virtual functions are overloaded in derived classes (very useful)
    • -Weffc++ (C++ only) warns when some key point of Scott Meyer's Efficient C++ and More Efficient C++ books are not met (good practice)
    • -Wold-style-cast (C++ only) warns when you use old-style C casts in a C++ program (good practice, you should be using the new-style C++ casts)




    3. Useful tips
    • the -g flag instructs the compiler to include debugging information, so that you can debug your program with gdb itself or any gdb frontend.
    • sudo apt-get install manpages-dev to help you make sense of compiler warnings and errors.




    4. Read the manual!

    Plain old manpage:
    Code:
    man gcc
    HTML version of the gcc manual:
    Code:
    sudo apt-get install gcc-doc
    Once installed, point your browser to /usr/share/doc/gcc-doc/gcc.html






    Thanks to so many people @UF for their very useful help, this FAQ wouldn't be the same without them (you know who you are ).
    Last edited by aks44; April 6th, 2008 at 07:48 PM.
    Not even tinfoil can save us now...

  2. #2
    Join Date
    Jan 2006
    Beans
    961

    Re: FAQ: Compiling your first C and C++ programs & Troubleshooting the most common er

    suggestion: people should use -Wall and -g when compiling

    * -Wall makes the compiler talk more and gives hints about potential problems in your code
    * -g makes the resulting binaries usable in a debugger

  3. #3
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: FAQ: Compiling your first C and C++ programs & Troubleshooting the most common er

    lnostdal you're perfectly right. There are also other useful switches, like (from the top of my head, so it may not be accurate) --std=C99, --pedantic-warnings, --Wold-style-casts etc.

    I'll research them & update my post tomorrow. For now it's almost time I go to sleep, so I'm done with technical stuff...


    Keep the suggestions coming!
    Not even tinfoil can save us now...

  4. #4
    Join Date
    Apr 2007
    Beans
    14,781

    Re: FAQ: Compiling your first C and C++ programs & Troubleshooting the most common er

    http://ubuntuprogramming.wikidot.com/cpp

    Or is the wiki no longer of interest? I thought it would be better suited for things such as this.

  5. #5
    Join Date
    Apr 2007
    Beans
    2,042

    Re: FAQ: Compiling your first C and C++ programs & Troubleshooting the most common er

    I find -Wextra useful when I am programming, it makes the code even more strict.

  6. #6
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: FAQ: Compiling your first C and C++ programs & Troubleshooting the most common er

    Quote Originally Posted by LaRoza View Post
    is the wiki no longer of interest? I thought it would be better suited for things such as this.
    IMO this post has its place on the forum. I guess you'll agree that the questions addressed are *very* common (if not overwhelming). I'm pretty sure I answer one of those questions almost every day.


    My reasoning is, despite the proven fact that most people don't read the stickies or search the forum, having this information on the forum itself will hopefully help the few people who actually use the search function.


    Besides that, I think the wiki deserves more in-depth articles. IMHO this post addresses very common issues in a very superficial way (quick and dirty Q/A). As I stated in another thread, writing a comprehensive C++ resource (and C to some extent) is on my agenda, and it will (amongst other things) quite probably cover those topics way more in-depth, including the whys that are behind the hows I just posted. Using the wiki will then make full sense to me.
    Last edited by aks44; February 7th, 2008 at 12:21 AM.
    Not even tinfoil can save us now...

  7. #7
    Join Date
    Apr 2007
    Beans
    14,781

    Re: FAQ: Compiling your first C and C++ programs & Troubleshooting the most common er

    Ok.

    May I recommend having a more uniform style? Blue is being used by others, and it is easier to read and stands out just as well.

  8. #8
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: FAQ: Compiling your first C and C++ programs & Troubleshooting the most common er

    We need FAQ about how to write FAQs

  9. #9
    Join Date
    May 2007
    Location
    Paris, France
    Beans
    927
    Distro
    Kubuntu 7.04 Feisty Fawn

    Re: FAQ: Compiling your first C and C++ programs & Troubleshooting the most common er

    Quote Originally Posted by LaRoza View Post
    May I recommend having a more uniform style? Blue is being used by others, and it is easier to read and stands out just as well.
    That I can do... /"Red"/"Blue"/g

    (sorry, I tend to associate errors with the red colour )
    Not even tinfoil can save us now...

  10. #10
    Join Date
    Dec 2006
    Location
    Glasgow, Scotland
    Beans
    470
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: FAQ: Compiling your first C and C++ programs & Troubleshooting the most common er

    Code:
    sudo apt-get install manpages-dev
    For further reading, and for when "-Wall" starts bleating.

Page 1 of 5 123 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

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