Yes, you should use g++ to compile C ++ instead of gcc , but you should probably consider the answer as you say you're new to using compilers.
GCC stands for GNU Compiler Collection and is a collection of programs that can be used to compile source code for different languages. The gcc command provides an external interface for users to compile their programs. The gcc command will try to determine the language you want to compile from the file extensions in the source files. For example, a file named hello.c will be compiled as C, a file named foo.cpp will be compiled as C ++, and a file named bar.m will compile as Objective-C.
You can give the -x option to explicitly specify which language to compile, regardless of the file extension. For example, gcc -x c++ main.c will compile main.c as a C ++ file, despite the extension .c .
However, although gcc will determine that your files are C ++, by default it will still not reference the standard C ++ library. This library should be related to compiling nothing but the simplest C ++ files. You can link to the GCC implementation of the standard library by adding -lstdc++ to your options. Therefore gcc -lstdc++ prog2.cpp should work fine.
However, for convenience, a different command is provided for C ++ programmers. Namely, g++ . The g++ command will automatically reference the standard C ++ library, so you do not need to do this explicitly. It also calls .c , .h and .i files to process C ++ files.
If you use gcc sequentially to compile C and g++ , you should have no problem.
source share