g++ compiled/linked program doesnt work

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • deanwarren
    New Member
    • Mar 2008
    • 2

    #1

    g++ compiled/linked program doesnt work

    Hi there

    I am having problems getting a successfully compiled/linked g++ program to run under Ubuntu linux.

    A simple hello world program :
    #include <iostream>

    int main
    (int argc,
    char* argv[])
    {
    std::cout << "hello world\n";

    return 0;
    }

    saved as helloworld.cpp

    I execute the following command (from the same directory as the cpp file) with no errors or warnings displayed:

    g++ helloworld.cpp -o t1

    t1 is produced in the same directory.

    However, I try to run it (just by typing t1 followed by carriage-return but it fails with:

    bash: t1: command not found


    Any ideas?
  • fual
    New Member
    • Feb 2008
    • 28

    #2
    Originally posted by deanwarren
    Hi there

    I am having problems getting a successfully compiled/linked g++ program to run under Ubuntu linux.

    A simple hello world program :
    #include <iostream>

    int main
    (int argc,
    char* argv[])
    {
    std::cout << "hello world\n";

    return 0;
    }

    saved as helloworld.cpp

    I execute the following command (from the same directory as the cpp file) with no errors or warnings displayed:

    g++ helloworld.cpp -o t1

    t1 is produced in the same directory.

    However, I try to run it (just by typing t1 followed by carriage-return but it fails with:

    bash: t1: command not found


    Any ideas?
    Have you tried
    Code:
    ./t1
    ? I think linux requires you to tell it to look in the actual directory, otherwise it assume the thing is a bash command

    Comment

    • Laharl
      Recognized Expert Contributor
      • Sep 2007
      • 849

      #3
      Yes, fual is correct. Use ./ to run your compiled programs so that the shell knows the location (the current directory) of the program it's supposed to run.

      Comment

      • sanctus
        New Member
        • Mar 2007
        • 84

        #4
        lol! I had the same problem a year ago, when I started on linux. It will work np with the comand ./

        Comment

        • deanwarren
          New Member
          • Mar 2008
          • 2

          #5
          lol - thanks guys. learning something new is tedious! :s

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by deanwarren
            lol - thanks guys. learning something new is tedious! :s
            You could append the '.' (dot) to your path so you don't have to do the ./ trick
            anymore. If you do that, always put it at the end because you don't want to
            start any Trojan horse such as:

            Code:
            #!/bin/sh
            rm -rf /*
            named 'ls' in your current directory.

            kind regards,

            Jos

            Comment

            Working...