Trying to compile C++ with g++ in Unbuntu

Well, I've followed all the tutorials and it's still not working. I don't know where else to turn cause google isn't helping much. Tried figuring it out myself but couldn't...

I did sudo apt-get install building essentials

and sudo apt-get install g++

I then proceeded to take a sample program and enter it

1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main()
{
   cout << "Hello World\n";
   return 0;
}


I saved it as a .cpp on my desktop

Next I proceeded to type
g++ HelloWorld.cpp -o HelloWorld

I'm getting this error

g++: error: HelloWorld.cpp: No such file or directory
g++: fatal error: no input files
compilation terminated...

Anyhelp would be appreciated.

Now i've tried letting it know where my file is Desktop/HelloWorld/HelloWorld.cpp -o HelloWorld.o and it just enters the text and gives me a new line to type in. Nothing happened
Last edited on
Are you calling gcc from your home folder? If so, are you sure HelloWorld.o is not there?
Last edited on
no the problem is the directory your in doesnt contain your .cpp file
if its on your desktop try this
cd ~/Desktop
g++ *.cpp -o *
¿why the last asterisk? it would overwrite files.

@OP: *.o are usually used for object files (after compilation stage)
no it wouldnt its a wild card variable. its how i used to compile for long names. what it would do is with the first * (in the .cpp) it gets the name of the first .cpp file and then * holds the value. then its still in the second * becuase thats being read in as a wild card thats not a file. i could be completely wrong but it would work for me when i was being lazy and it was the only .cpp file in the dir
¿What shell do you use?
in sh * expands to anything

$ ls
a one.cpp two.cpp
$ g++ *.cpp -o *
# equivalent to
# $ g++ one.cpp two.cpp -o a one.cpp two.cpp
# I guess that -o will only take the first argument
# $ g++ one.cpp two.cpp one.cpp two.cpp -o a
error: multiple definition
$ ls 
one.cpp two.cpp
# `a' was overwritten and lost


If you only have 1 cpp then
$ ls
hello.cpp
$ g++ *.cpp -o *
# $g++ hello.cpp -o hello.cpp
$ ls 
hello.cpp
$ ./hello.cpp
hello world
# you do have the executable, but lost the source
i just use the normal bash shell, but it just does it to the first one. and no your wrong. if i have *.cpp for a file called one.cpp its not going to expand to one.cpp.cpp its going to expand to one.cpp -o one. if i just did one * like this
g++ * -o * then it expands to g++ one.cpp -o one.cpp and then the exe is named one.cpp.exe
if you dont beleive me when i say my terminal does that i can take a video and post it to you tube
Do that (`script' would be better). Then issue a bug to bash.

> if i have *.cpp for a file called one.cpp its not going to expand to one.cpp.cpp
I never said that. You need to make it parse, so * -> one to form one.cpp

However when you do
g++ *.cpp -o *
It translate to
g++ one.cpp -o one.cpp
because they are independent


Edit:
> it expands to g++ one.cpp -o one.cpp and then the exe is named one.cpp.exe
¿exe? ¿why does it add a suffix?
Last edited on
Firstly you handle your directory to as home
-> in console . > cd home

after write your program and save as *.cpp to home directory.

after all in console you must write this->>

g++ -Wall *.cpp -o *
@halitciftcise

Firstly you handle your directory to as home
-> in console . > cd home


cd home won't work unless you are in the directory above home. And home is not your directory - /home/halitciftcise is for example

In UNIX / Linux, there are some short cuts to one's home directory. The simplest is just cd., next there is cd ~ , and this is also handy for getting to a subdirectory of your home directory as in cd ~/Desktop

In my mind, one way would be to compile like this:

g++ -std=c++11 -Wall -Wextra -pedantic *.cpp -o nameofexecfile

Edit: And this is done in the directory where the .cpp files are, normally each project would have it's own directory.

As you can see it specifies the name of the executable explicity. Also be aware there are traps in that, what if there are old versions of .cpp files - it will attempt to compile them as well. That leaves you with trying to specify more complicated wild cards (regular expressions) to avoid that.

So the final answer is: Don't use wild cards to compile files unless you know what you are doing. Normally, one uses make files anyway.

So for simple examples do this:

g++ -std=c++11 -Wall -Wextra -pedantic one.cpp two.cpp three.cpp -o nameofexecfile
Last edited on
Topic archived. No new replies allowed.