I am new to Linux, and C++. Usually when I used Java, I could just use javac Something.java and run it using java Something. How do I compile my own c++ code without the help of an IDE? Also I am using Linux Ubuntu 12.04

Edited by godzab

4 Years
Discussion Span
Last Post by godzab

The shell command you are looking for is g++ filename -o executablename, or at least that will get you started (the '-o' stands for 'output', and let's you choose the name of the final executable). Look up the documentation on GCC and make for more involved compilation.

Edited by Schol-R-LEA


Install the build-essential package using sudo apt-get install build-essential then use g++ filename -o outputname as mentioned by Schol-R-LEA.

This will produce the executable, you can then run it by typing ./outputname.

Enjoy coding in C++ on Linux!


Simple (copy&paste entire parts into root and user shell terminal prompts and hit enter, respectively):

(at # root shell prompt)
apt-get install build-essential cmake

(at $ user shell prompt)
export SOURCEDIR=~/projects/cmake_hello_world && mkdir -p $SOURCEDIR && cd $SOURCEDIR &&
(cat <<'EOF'
cmake_minimum_required(VERSION 2.6)
file(WRITE main.cpp "#include <stdio.h>\n int main() { printf(\"Hello World!\\n\"); return 1; }")
add_executable(cmake_hello_world main.cpp)
) > CMakeLists.txt &&
export BUILDDIR=build.make && mkdir $BUILDDIR && cd $BUILDDIR &&
cmake .. && make && ls -lR .. &&

So much for an 11-liner shell script snippet to create a fully flexible Makefile-based build environment ;)

See also
$ cmake --help
$ cmake --help-command file

Next, maybe install eclipse / kdevelop / codeblocks / anjuta and generate projects for them (-G'KDevelop3').

Have fun!

[let's hope all shell parts came across properly escaped in the posting]


For simple C++ executables, you can just use the make command directly. So, if you want to build a simple C++ source file named my_application.cpp, you can just execute the command: make my_application
That will compile and link the executable for you quite nicely. A Makefile is unnecessary unless you have more than one source file to link.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.