OpenCV requires huge amount of libraries to compile . trying to do it all yourself is not a good idea, do not reinvent the wheel. A good starting point would be this installer.
Go to your home and then type
git clone https://github.com/jayrambhia/Install-OpenCV.git
Then a simple ls will show you available installation scripts for various distributions. For example if you are on Ubuntu you would
cd Ubuntu .
Then you need to make the files executables by
chmod +x *
Then simply type
./opencv_latest.sh and wait for it to finish