Announcement

Collapse
No announcement yet.

How to run C++ Programs

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to run C++ Programs

    Well, I'm almost entirely new to linux and I've only picked up programming in C++ in the past few weeks. I have run Windows XP for most of the time I have owned a computer (with only a brief break to try out Solaris), so the terminal prompt is running circles around my head compared to the MS-DOS prompt in Windows.

    Primarily, I would like to know how to run programs written in C++ on the linux operating system. I think I have the compiler right. I installed the g++ package that was included with Kubuntu (following the directions given to me by the terminal).

    If there is any advice or wisdom or good tutorials on how to use the terminal, I would gladly appreciate that as well.

    Thanks,

    Lighbulbjb

    #2
    Re: How to run C++ Programs

    http://gcc.gnu.org/

    Comment


      #3
      Re: How to run C++ Programs

      1. Install the build-essential package.
      2. Compile the C++ source code:
      Code:
      g++ filename.cpp -o foo
      Replace "foo" with the name you want to give the program. If you leave out the "-o foo" part of the command, the name of the program always defaults to "a.out".

      3. If it compiles successfully, type
      Code:
      ./foo
      To run the program. Replace foo with the name of your program.

      That would be the basics of how to compile a C++ program and run it. You might run into some problems, depending on how complex the program is (you might to do some linking, etc.).
      Jucato's Data Core

      Comment


        #4
        Re: How to run C++ Programs

        Thanks.

        What does the ./ mean?

        Comment


          #5
          Re: How to run C++ Programs

          ./ means "current directory", so "./foo" means that the file "foo" is in the current directory.

          The reason that you have to put ./ before running an executable file that is in the current directory is because of how Linux searches for executable files. When you type in a name of a program, for example, "cat", Linux searches for that file in certain directories. The list of directories which Linux searches is defined in a variable called PATH. For security purposes, the PATH does not include whatever current directory you are in.

          So, for example, if the program foo is in the current directory, but you just type in plain "foo", Linux will try to search for the program in the list of directories defined in PATH. But since foo isn't in any of those directories (presuming it isn't), it will say that the command isn't found. You have explicitly tell Linux to use the foo file in the current directory.

          Of course if that program is installed in the directories included in PATH, and you are in that directory, you don't need to use ./

          To see the default list of directories included in the PATH, type
          Code:
          echo $PATH
          Hope that helps.
          Jucato's Data Core

          Comment


            #6
            Re: How to run C++ Programs

            Thanks. That helps a lot.

            Comment

            Working...
            X