Find the answer to your Linux question:
Results 1 to 8 of 8
Hi, i have fedora core 4, and starting to learn C++. To program in perl on linux, you would ----> " perl filename.pl " How do you program in C++ ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2006
    Posts
    18

    how to program in C++ in linux


    Hi, i have fedora core 4, and starting to learn C++.
    To program in perl on linux, you would ----> " perl filename.pl "
    How do you program in C++ on linux?

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Righto. Let's say that you have your source saved in hello.cpp.

    Code:
    g++ -Wall -o hello hello.cpp
    Allow me to explain:

    g++ - Invoke the GNU C++ Compiler
    -Wall - Enable all warnings
    -o hello - Name the outputted binary "hello" (if -o is not given, the binary is called a.out)
    hello.cpp - The file to compile

    So in this case, we have called the binary "hello", and so can execute the program via:
    Code:
    ./hello
    assuming that you are currently in the same directory as the binary.


    Hope that helps!


    EDIT:

    Let it be noted that there is a difference between how Perl and C++ run. Perl, you write your source and just run it: it is compiled and executed automatically. In C++, the source and program are different files: you must manually compile the program (using g++) before you can run it.

  3. #3
    Just Joined!
    Join Date
    Feb 2006
    Posts
    18
    niice

  4. #4
    Just Joined!
    Join Date
    Mar 2006
    Posts
    34
    If you want to write programs with multiple source files, or programs with complex compile options, or programs you plan on distributing in source form, it's a good idea to use make.

  5. #5
    Just Joined!
    Join Date
    Feb 2006
    Posts
    18
    thanx for that, now after the compile, how do you run the program?
    i tried " ./hello.cpp "
    ************************************************** *******************
    /home/max/hello.cpp: line 3: using: command not found
    /home/max/hello.cpp: line 5: syntax error near unexpected token `('
    /home/max/hello.cpp: line 5; `int main(void)`
    ************************************************** ************************************************** *******

  6. #6
    Linux Guru AlexK's Avatar
    Join Date
    Feb 2005
    Location
    Earth
    Posts
    3,379
    what you did with that last command was to try and run the file which contains the source code and not the executable.

    I suggest you read through Cabhan's post again.

    hint:: note how he runs the program with ./hello i.e. the executable and not the source code.
    Life is complex, it has a real part and an imaginary part.

  7. #7
    Just Joined!
    Join Date
    Sep 2005
    Location
    Nowhere and Everywhere
    Posts
    98
    When you compile a program, do you need to be in root??

  8. #8
    Linux Engineer Javasnob's Avatar
    Join Date
    Jul 2005
    Location
    Wisconsin
    Posts
    942
    Quote Originally Posted by southpaw
    When you compile a program, do you need to be in root??
    No, you do not.
    Flies of a particular kind, i.e. time-flies, are fond of an arrow.

    Registered Linux User #408794

Posting Permissions

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