After building the executable, the file is not found

Hi there,

when I run my crystal-script via

crystal run src/main.cr

the script works fine

Now I try to build a executable:

crystal build src/main.cr

The file ‘main’ is created without any errors.

I change to the ‘src’ directory and want to start ‘main’:

cd src
main
Command 'main' not found, did you mean:
   command 'man' from deb man-db
   ...

Running crystal on Linux Ubuntu 22.04

Thanks for any help.

If I’m not mistaken, the compiler will put the compiled binary in the same directory as where you ran it. So it won’t be in src.

2 Likes

Yes, you are right. The file is in the same directory as I run the compiler. But I get the same Error.

One way to get it running is to move the file “main” in the subdirectory “/src”. Starting the programm from a directory-level lower works “src/main”.

My directory-structure is:

mf/src/main.cr
mf/lib/somelibs.cr
mf/output/someoutput.csv
mf/shard.yml

Here I run the compiler:

mf$ crystal build src/main.cr

As expected the binary is here:

mf/main

Trying to start the binary do not work

mf$ main

So I copy the binary to /src:

mf/src/main

Running the binary:

mf$ src/main

I do not understand, why the binary have to be in the “src”-folder and have to been startet from a different level. I’m afraid I’m making some fundamental mistake.

What is the actual error you get as to why it doesn’t work?

It sounds like the current working directory isn’t in $PATH (for good reasons). Try ./main instead

3 Likes

I do not understand, why the binary have to be in the “src”-folder and have to been startet from a different level. I’m afraid I’m making some fundamental mistake.

In order to run it you need to type:

./main 

Short-hand example with an example structure from my point of view:

crystal build src/main.crf -o src/main

this works under Ubuntu

The error is:

Command 'main' not found, did you mean:
   command 'man' from deb man-db
   ...

And since the command is not found, it will not work.

./main works fine. Thanks.
What does the ./?

It means it’s a file in your current directory.

For example if you wanted to copy a file that’s located at: /tmp/mysuperfile to your current directory you would use the ‘.’ as well:

cp /tmp/mysuperfile .

1 Like

So if I use main without a ./, the system doesn’t search in the directory I’m in right now?

yes. on *nix systems the current directory is not included in the search path.

if you are curious, you can show the searchpath with
echo $PATH

if you want to run something, it needs to be in a directory in the path, or you specify the location relative to your current directory, or the full path.

if the executable is in the current directory (assuming mf/)

./main
~/mf/main

if it is in the src folder and you are in the mf folder:

src/main
./src/main

Or globally whereever you are:

~/mf/src/main

The “~/” is substituted by the shell for your home directory and is a shorthand for /home//.

1 Like

Thanks! I still have a lot to learn about linux.
It’s often these little things that cause difficulties for beginners.

1 Like