-2

I am trying to compile a small project that uses a library (a header file and a .so file) for reading in images from a camera (on a RPI). The vendor (Arducam) provided a makefile example file, but not a CMake example file. The project is composed of: test.cpp(main file), arducam_mipicamera.h, and libarducam_mipicamera.so.

The makefile that compiles the project successfully is:

CROSS_PREFIX    ?=
CC      := $(CROSS_COMPILE)gcc
CXX     := $(CROSS_COMPILE)g++
CFLAGS  ?= -I. -g -O0 -std=gnu11

OPENCV_LIB = $(shell pkg-config --cflags --libs opencv)
ifeq ($(OPENCV_LIB), )
OPENCV_LIB = $(shell pkg-config --cflags --libs opencv4)
endif

CXXFLAGS?= -I. -g -std=gnu++11 ${OPENCV_LIB}
LDFLAGS ?=
LIBS    := -larducam_mipicamera -lpthread
OLIB    := lib
examples:= test

all: $(examples)

test : test.cpp
    $(CXX) $(CXXFLAGS) -o $@ $^ $(LIBS)

clean:
    -rm -f *.o
    -rm -f $(examples)

.PHONY: install

install: 
    sudo install -m 644 $(OLIB)/libarducam_mipicamera.so /usr/lib/

And the CMakefile I have so far is:

cmake_minimum_required(VERSION 3.11)
project( DisplayImage )

set(CMAKE_CXX_STANDARD 11)

find_package( OpenCV REQUIRED )

include_directories( ${OpenCV_INCLUDE_DIRS} )

add_executable( DisplayImage test.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )

I am confused on how to add the arducam library to the CMakefile. The header file is in the same directory as the main file and the .so file is in a subdirectory under.

How do I go about adding this simple library to the CMakefile, so that it compiles successfully (like the makefile)? This is not meant to be a dumb question, I just finished reading the CMake documentation. I am just confused about how to go about adding this library to the CMakefile. Does it involve using calls like link_directories() and target_link_libraries()?

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Sed
  • 47
  • 1
  • 1
  • 10

1 Answers1

0

The CMakefile should work if you add two more lines.

If the library is called arducam_mipicamera (which it looks like in this case), then you can use find_library(). And if the .so library file is in a subdirectory, the command should simply come out to:

find_library(ARDUCAM_LIBRARY arducam_mipicamera HINTS /*)

Where the first parameter is the name of your library (within the context of the CMakefile, you can rename this to whatever you want) and the last parameter gives the command a hint where the library is located (in this case a subdirectory).

Don't forget to link the library to target! target_link_libraries( DisplayImage ${ARDUCAM_LIBRARY} )

The final CMakefile should come out to:

cmake_minimum_required(VERSION 3.11)
project( DisplayImage )

set(CMAKE_CXX_STANDARD 11)

find_library(ARDUCAM_LIBRARY arducam_mipicamera HINTS /*)

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable( DisplayImage test.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
target_link_libraries( DisplayImage ${ARDUCAM_LIBRARY} )
Sed
  • 47
  • 1
  • 1
  • 10