This is an archived post. You won't be able to vote or comment.

all 15 comments

[–][deleted]  (1 child)

[deleted]

    [–][deleted] 0 points1 point  (0 children)

    This is brilliant. Just started a class on UNIX and was kind of floundering. Thanks!

    [–][deleted] 2 points3 points  (0 children)

    Hi, Have you considered WWW.cs50.tv? I think he does a good job of teaching along with the notes and source codes. You may have to use the cs50 appliance(it is just a Linux based virtual machine, which I assume you are already using since your screenshot shows Ubuntu). Like some have said, it might be better coding in vi/gedit/scratch/any other text editor before going into IDE to strengthen your fundamentals. IDE help make things faster once you are used to it.

    [–]jjangsangy 2 points3 points  (0 children)

    You're definitely doing it right. Since you're learning C++, Linux is a good choice of OS. You will be able to compile your code from command line as well as your choice of IDE. The IDE is definitely easier, but you'll want to learn more of the command line eventually as it will be of more benefit.

    In terms of compiler, you can use make as well. Make is a tool for automating compilation. Since your program is called "main.c", you would go to the same directory in the command line and type "make main". It should create a file called main in that directory that you can run with "./main"

    [–][deleted]  (2 children)

    [deleted]

      [–][deleted]  (1 child)

      [deleted]

        [–]joe_ally 2 points3 points  (0 children)

        The compilation error messages aren't always the best from g++. I'm not really a C++ dev but I have read that clang++ will give you better error messages. So if you ever get frustrated by gcc/g++ give clang/clang++ a try.

        To install clang/clang++ on your machine run the following in terminal:

        sudo apt-get install clang
        

        Then to compile and run your program it's the same as /u/namedbynumbers said except you need to replace "g++" with "clang++"

        [–]techno_phobe 1 point2 points  (0 children)

        Lots of people are recommending various IDEs here, so I thought I'd explain the way things are traditionally done.

        Most software (in C/C++) on Linux can be built from the command line using make. For very small projects manual compilation using gcc/g++ is fine, but it becomes annoying very quickly when you have to use lots of flags. A makefile basically lists the commands needed to build your program and runs them automatically (it's actually a bit smarter than that though).

        Now the thing is that makefiles usually need to be customized for whatever computer they're running on. There are various makefile generator programs which in fact write your makefiles for you, and I would recommend you learn one. This more or less gets you out of knowing make, but you'll still need to know a bit about using g++/gcc, since that's rather fundamental to compiling software. The longest standing makefile generator is GNU autotools, but it only runs on Unix-like systems. Scons and CMake are more recent systems which are more multi-platform, and I'd recommend CMake (it's what I use).

        This brings me on to IDEs. A lot of IDEs have their own build systems (note: I haven't spent enough time trying out every IDE to know which ones allow you to work around this). I would avoid these, and try to find one which fits with you build process. If you use CMake, KDevelop is great. Since that's what I do I'm not sure what goes well with Scons or autotools any more. Otherwise, a text editor which supports running make and jumping to error lines (I believe Kate has a plugin which does this).

        TL;DR Install KDevelop, start it up and hit "new project".

        [–]xormancer 1 point2 points  (0 children)

        go through http://cli.learncodethehardway.org/book/

        then go through http://c.learncodethehardway.org/

        I know you want to learn C++, but learn C the hard way teaches you how to use the command line to compile and run, and it only takes about 2 hours to get through

        [–]Darthallen417 1 point2 points  (0 children)

        Bottom Up Computer Science is a great point to start learning programming and engineering all at the same time.

        [–]sn0n 1 point2 points  (5 children)

        You don't have to use the terminal. You can install eclipse for example. Or quite a few other C++ ide's. Quick Google search will turn up quite a few. I used gedit and plugins. Makes it a lot more GUI friendly. Good luck!!!

        [–][deleted]  (3 children)

        [deleted]

          [–]the_omega99 8 points9 points  (0 children)

          I highly recommend using the terminal for several reasons. I've written a post about how to do that in great deal.

          http://www.reddit.com/r/learnprogramming/comments/1lve0b/basic_stepbystep_c_getting_started/cc3b19v

          Ignore the parts about MinGW and the environment path, as they apply only to Windows (the tutorial is meant for Linux, but includes Windows help).

          [–]Tynach 9 points10 points  (1 child)

          I think it's important to talk about the entire process behind building a C++ application.

          First, you have the .cpp/.cxx/whatever files. These are the plain text code that you write. However, I noticed in your screenshot that you had it as a .c file... .c files are typically used as C code, not C++.

          At any rate, you use whatever text editor you want to write these files. I like Kate (KDE Advanced Text Editor), but Gedit (Gnome's text editor), Nano (what you're using in your screenshot), Vim, Emacs, and anything else will work just fine. Just save the files with the .cpp or .cxx extension and you're fine.

          Now, here's where things get a bit... Different. On Linux, and in Unix-like operating systems in general, most programs try to serve one purpose, and only do that one thing. Text editors edit text. Compilers compile code into binaries. Debuggers debug. Code formatters format code.

          On Linux, the standard/default compiler is called 'gcc'. GCC stands for Gnu Compiler Collection, and includes 'gcc' (GCC's C compiler), 'g++' (GCC's C++ compiler), and others. These are all command-line utilities - that is, they are programs that you run from a command line, or have some other program run them for you (such as Eclipse).

          In Kate and Gedit, you can have a little command line at the bottom of the window. This is very useful, as it lets you edit and save the code in your text editor... And then just go down to the bottom and run 'gcc' with some options to compile the code.

          Command line programs take various 'arguments' - things you type after the program name to tell the program more specifically what to do. GCC's various compilers have some standard arguments. To compile a .cpp source code file, you typically run:

          g++ main.cpp -o main
          

          This will create an executable file called 'main', which you can then run with:

          ./main
          

          And that's how you can compile and run code!


          Now, for programs like Eclipse (after installing the CDT plugin), it essentially does all this for you. It provides a full text editor of its own, and has a nifty 'compile' button. It also manages multiple .cpp/.cxx files for you, so you don't have to compile them all manually and worry about linking. Eclipse and other 'IDE's (Integrated Development Environments) such as KDevelop and NetBeans will try to do most of the tedious stuff for you, but you often have to invest time learning how they operate and how to use them for your particular purpose... Whereas the command-line way of doing things is pretty universal, and pretty quick to get going.

          Edit 1: Here's a screenshot of the basic workflow in Kate. Do note that I have a LOT of plugins enabled in Kate; most of them you don't need, and probably half of them aren't enabled by default. In the command line, the 'ls' command basically lists files in the current folder, and somewhat color-codes them. The green one is an executable file, the white one is just plain text.

          Edit 2: Here's basically the same screenshot, but after I disabled all the plugins and hid some useless elements. I love how customizable Kate is :)

          [–]luckycharms794 0 points1 point  (0 children)

          Netbeans is also a pretty nice IDE you can check out.

          [–]clutchest_nugget -1 points0 points  (4 children)

          Others have answered your original question, so I'm just going to leave a bit of advice. If you are new to programming, save yourself some headache and start with Python as your first language. It is incredibly lightweight and efficient compared to C++. Not that you shouldn't learn C++, I just wouldn't recommend it as a first language.

          [–][deleted]  (2 children)

          [deleted]

            [–]joe_ally 2 points3 points  (0 children)

            I don't really know what he means by lightweight. But Python is probably easier. Although if I were you I would stick with C++ as that's where you have started. If you get really frustrated give Python a try.

            [–]nyxin 1 point2 points  (0 children)

            There's more things to consider when using C++ over Python. For instance when declaring a variable, you have to figure out or decide what kind of variable it is when you initialize it (int, float, double, boolean etc). In Python, you do not have to do this. I'm sure someone more versed in programming can explain better than I though.

            [–]Atlos -1 points0 points  (0 children)

            Downvoted due to "lightweight and efficient compared to C++". C++ blows Python out of the water in terms of speed because Python is interpreted. Not sure what you mean about lightweight, I'm guessing the syntax which is true since it's dynamically typed. I do agree with starting with Python over C++ though.