C++ 1

July 21, 2019
C++ Makefile CMake

C++ ~ Make and CMake

One of the things Sam suggested to learn/use with C++ is make and cmake. I’ve come across make before, but not cmake. This blog is going to be about learning the basics of both make and cmake, as well as creating simple example projects with them.

The following is a simple program that I made to start with make.

// main.cpp
#include "hello.h"

int main()
{
    Hello h;
    h.printHello();

    return 0;
}
// hello.h
class Hello {
public:
    void printHello();
};
// hello.cpp
#include <iostream> // System defined header file
#include "hello.h" // User defined header file

using namespace std;

void Hello::printHello() {
    cout << "Hello world from Makefile example!\n";
}
# Makefile
output: main.o hello.o
	g++ main.o hello.o -o main

main.o: main.cpp
	g++ -c main.cpp

hello.o: hello.cpp hello.h
	g++ -c hello.cpp

clean:
	rm *.o main

Now all that needs to be done is run the make command. After running the newly compiled binary main, we get the expected output; Hello world from Makefile example!.

Next is to create a cmake example using the same program.

// CMakeLists.txt
cmake_minimum_required (VERSION 3.10)

project (hw_cmake_example)

add_executable(
    main
    main.cpp
    hello.cpp
    hello.h
)

With the CMakeLists.txt file done, now to create a build directory mkdir build && cd build, then to run the command cmake ... This creates a few files, one of which is a Makefile. We can now run make and then run the compiled binary main, which gives the expected output; Hello world from the CMake example!.

Doom 0

September 22, 2019
DOOM C C++ Linux Open Source

C++ 4

July 26, 2019
C++ Headers

C++ 3

July 23, 2019
C++ Basics
comments powered by Disqus