Hello World C++ application setup using CMake for Eclipse IDE
Sep 27, 2018
This is my first blog post. So I ll be presenting a simple introduction to C++ project developement.
Let us create a simple
Hello World application in C++.
|IDE / Editor||Eclipse, Vim & VS Code|
- First we create a folder to host the project files such as header files, source files and the build system files. This is called the source folder
HelloWorld(it is recommended to avoid spaces in file and folder names) as it hosts only the source files.
- We create another folder to host the build results files such as the object files, compiled files and binaries. This is called build folder
- For convenience sake we create a top level folder
HelloWorld_topto host the source and build folders. This type of project structure is useful to separate the source code from the build files and hence has many advantages which I’ll be writing about in the following blog posts.
- Combining the above 3 steps we get a folder structure as:
|-- HelloWorld_top |-- build |-- HelloWorld
- Go to source folder
HelloWorldand create folder to host:
- Header files
inc, i.e. *.h, *.hpp files
- Source files
src, i.e. *.c, *.C++ files
- Test files
test, i.e. test code required for testing the source files.
- Document files
doc, i.e. *.dox, *.rst to store the documentation for the code. Will be used in bigger project for automatic generation of documents in various popular file formats such as PDF, HTML.
- Data files
data, i.e. any data required for building/testing the application such as the images, icons etc. Not applicable here [small projects], will be used in bigger projects.
- Utility files
util, i.e. bash scripts, python scripts to automate or simplify the redundant tasks such as creating the folders for above steps.
Now the source folder
HelloWorldstructure is as follows:
|-- HelloWorld |-- data |-- doc |-- inc |-- src |-- test |-- util
- Header files
- Write the code for
HelloWorldproject as in the github repo: HelloWorld
- Now go to the
buildfolder. Use the below cmake command to generate the build files along with the Eclipse CDT project files. For bigger projects it is recommended to use an IDE like Eclipse, CLion or VS Studio. Please read CMake documentations to understand the below command in depth.
cmake -G"Eclipse CDT4 - Unix Makefiles"\ -DCMAKE_ECLIPSE_GENERATE_SOURCE_PROJECT=TRUE\ -DCMAKE_BUILD_TYPE=Debug\ -DCMAKE_CXX_FLAGS="-std=c++11"\ -DCMAKE_CXX_COMPILER_ARG1=-std=c++11
- That was a rough explanation of how I setup my projects in C++.
- I will explain the reason behind the project structure, choice of tools used, code style and many more things in my future posts.
- It is definitely not an entry level tutorial it is for professional programmers with at-least 1 year of work experience.
- I have more ideas to share in the future posts all of them follow the same project structure, coding style and tools. Hence this introductory post. Stay tuned and follow me on social medias in my bio.
- Thank you, for reading. Please leave a comment below or use one of the social media links from my bio.
- This blog post is about Hello World project in C++ not a program that can be written in a single file.
- The intent of this post is to give you an idea of how to setup a C++ project with CMake for Eclipse IDE.
- This post assumes that you already know programming and have experience in creating, debugging and executing programs in any modern languages such as C, C++, C#, Java or Python.