I just started with C++ on Ubuntu and I'm already stuck.
I made my first .cc program and tried to compile it with g++.
I named it variabili.cc and saved it in a folder called Esercizi on my Desktop.
When I try with
g++ variabili.ccI get:
g++: error: variabili.cc No such file or directory
g++: fatal error: no input files compilation terminatedI checked that the file is in there with ls ~/Desktop and it is there. The name is correct as well.
No matter what I do it doesn't seem to work.
1 Answer
g++ doesn't automatically know the source code file is on your desktop. It's looking for it in your current directory -- wherever you are in your shell from which you run it. If you haven't changed directory with the cd command, that's probably your home directory. The Desktop directory is a subdirectory of your home directory.
variabili.cc is in ~/Desktop, so you must either...
...be there in your shell when you run
g++:cd ~/Desktop g++ variabili.cc...or tell
g++where the file is:g++ ~/Desktop/variabili.cc
Of these two options, I recommend cding to the location where your file is. That way, the generated executable and any other output files will automatically be put in the same directory as your source code.
You might want to give g++ some other options too, such as to enable warnings (-Wall -Wextra) or specify the name of an output file (-o filename), but neither is necessary to make your code compile.
If you're wondering what directory currently you're in, your prompt likely shows you. For example, on my machine (Io), logged in as the user ek, I started in my home directory (~, which for me means /home/ek), changed directory to ~/Desktop, then changed directory to /:
ek@Io:~$ cd Desktop/ek@Io:~/Desktop$ cd /ek@Io:/$The text that I have shown in bold is the prompts.
You can also always find out where you are by running pwd, which stands for "present working directory":
ek@Io:~/Desktop$ pwd
/home/ek/Desktop 0