简体   繁体   中英

create and compile “hello world” application in Linux using Visual Studio Code

I am new to Linux, but developed C/C++ in windows for some times now.

I installed a Linux Ubuntu 16.4 and Visual Studio Code.

I create a folder and inside that folder I created a file called main.cpp and inside that file I wrote:

#include <iostream>
void main()
{
    std::cout << "Hello World" << std::endl;
}

Now I want to compile and run it and possibly debug it (step by step to see how I can debug a simple application).

How can I do this?

Any tutorial on to setup a development system in Linux using Visual Studio Code?

I installed "C/C++ for Visual Studio Code" but I am still not able to compile and run the sample application.

Edit1

I already installed compiler and can compile my code using

g++ main.cpp

and getting a.out

How can I configure VSC to automate this processor and generate dependency and if there is an error, open the file with error on editor and show me the line that generate error. Also during debug show me the source code when I am stepping the code.

These are some basic requirement that I have from a development system, otherwise I call it an editor and not a development system.

First, you have to install a compiler, I recommend GCC (Ubuntu usually doesn't come with one even though it's mostly written in C/C++)

Second, compile your program, here's how

To compile the program, open the terminal and move on to the target directory type the command – (where gcc implies compiler name, then it asks for the file name of the source program while -o option specifies the file name of the output program)

 gcc hello.c -o hello1 

If there is no syntax/semantic error in you program then the compiler will successfully generate an executable file, otherwise fix the problem in your code.

However, that will only work for C, here's how to do it for C++ (only if the extension is .cpp)

The steps are almost same as above but you need to install g++ compiler, the file extension should be .cpp and in compilation phase replace gcc with g++. To install G++ compiler, execute the command -

sudo apt-get install g++

The compilation command now is:

g++ hello.cpp -o hello1

It goes without saying that you should replace "hello.cpp" with your files name and "hello1" with the name you want your "exe" file to have

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM