1

I am a beginner to Ubuntu and Linux. I'm attempting to run just a simple hello world code and every time I run the C++ code in visual studio, it doesn't actually run, it opens to a new file called settings.json (which is empty). enter image description here

If anyone can help me sole this problem that would be great, thank you.

5
  • 4
    Visual Studio Code is not Visual Studio, btw. Commented Jun 25, 2020 at 2:30
  • 1
    OT: c++ is not a scripting language. The term "run c++ files" is not technically correct. You don't run c++ files at all. You compile them into object files which a linker may (depending on your commands) turn it into an executable application that then you can run. Commented Jun 25, 2020 at 2:31
  • Can you show your tasks.json and launch.json Commented Jun 25, 2020 at 2:32
  • 1
    Visual Studio Code doesn't come with a compiler. You need to have one installed and have VS Code set up to use the compiler. See code.visualstudio.com/docs/cpp/config-mingw. Commented Jun 25, 2020 at 3:12
  • This is probabably a better link for linux: https://code.visualstudio.com/docs/cpp/config-linux Commented Jun 25, 2020 at 3:22

3 Answers 3

2

I am a beginner to Ubuntu and Linux.

Read about the Unix philosophy.

It is favoring the command line and combination of simple tools

(e.g. in a command pipeline for your unix shell in some terminal emulator). A successful command (e.g. cp(1), used to copy files, or g++(1), a C++ compiler, or man(1) to read documentation, and od(1) or less(1) to inspect a file, or ls(1) to list them) often stays nearly silent when successful; see intro(1). Be aware of syscalls(2) (see also intro(2)). Remember that some parts of your C++ code could be generated (by metaprogramming tools such as ANTLR, swig, or GNU bison or GNU autoconf or GPP, or your own Guile, Python or GAWK or GNU bash script, or some other C++ program, etc...). See also Linux From Scratch. Every executable and process (except /sbin/init) is started by execve(2) with fork(2). See also ps(1), top(1), pstree(1) and proc(5).

Your C++ compiler could be GCC (or else Clang). Be sure to read the documentation on invoking GCC, and about your C++ preprocessor (perhaps GNU cpp). Try g++ --version then g++ --help in some terminal emulator.

If that command works, compile your HelloWorld.cpp with all warnings and debug info, so run in your terminal g++ -Wall -Wextra -g HelloWorld.cpp -o HelloWorld; you later run the obtained executable using ./HelloWorld in the same terminal (read about the $PATH variable in environ(7) and try the printenv(1) command).

Of course, you'll use some IDE or source code editor (e.g. Visual Studio Code, vim or GNU emacs or geany). Be sure to take some time to read its documentation. You'll configure them to run some build automation tool. You surely want to use some version control system, such as git.

And you'll need a debugger such as GDB.

Later, you'll want to use some build automation tool to drive your compilation commands (of several translation units) and the linking command (part of GNU binutils). Consider for your build automation using GNU make or ninja or many others.

Read of course Advanced Linux Programming and some good C++ programming book (and reference website). Be aware that C++ is a very difficult and complex programming language (see its spec n3337).

You could enjoy reading some textbook on operating systems. Study for inspiration the source code of existing open source C++ programs (e.g. on github), such as the fish shell.

Sign up to request clarification or add additional context in comments.

Comments

1

You cannot just run the file. The simplest way is to open the command terminal and find where your file is located, the path that it is stored in and type in g++ fileName.cpp, make sure to name it with a cpp extension.

7 Comments

Thank you, it doesn't output anything to the terminal though, i'm not sure why. Could it be something with the location of my G++? I have my c++ code stored in my Documents folder and g++ is located here: g++: /usr/bin/G++/usr/share/man/man1/g++.1.gz
Use cd to navigate to your file, so if your file is in users/bin/helloWorld.cpp: Do the following: cd users[enter] cd bin[enter] g++ helloWorld.cpp use ls to list all the files in the directory, this is starting to become UNIX knowledge not c++ anymore.
I cd into the documents folder and then entered "g++ HelloWorld.cpp"
Check where your helloworld.cpp is located, also don't forget to add a main function.....
The hello world cpp file is located in the documents folder, I cded into documents and typed ls, then it shows "HelloWorld.cpp" so i ran that command above^ and it did not output anything. I have a main function in the program
|
1

It is possible to compile and run C/C++ programs from Visual Studio Code.

To compile and run C/C++ programs from Visual Studio Code (vscode) you need to install C/C++ Compile Run extension from danielpinto8zz6 in Visual Studio Code.

After installing the C/C++ Compile Run extension press F6 to compile and run C/C++ Program.

Hope it helps !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.