{"id":277,"date":"2019-04-29T02:34:19","date_gmt":"2019-04-29T02:34:19","guid":{"rendered":"http:\/\/kabiliravi.com\/?page_id=277"},"modified":"2019-07-21T13:47:54","modified_gmt":"2019-07-21T13:47:54","slug":"build-and-run-your-first-application-with-gcc","status":"publish","type":"page","link":"http:\/\/kabiliravi.com\/index.php\/software\/programming\/mycpptutorial\/environment-setup\/build-and-run-your-first-application-with-gcc\/","title":{"rendered":"Build and run your first application with g++"},"content":{"rendered":"\n<p>This example shows you how to create a Hello World application with having the logic of hello world in a separate C++ files called hello.h the header file and hello.cpp the implementation file.<\/p>\n\n\n\n<p>Let&#8217;s start coding. However you can find following code <a href=\"https:\/\/github.com\/mycpptutorial\/helloworld-gpp.git\">here in My C++ Tutorial<\/a> in Github.<\/p>\n\n\n\n<p>First make sure gcc is installed on your machine. g++ command will be installed as a part of gcc package.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>MacOS: <strong>brew install gcc<\/strong><\/li><li>Linux:<ul><li>Debian\/Ubuntu: <strong>apt-get install gcc<\/strong><\/li><li>CentOS\/Fedora\/Redhat: <strong>yum install gcc<\/strong><\/li><\/ul><\/li><li>Windows: Install <a href=\"https:\/\/www.cygwin.com\/\"><strong>Cygwin<\/strong><\/a> or <a href=\"http:\/\/www.mingw.org\/\"><strong>MingW<\/strong><\/a><\/li><\/ul>\n\n\n\n<p>If gcc and g++ are installed correctly, you should be able to check their version using following commands:<\/p>\n\n\n\n<pre>$ g++ --version\nConfigured with: --prefix=\/Library\/Developer\/CommandLineTools\/usr --with-gxx-include-dir=\/Library\/Developer\/CommandLineTools\/SDKs\/MacOSX10.14.sdk\/usr\/include\/c++\/4.2.1\nApple LLVM version 10.0.1 (clang-1001.0.46.4)\nTarget: x86_64-apple-darwin18.2.0\nThread model: posix\nInstalledDir: \/Library\/Developer\/CommandLineTools\/usr\/bin\n\n$ gcc --version\nConfigured with: --prefix=\/Library\/Developer\/CommandLineTools\/usr --with-gxx-include-dir=\/Library\/Developer\/CommandLineTools\/SDKs\/MacOSX10.14.sdk\/usr\/include\/c++\/4.2.1\nApple LLVM version 10.0.1 (clang-1001.0.46.4)\nTarget: x86_64-apple-darwin18.2.0\nThread model: posix\nInstalledDir: \/Library\/Developer\/CommandLineTools\/usr\/bin\n<\/pre>\n\n\n\n<p>The first step is create the header file called <strong>hello.h<\/strong> . The hello.h header file contains only one line which describes the hello() function signature in abstract level. Make sure hello.h contains following line:<\/p>\n\n\n\n<b>hello.h<\/b>\n<pre>void hello();<\/pre>\n\n\n\n<p>After that create <strong>hello.cpp<\/strong> which is the implementation of hello.h header file. There you need to implement hello() function that contains body:<\/p>\n\n\n\n<b>hello.cpp<\/b>\n<pre>\n#include <iostream>\n#include \"hello.h\"\n\nvoid hello(){\n    std::cout << \"Hello World!\\n\";\n}\n<\/pre>\n\n\n\n<p>The last part is creating <strong>main.cpp<\/strong> file which in this file you call hello() function that you implemented in hello.cpp. That prints <em><strong>Hello World!<\/strong><\/em> in the output.<\/p>\n\n\n\n<b>main.cpp<\/b>\n<pre>#include \"hello.h\"\n\nint main (void){\n    hello();\n}\n<\/pre>\n\n\n\n<p>Now it's time to compile your source code and make it executable. For compiling C source codes you can use gcc command, but for C++ source codes, you need to use g++ compiler command. Here are the instruction to compile the files you have just created to Hello World application.<\/p>\n\n\n\n<p>Using -c you can compile your shared library that typically it shouldn't be executable and having main method. Here is an example:<\/p>\n\n\n\n<pre>$ g++ -c hello.cpp<\/pre>\n\n\n\n<p>The output will be<strong> hello.o<\/strong> file. Now, use following g++ with -o option to specify the executable file name you want to run and also add hello.o shared library file, so that g++ compiler link all files into one executable file called greet.<\/p>\n\n\n\n<pre>$ g++ -o greet main.cpp hello.o<\/pre>\n\n\n\n<p>Finally, you can run your compile greet app using following command:<\/p>\n\n\n\n<pre>$ .\/greet\nHello World!\n<\/pre>\n\n\n\n<p>Find the complete source code <a href=\"https:\/\/github.com\/mycpptutorial\/helloworld-gpp.git\">here in Github<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This example shows you how to create a Hello World application with having the logic of hello world in a separate C++ files called hello.h the header file and hello.cpp the implementation file. Let&#8217;s start coding. However you can find following code here in My C++ Tutorial in Github. First make sure gcc is installed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":274,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"ngg_post_thumbnail":0,"footnotes":""},"class_list":["post-277","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/277","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/comments?post=277"}],"version-history":[{"count":34,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/277\/revisions"}],"predecessor-version":[{"id":397,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/277\/revisions\/397"}],"up":[{"embeddable":true,"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/pages\/274"}],"wp:attachment":[{"href":"http:\/\/kabiliravi.com\/index.php\/wp-json\/wp\/v2\/media?parent=277"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}