run a C++ program from terminal


Linux doesn’t have extensions … it’s not a brain dead OS like Windows, at least not when it comes to actually developing anything.

To make life easy, however, there are certain conventions.

If I have a c++ source file called foo.cpp and I run



gcc -c -Wall -g foo.cpp

The program gcc will understand that foo.cpp is a C++ source file (not a fortran, C or java source file) and generate an object file with the extension .o

If I then do:

gcc -g -L/usr/lib -lstdc++ foo.o

gcc will understand that i intend to invoke the linker (ld) and use in the /usr/lib directory, also — add debugging symbols.

Because I didn’t specify a target, gcc will produce a file a.out and set executable permissions on it.

I type:


On the other hand I can specify a target with gcc:

gcc -g -L/usr/lib -lstdc++ foo.o -o

It makes no difference what I call the compiled binary executable, linux could care less.

Of course, for a simple program, I would skip the intermediate object:

gcc -g -Wall -lstdc++ foo.cpp -o foo

and now gcc understands that I want to compile a c++ source file, link it to the system’s standard c++ shared object and put the result (with debugging symbols) in a file called foo.

Or I can make my life even easier:

g++ -g -Wall foo.cpp -o foo

g++ is a program that does everything gcc did in the example above, but it only deals with c++ code and linkage.



Please shareShare on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0Digg thisShare on StumbleUpon0Share on VKBuffer this page