Compiling App with OpenCV and CMake

Hi everyone

I’m trying to compile my first app using snap. It’s a simple command line tool but relies on OpenCV. Currently, I compile it using a CMakeLists.txt which has find_package(OpenCV REQUIRED) as well as target_link_libraries. I then run the following:

$ cmake .
$ make

which produces the binary of the project.

I’d like to package everything as a snap which includes the OpenCV libraries. I went as far as downloading OpenCV from source (like I’d usually do) then including it as a part.I know the compilation process but not sure how to first compile OpenCV then link it to my app.

Hope that made sense.XD

It’s pretty easy to use OpenCV as a build-package if you don’t want to build it from source (doing so is a snapcraft demo, actually).

However, assuming you’re building it from source because you need a newer (or older) version than what is in the archive, it should be pretty easy.

Make OpenCV its own part, and utilize the after keyword for the part that depends upon it (i.e. the one using find_package(OpenCV REQUIRED). after: [opencv] or something (more information here). That should Just Work. If it doesn’t we can investigate further :smile: .

1 Like

Hi kyrofa.

Thanks for the direction. Will try it out soon and let you know.:slight_smile:

So far I have opencv’s source code located in:

project/
src/
opencv-3.2.0/

And for my snapcraft.yml I have:

parts:
opencv3-2:
plugin: cmake
source: ./src/opencv3-2.0/

However it won’t compile as expected. How will I tell snapcraft to produce the necessary libraries and link that to my CMakeLists.txt for the main app?

Progress…sort of? This allows me to compile the local opencv library but not sure if this is the best practice. I can always use a custom script to do the make install process then run CMake against my app but that kinda defeats the whole purpose of snapcraft right?

Finally got it to work with this snapcraft.yml:

So it downloads OpenCV first and compiles using CMake. It then compiles the main app (img2vector) after opencv-snap. The trick in the actual app was to have install(TARGETS img2vector RUNTIME DESTINATION bin) in the CMakeLists.txt

1 Like

Awesome, nice job!

Yeah, without install rules the cmake plugin doesn’t know what you want installed into the snap, or where to put it. It’s good to add it in your case, but you may not always have control over the source that doesn’t have install rules. In which case, you could do something like this:

img2vector-snap:
  plugin: cmake
  source: https://github.com/ralampay/img2vector
  source-type: git
  after: [opencv-snap]
  install: |
    mkdir -p $SNAPCRAFT_PART_INSTALL/bin/
    cp path/to/img2vector $SNAPCRAFT_PART_INSTALL/bin/

You can read more about the prepare, build, and install scriptlets here.

Final note: the cmake plugin will make sure cmake is installed as a build-package– you don’t need to include (although it doesn’t hurt anything).

Awesome. Ok will try that out. Thanks for the advice. :slight_smile: