So I just installed Ubuntu 18.04 on my (not so shiny) new thinkpad and I'm loving it, especially the new GNOME getup.

So I was looking to run some c++ code. Seems gcc is already installed by default so I try out a simple hello world program called test.cpp

I compile it: cpp test.cpp -o test ... works fine no errors. I then try to run it but it says permission denied, so I do a quick chmod u+x and it seems to at least try to run the program. No matter what code I put in and compile, it always comes with these errors:

./test: line 18: namespace: command not found
./test: line 22: syntax error near unexpected token `('
./test: line 22: `  typedef decltype(nullptr) nullptr_t;'

--Also note that there are only 7 lines of code and it gives me an error for line 18+?! Also why is it giving me errors after I compiled it?!

So I made a new account on #askubuntu and here I am now.

  • If your test is really c++ code, you should install and use the g++ compiler, not the gcc compiler. – ubfan1 Mar 12 '19 at 20:03
  • oh sorry by gcc I meant the gcc compiler collection which is pre-installed on Ubuntu, to actually compile the code I used cpp which is part of gcc – armand de bruijn Mar 12 '19 at 22:16
  • Yes I see what you mean, but I can assure you that i typed the command ./test rather than ./test.cpp. @steeldriver – armand de bruijn Mar 12 '19 at 22:18

cpp is the C pre-processor - it is not a compiler. Its output is a text file containing expanded macros and declarations from any files that your code has #includeed. When you made it executable and tried to run it, your shell made an attempt to interpret this text as shell code - and failed.

To compile your C++ file (and link into an executable) you should use g++ instead:

g++ test.cpp -o test
  • it all makes sense now, guess I mixed up – armand de bruijn Mar 13 '19 at 21:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.