Create a tutorial.cxx file with the following code:
#include <iostream> #include <stdlib.h> #include "MathFunctions.h" using namespace std; int main(int argc, char* argv[]) { if(argc < 2){ cout<<"Usage: "<<argv[0]<<" number"<<endl; return 1; } cout<<"The sqrt of "<<argv[1]<<" is "<<mysqrt(atoi(argv[1]))<<endl; return 0; }
Create a CMakeLists.txt in the same folder of tutorial.cxx with the following code:
cmake_minimum_required(VERSION 2.6) Project(Tutorial) include_directories("${PROJECT_SOURCE_DIR}/MathFunctions") add_subdirectory(MathFunctions) add_executable(Tutorial tutorial.cxx) target_link_libraries(Tutorial MathFunctions)
Create a folder called MathFunctions and go into it:
mkdir MathFunctions && cd MathFunctions
Create a mysqrt.cxx file with the following code:
#include "MathFunctions.h" double mysqrt(int number) { if(number <=0) return 0; double delta, result; // do ten iterations for (int i = 0; i < 10; ++i) { if (result <= 0) { result = 0.1; } delta = number - (result * result); result = result + 0.5 * delta / result; } return result; }
Create a MathFunctions.h file with the following code:
#include <math.h> double mysqrt(int);
Create a CMakeLists.txt in this same folder with the following code:
add_library(MathFunctions mysqrt.cxx)
In order to keep the build files easy to remove, create a build folder by running
mkdir build && cd build
Run Cmake and then make
cmake .. && make