Running C Programs on Linux

5,839

Solution 1

You don't have the value of the PATH environment variable set to include whatever directory the HelloWorld executable file lives in.

Supposing you have used cd to get to the directory, you can run HelloWorld with this command: ./HelloWorld

Unix shells have a variable called PATH, which is a :-delimited list of directories in which to look when the user issues a command without a fully-qualified path name (/usr/bin/ls is fully qualified: it starts at / and ends at ls, but ls is not fully-qualified by itself). If you don't have an entry of . in PATH, you have to explicitly use ./ on the beginning of a command to get the file of that name in the current directory to execute.

Solution 2

Use ls -F to see a star at the end of filename. If you use ls from GNU coreutils you can set the environment variable LS_OPTIONS to automatically include this and any other number of options for you. The options for ls are listed in the ls manual, to see them run: man ls

@brewer2: setting environment variables on login is shell specific and a good question in itself.

Share:
5,839

Related videos on Youtube

brewer2
Author by

brewer2

Updated on September 18, 2022

Comments

  • brewer2
    brewer2 over 1 year

    I watched a video lecture today that introduced C and things like how to make a C program that will run in Linux. I followed the steps given and now I'm stuck with a bit of a problem.

    I created my C file (HelloWorld.c) and used the command gcc -o HelloWorld HelloWorld.c to compile the file, both of these steps were successful. Afterwards I checked to make sure that HelloWorld had been created by using the command ls, and it had been. However, when I use the command HelloWorld, which is supposed to run the program, I get an error that says HelloWorld: command not found.

    In the video lecture the professor did mention that this worked for 32-bit systems and I'm using a 64-bit system. Perhaps this could be the problem?

    EDIT: Also in the video lecture the professor mentioned that when I use the command ls I should see HelloWorld*. I see only HelloWorld (without the star).

  • brewer2
    brewer2 about 13 years
    How would I set LS_OPTIONS to automatically include -F?
  • Mr. Shickadance
    Mr. Shickadance about 13 years
    While I think the accepted answer is the right one, this is also good to know.
  • Alen Milakovic
    Alen Milakovic about 13 years
    It's worth pointing out that '.' is not in PATH for security reasons, and therefore it is a bad idea to add it. In case anyone thinks this was some unaccountable oversight on the part of their OS provider.
  • Anderson Green
    Anderson Green over 11 years
    How would you run the program with command-line arguments, then?
  • Kusalananda
    Kusalananda almost 6 years
    @AndersonGreen ./HelloWorld arg1 arg2 arg3 etc
  • Kusalananda
    Kusalananda almost 6 years
    @brewer2 alias ls='ls -F'
  • ron
    ron almost 6 years
    Regarding current directory of . being in PATH, for a regular user who falls under the security umbrella of the system it should no longer be said it should not be done for security reasons. It is not 1995, and with most modern linux distros there are plenty of other functional security mechanisms in place preventing what used to be a risk regarding this. Any user has always been able to modify PATH and put . anywhere in it. This is one of those old ideas die hard; doing . in PATH for the root account would be a risk and should not be done unless the risk is understood