Using CMake (part 2) – Adding Libraries

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

Using CMake (part 1) – Hello World

Create a tutorial.cxx file with the following code:

#include 

using namespace std;

int main()
{
 cout<<"Hello world"<<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)

add_executable(Tutorial tutorial.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

Turn on/off GCC warnings in CMakeLists.txt

For example, you receive the following message:

nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures 
are deprecated, and may be removed in a future release 
(Use -Wno-deprecated-gpu-targets to suppress warning).

So, how do you do this with CMake?

Just add the following line to your CMakeLists.txt:

add_definitions("-Wno-deprecated-gpu-targets")

But if you want, for example, turn on warnings for the return of a function (“control reaches end of non-void function” type of error, for example), just add:

add_definitions("-Wreturn-type")

How to check if some program is installed in Ubuntu

You can run the following command:

dpkg -s packagename | grep Status

If your program is installed, you will have an answer like this:

$ dpkg -s libusb-1.0-0-dev | grep Status
Status: install ok installed

But if you have not the program, the answer can be similar to:

$ dpkg -s openjdk-6-jdk | grep Status
dpkg-query: package 'openjdk-6-jdk' is not installed and no information is 
available
Use dpkg --info (= dpkg-deb --info) to examine archive files,
and dpkg --contents (= dpkg-deb --contents) to list their contents.

How can I make a program executable from everywhere (Ubuntu)

UPDATE: from here

Note that ~/.bash_rc is not read by any program, and ~/.bashrc is the configuration file of interactive instances of bash. You should not define environment variables in ~/.bashrc. The right place to define environment variables such as PATH is ~/.profile (or ~/.bash_profile if you don’t care about shells other than bash). 

Just type the the following command in the terminal ant it will work temporarily

export PATH=$PATH:</path/to/file>

So if I have a program called a.out located in /home/user/Desktop/MyProgram/, I would type:

export PATH=$PATH:home/user/Desktop/MyProgram/

To make it permanent, add the same line to the file ~/.bashrc .

error while loading shared libraries: libcudart.so.8.0: cannot open shared object file: No such file or directory

From here:

“When a CUDA program is executed, it needs to dynamically link to the CUDA runtime libraries. By default, these libraries are located in the /usr/local/cuda/lib directory. When searching for these libraries, the operating system looks in directories specified in the $LD_LIBRARY_PATH environment variable. If the CUDA library directory is not specified here, the program will fail with the error shown above.

To solve the problem, one option is to edit the CMakeLists.txt to find the library in the case it is not in the default location. IMPORTANT: the file “libcudart.so.8.0” (or libcudart.so.7.0 if you have cuda 7, for example) must be inside this folder! To do that, just add the following line in your CMakeLists.txt:

link_directories(/usr/local/cuda-8.0/lib64)

This is for the cuda 8.0. Just change the “cuda-8.0” according to your version.

The same effect can be reached by changing the shared library cache. Just run in the terminal:

sudo ldconfig /usr/local/cuda-8.0/lib64

This one will also work:

export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64/:$LD_LIBRARY_PATH