Compiling Caffe C++ Classification Example

Usually, in order to help the compiler locate header files you need to add -I /path/to/include/folder option to the compilation line:

~$ g++ -I /path/to/caffe/include myfile.cpp

If you want to build custom files in caffe, there are two ways

The easy way

  • Make the necessary changes and keep the file ( in your case - classification.cpp ) inside a directory ( say test ) in examples folder in th e caffe root directory.
  • run make. This will automatically add the necessary cxxflags and ldflags and compile your code and place the executable in the build/examples/test folder. This also ensure the flag CPU_ONLY is set ( as mentioned in the Makefile.config )

The Hard way

  • Run make without the pretty print option ( mentioned inside Makefile.config ). You will be able to see the compile and link options used to build the examples and tools. You can copy and paste these options ( and make necessary changes to relative paths if used ) to compile your file

Hope this helps

Edit As the op requested an easy way, it can be done as follows

This is a very minimal example and I encourage the OP to refer to full online documentation and example of cmake usage.

  • Requirements
    • Caffe needs to be built with cmake - Relatively easy as the current master branch has CMakeLists and everything defined. Use the Cmake-gui or ccmake to set your options

Now, I am assuming you have a project structure as follows.

-project  
    - src  
         - class1.cpp
         - CMakeLists.txt ( to be added )
    - include
         - class1.hpp

    - main.cpp
    - CMakeLists.txt ( to be added )

The CMakeLists.txt ( src ) needs to contain (at minimum) the following lines,

cmake_minimum_required(VERSION 2.8)
find_package(OpenCV REQUIRED) # Optional in case of dependency on opencv 
add_library( c1 class1.cpp )

Note: In case class1 depends on other external libraries, the path to headers must be included using include_directories.

The CMakeLists.txt ( outermost ) needs to contain the following at minimum

cmake_minimum_required(VERSION 2.8)
PROJECT(MyProject)

find_package(OpenCV REQUIRED)
find_package(Caffe REQUIRED)

include_directories( "${PROJECT_SOURCE_DIR}/include" )
add_subdirectory( src )

include_directories( "$Caffe_INCLUDE_DIRS}" )
add_executable(MyProject main.cpp)

target_link_libraries( MyProject ${OpenCV_LIBS} c1 ${Caffe_LIBRARIES} )    

Now, the following commands from inside the project directory will create the executable MyProject inside the build folder.

mkdir build
cd build
cmake ..
make

You can then run your program with ./MyProject (arguments)

EDIT 2

Satisfying the requirement of building caffe with CMake is very important for this to work. You need to configure and generate the Makefiles using CMake. Use cmake-gui or ccmake for this purpose so that you can set your options like CPU_ONLY, etc.

You should create a build directory inside caffe and execute the following for a basic setup

mkdir build
cd build
cmake ..
make -jX #X is the number of threads your CPU can handle

Now, the .cmake directory in your $HOME folder consists of the following /home/user/.cmake/packages/Caffe/<random_string> file. This file points to the install location of caffe ( which is our build directory )

Now, the find_package command should run without errors for your other projects. And since you are using CMake you can keep your project folder outside the Caffe folder ( and it is better to keep it outside as the make process of caffe will try to build your files but it will fail )

Note: In case that the error persists, you can manually set the Caffe_DIR during cmake configuration.