Unable to include android ndk in cmake visual studio - c++

I was trying to make a cross platform imgui project in visual studio 2022, starting with android. In the beginning, I was not able to include some android libraries properly, so after following several tutorials, I ended up with this CMakeLists.txt:
# CMakeList.txt : CMake project for CMakeProject3 (imgui Cross-platform), include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
project ("CMakeProject3 (imgui Cross-platform)")
list(APPEND CMAKE_PREFIX_PATH "C:/Users/HP/source/repos/CMakeProject3 (imgui Cross-platform)/build/vcpkg_installed/x64-windows/share/")
find_package(imgui REQUIRED)
# Add source to this project's executable.
add_executable (CMakeTarget "CMakeProject3 (imgui Cross-platform).cpp" "CMakeProject3 (imgui Cross-platform).h" "imgui_android.cpp" "imgui_impl_android.cpp" "imgui_impl_android.h")
target_include_directories(CMakeTarget PRIVATE "C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/android/"
"C:/Microsoft/AndroidNDK/android-ndk-r23c/sources/android/native_app_glue/")
target_link_libraries(CMakeTarget
PRIVATE
imgui::imgui
)
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET CMakeTarget PROPERTY CXX_STANDARD 20)
endif()
# TODO: Add tests and install targets if needed.
It works partially but according to example of android imgui example, it needs
#include <android/log.h>
#include <android_native_app_glue.h>
#include <android/asset_manager.h>
I changed to:
#include "log.h"
#include "android_native_app_glue.h"
#include "asset_manager.h"
It started to give several errors for cannot open source file. The errors were for poll.h, pthread.h, sched.h, android/configuration.h, etc. It seems to me that I need to inlcude android ndk completely but
include(AndroidNdkGdb)
include(AndroidNdkModules)
gives error in cmake even when I installed android ndk and sdk.
I want to be able to use android libraries in my cross-platform project in visual studio 2022.

I fixed the error by updating CMakeLists.txt to:
# CMakeList.txt : CMake project for CMakeProject3 (imgui Cross-platform), include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
project ("CMakeProject3 (imgui Cross-platform)")
list(APPEND CMAKE_PREFIX_PATH "C:/Users/HP/source/repos/CMakeProject3 (imgui Cross-platform)/build/vcpkg_installed/x64-windows/share/")
find_package(imgui REQUIRED)
set(CMAKE_SYSROOT C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot)
# Add source to this project's executable.
add_executable (CMakeTarget "CMakeProject3 (imgui Cross-platform).cpp" "CMakeProject3 (imgui Cross-platform).h" "imgui_android.cpp" "imgui_impl_android.cpp" "imgui_impl_android.h")
target_include_directories(CMakeTarget PRIVATE "C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/"
"C:/Microsoft/AndroidNDK/android-ndk-r23c/sources/android/native_app_glue/"
"C:/Microsoft/AndroidNDK/android-ndk-r23c/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/asm/")
target_link_libraries(CMakeTarget
PRIVATE
imgui::imgui
)
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET CMakeTarget PROPERTY CXX_STANDARD 20)
endif()
# TODO: Add tests and install targets if needed.

Related

"Cannot open include file" when implementing library that implements GLM using CMake

I'm creating a game using opengl, glfw and glm. Now I have two projects, the engine (named Cheetah) and the game. The engine is a dll library and the game implements that library. Both the engine and the game projects contain a CMakeLists.txt, and there is one top level CMakeLists.txt that builds both.
The folder structure is as below:
The issue
I'm trying to add glm to the library, but now I'm running in to the issue that I can build my library but I'm unable to build the game project that implements the library, throwing the compile error:
Error C1083 Cannot open include file: 'glm/common.hpp': No such file
or
directory C:\Projects\Game\out\build\x64-Debug\Game C:\Projects\Game\Cheetah\src\Engine\Renderer\OrthoGraphicCamera.h
Here are the CMake files per project:
Cheetah(Engine project)
# CMakeList.txt : CMake project for Cheetah, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
project ("Cheetah")
# Platform defines
IF (WIN32)
add_compile_definitions(CH_PLATFORM_WINDOWS)
ELSE()
# set stuff for other systems
ENDIF()
#----------------------------------------------
# ADD CHEETAH LIBRARY
#----------------------------------------------
message(${PROJECT_SOURCE_DIR})
# Define Cheetah variables
set(LIB_DIR "${PROJECT_SOURCE_DIR}/dependencies")
set(INCLUDES_DIR_PUBLIC "${PROJECT_SOURCE_DIR}/includes")
set(INCLUDES_DIR_PRIVATE "${PROJECT_SOURCE_DIR}/src")
set(ENGINE_DIR "${PROJECT_SOURCE_DIR}/src/Engine")
set(ENGINE_CORE_DIR "${ENGINE_DIR}/Core")
set(ENGINE_INPUT_DIR "${ENGINE_DIR}/Input")
set(ENGINE_EVENTS_DIR "${ENGINE_DIR}/Events")
set(ENGINE_RENDERER_DIR "${ENGINE_DIR}/Renderer")
set(ENGINE_DEBUG_DIR "${ENGINE_DIR}/Debug")
set(ENGINE_MATH_DIR "${ENGINE_DIR}/Math")
set(ENGINE_RESOURCES_DIR "${ENGINE_DIR}/Resources")
set(PLATFORM_DIR "${PROJECT_SOURCE_DIR}/src/Platform")
set(PLATFORM_WINDOWS_DIR "${PLATFORM_DIR}/Windows")
set(PLATFORM_OPENGL_DIR "${PLATFORM_DIR}/OpenGL")
# Set default files
list(APPEND SOURCE_FILES
"${ENGINE_DIR}/Engine.h"
"${ENGINE_CORE_DIR}/Application.h"
"${ENGINE_CORE_DIR}/Application.cpp"
"${ENGINE_CORE_DIR}/Core.h"
"${ENGINE_CORE_DIR}/EntryPoint.h"
"${ENGINE_CORE_DIR}/Window.h"
"${ENGINE_CORE_DIR}/UpdateLayer.h"
"${ENGINE_CORE_DIR}/UpdateLayer.cpp"
"${ENGINE_CORE_DIR}/UpdateLayerQueue.h"
"${ENGINE_CORE_DIR}/UpdateLayerQueue.cpp"
"${ENGINE_CORE_DIR}/Time.h"
"${ENGINE_CORE_DIR}/Time.cpp"
"${ENGINE_RENDERER_DIR}/RenderAction.h"
"${ENGINE_RENDERER_DIR}/RenderAction.cpp"
"${ENGINE_RENDERER_DIR}/GraphicsContext.h"
"${ENGINE_RENDERER_DIR}/GraphicsContext.cpp"
"${ENGINE_RENDERER_DIR}/RenderAPI.h"
"${ENGINE_RENDERER_DIR}/RenderAPI.cpp"
"${ENGINE_RENDERER_DIR}/Renderer.h"
"${ENGINE_RENDERER_DIR}/Renderer.cpp"
"${ENGINE_RENDERER_DIR}/Renderer2D.h"
"${ENGINE_RENDERER_DIR}/Renderer2D.cpp"
"${ENGINE_RENDERER_DIR}/IndexBuffer.h"
"${ENGINE_RENDERER_DIR}/IndexBuffer.cpp"
"${ENGINE_RENDERER_DIR}/VertexBuffer.h"
"${ENGINE_RENDERER_DIR}/VertexBuffer.cpp"
"${ENGINE_RENDERER_DIR}/VertexArray.h"
"${ENGINE_RENDERER_DIR}/VertexArray.cpp"
"${ENGINE_RENDERER_DIR}/Shader.h"
"${ENGINE_RENDERER_DIR}/Shader.cpp"
"${ENGINE_RENDERER_DIR}/VertexBufferLayout.h"
"${ENGINE_RENDERER_DIR}/VertexBufferLayout.cpp"
"${ENGINE_RENDERER_DIR}/Texture.h"
"${ENGINE_RENDERER_DIR}/Texture.cpp"
"${ENGINE_RENDERER_DIR}/OrthoGraphicCamera.h"
"${ENGINE_RENDERER_DIR}/OrthoGraphicCamera.cpp"
"${ENGINE_RENDERER_DIR}/Renderer2DQueue.h"
"${ENGINE_RENDERER_DIR}/Renderer2DQueue.cpp"
"${ENGINE_INPUT_DIR}/Input.h"
"${ENGINE_INPUT_DIR}/Input.cpp"
"${ENGINE_EVENTS_DIR}/ApplicationEvents.h"
"${ENGINE_EVENTS_DIR}/ApplicationEvents.cpp"
"${ENGINE_EVENTS_DIR}/Event.h"
"${ENGINE_EVENTS_DIR}/Event.cpp"
"${ENGINE_EVENTS_DIR}/EventDispatcher.h"
"${ENGINE_EVENTS_DIR}/EventDispatcher.cpp"
"${ENGINE_EVENTS_DIR}/EventTypes.h"
"${ENGINE_EVENTS_DIR}/InputEvents.h"
"${ENGINE_EVENTS_DIR}/InputEvents.cpp"
"${ENGINE_RESOURCES_DIR}/ResourceCache.h"
"${ENGINE_RESOURCES_DIR}/ResourceCache.inl"
"${ENGINE_RESOURCES_DIR}/ResourceLoader.h"
"${ENGINE_RESOURCES_DIR}/ResourceLoader.cpp"
)
# Set platform specific files
# Windows
list(APPEND SOURCE_FILES_WINDOWS
"${PLATFORM_WINDOWS_DIR}/WindowsWindow.h"
"${PLATFORM_WINDOWS_DIR}/WindowsWindow.cpp"
"${PLATFORM_WINDOWS_DIR}/WindowsTime.h"
"${PLATFORM_WINDOWS_DIR}/WindowsTime.cpp"
"${PLATFORM_WINDOWS_DIR}/WindowsInput.h"
"${PLATFORM_WINDOWS_DIR}/WindowsInput.cpp"
)
# OpenGL
list(APPEND SOURCE_FILES_OPENGL
"${PLATFORM_OPENGL_DIR}/OpenGLRenderAPI.h"
"${PLATFORM_OPENGL_DIR}/OpenGLRenderAPI.cpp"
"${PLATFORM_OPENGL_DIR}/OpenGLGraphicsContext.h"
"${PLATFORM_OPENGL_DIR}/OpenGLGraphicsContext.cpp"
"${PLATFORM_OPENGL_DIR}/OpenGLVertexBuffer.h"
"${PLATFORM_OPENGL_DIR}/OpenGLVertexBuffer.cpp"
"${PLATFORM_OPENGL_DIR}/OpenGLIndexBuffer.h"
"${PLATFORM_OPENGL_DIR}/OpenGLIndexBuffer.cpp"
"${PLATFORM_OPENGL_DIR}/OpenGLShader.h"
"${PLATFORM_OPENGL_DIR}/OpenGLShader.cpp"
"${PLATFORM_OPENGL_DIR}/OpenGLVertexArray.h"
"${PLATFORM_OPENGL_DIR}/OpenGLVertexArray.cpp"
"${PLATFORM_OPENGL_DIR}/OpenGLTexture.h"
"${PLATFORM_OPENGL_DIR}/OpenGLTexture.cpp"
)
list(APPEND SOURCE_FILES ${SOURCE_FILES_OPENGL})
# Append Platform specific files
# Operating platform
IF (WIN32)
list(APPEND SOURCE_FILES ${SOURCE_FILES_WINDOWS})
# TODO: Linux
# TODO: MacOS
# TODO: Android
# TODO: IOS
ENDIF()
# Add source to this project's executable.
add_library(Cheetah SHARED ${SOURCE_FILES})
target_include_directories (Cheetah INTERFACE ${INCLUDES_DIR_PUBLIC})
target_include_directories (Cheetah PRIVATE ${ENGINE_DIR})
target_include_directories (Cheetah PRIVATE ${INCLUDES_DIR_PRIVATE})
# Add compile definitions
list(APPEND CHEETAH_COMP_DEFS
"CH_BUILD_DLL"
${RENDER_API}
)
target_compile_definitions(Cheetah PRIVATE ${CHEETAH_COMP_DEFS})
target_compile_definitions(Cheetah PUBLIC "$<$<CONFIG:DEBUG>:DEBUG>")
target_compile_definitions(Cheetah PUBLIC "$<$<CONFIG:DEBUG>:CH_ASSERT_ENABLED>")
set_target_properties(Cheetah PROPERTIES LINKER_LANGUAGE CXX)
# Copy dll to game project build folder
IF(${APPLICATION_NAME})
message("---------------------------------------------------------------------")
add_custom_command(
TARGET Cheetah
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${PROJECT_BINARY_DIR}/Cheetah.dll"
"${PROJECT_BINARY_DIR}/${APPLICATION_NAME}"
)
ENDIF()
#----------------------------------------------
# ADD OPENGL LIBRARY DEPENDENCY
#----------------------------------------------
find_package(OpenGL REQUIRED)
target_link_libraries(Cheetah ${OpenGL})
#----------------------------------------------
# ADD GLFW LIBRARY DEPENDENCY
#----------------------------------------------
set(GLFW_DIR "${LIB_DIR}/glfw")
set(GLFW_BUILD_EXAMPLES OFF CACHE INTERNAL "Build the GLFW example programs")
set(GLFW_BUILD_TESTS OFF CACHE INTERNAL "Build the GLFW test programs")
set(GLFW_BUILD_DOCS OFF CACHE INTERNAL "Build the GLFW documentation")
set(GLFW_INSTALL OFF CACHE INTERNAL "Generate installation target")
add_subdirectory("${GLFW_DIR}")
target_include_directories(Cheetah PRIVATE "${GLFW_DIR}/include")
target_link_libraries(Cheetah "glfw" "${GLFW_LIBRARIES}")
target_compile_definitions(Cheetah PRIVATE "GLFW_INCLUDE_NONE")
#----------------------------------------------
# ADD GLAD LIBRARY DEPENDENCY
#----------------------------------------------
set(GLAD_DIR "${LIB_DIR}/glad")
add_library("glad" "${GLAD_DIR}/src/glad.c")
target_include_directories("glad" PRIVATE "${GLAD_DIR}/include")
target_include_directories(Cheetah PUBLIC "${GLAD_DIR}/include")
target_link_libraries(Cheetah "glad" "${CMAKE_DL_LIBS}")
#----------------------------------------------
# ADD STB_IMAGE LIBRARY DEPENDENCY
#----------------------------------------------
set(STB_IMAGE_DIR "${LIB_DIR}/stb_image")
add_library("stb_image" "${STB_IMAGE_DIR}/stb_image.cpp")
target_include_directories("stb_image" PRIVATE "${STB_IMAGE_DIR}/include")
target_include_directories(Cheetah PUBLIC "${STB_IMAGE_DIR}/include")
target_link_libraries(Cheetah "stb_image" "${CMAKE_DL_LIBS}")
#---------------------------------------------
# ADD GLM LIBRARY DEPENDENCY
# --------------------------------------------
set(GLM_DIR "${LIB_DIR}/glm")
add_subdirectory("${LIB_DIR}/glm")
target_include_directories(Cheetah PRIVATE "${GLM_DIR}/glm")
target_link_libraries(Cheetah glm)
Game
# CMakeList.txt : CMake project for Cheetah, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.13)
set(SOURCE_DIR "${PROJECT_SOURCE_DIR}/Game/src")
list(APPEND SOURCE_FILES
"${SOURCE_DIR}/Main.cpp"
"${SOURCE_DIR}/GameLayer.cpp"
"${SOURCE_DIR}/GameLayer.h"
"${SOURCE_DIR}/GameObject.h"
"${SOURCE_DIR}/GameObject.cpp"
"${SOURCE_DIR}/Scene.h"
"${SOURCE_DIR}/Scene.cpp"
"${SOURCE_DIR}/GameScene.h"
"${SOURCE_DIR}/GameScene.cpp"
)
# Platform defines
IF (WIN32)
add_compile_definitions(CH_PLATFORM_WINDOWS)
ELSE()
# set stuff for other systems
ENDIF()
find_library( CHEETAH_LIB
NAMES Cheetah
HINTS "${CMAKE_BINARY_DIR}/Cheetah")
# Add source to this project's executable.
add_executable (Game ${SOURCE_FILES})
target_include_directories(Game PUBLIC "${PROJECT_SOURCE_DIR}/Cheetah/includes")
# TODO: Add tests and install targets if needed.
target_link_libraries(Game ${CHEETAH_LIB})
Top level CMakeList
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)
project ("Game")
set(APPLICATION_NAME "Game")
set(RENDER_API "OPENGL")
# Include sub-projects.
add_subdirectory("Cheetah")
add_subdirectory ("Game")
Anything that could point me in the right direction is much appreciated!
The include directories you specify for the Game target is a pretty short list, compared to what you specify for Cheetah:
target_include_directories(Game PUBLIC "${PROJECT_SOURCE_DIR}/Cheetah/includes")
It seems like you may need some of these Cheetah include directories for Game as well.
Note: If you are willing to re-organize the design of these two projects to always use the top-level CMake file, there is no need to use find_library() to find the Cheetah library. CMake already knows about the Cheetah target, as it was just created! The Game CMakeLists.txt file could look like this:
# CMakeList.txt : CMake project for Game, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.13)
set(SOURCE_DIR "${PROJECT_SOURCE_DIR}/Game/src")
list(APPEND SOURCE_FILES
"${SOURCE_DIR}/Main.cpp"
"${SOURCE_DIR}/GameLayer.cpp"
"${SOURCE_DIR}/GameLayer.h"
"${SOURCE_DIR}/GameObject.h"
"${SOURCE_DIR}/GameObject.cpp"
"${SOURCE_DIR}/Scene.h"
"${SOURCE_DIR}/Scene.cpp"
"${SOURCE_DIR}/GameScene.h"
"${SOURCE_DIR}/GameScene.cpp"
)
# Platform defines
# Note, this block can also be removed if target_compile_definitions
# is used for Cheetah instead.
IF (WIN32)
add_compile_definitions(CH_PLATFORM_WINDOWS)
ELSE()
# set stuff for other systems
ENDIF()
# Add source to this project's executable.
add_executable (Game ${SOURCE_FILES})
# TODO: Add tests and install targets if needed.
target_link_libraries(Game PUBLIC Cheetah)
The Cheetah target will carry all of the include directories along with it, so there is no need to list them all separately again. It can propagate the compile definitions as well, just use target_compile_definitions() when defining them for the Cheetah target.

Cmake managed C++

I have cli wrapper function which i am trying to configure in cmake. After i generate the project with cmake the generated .proj file does not have the property of clr support is set to no common languaage runtime support. below is my cmake file
# This is the root ITK CMakeLists file.
cmake_minimum_required(VERSION 2.8.9)
if(COMMAND CMAKE_POLICY)
cmake_policy(SET CMP0003 NEW)
endif()
set_target_properties(${TargetName} PROPERTIES COMPILE_FLAGS "/clr")
SET(LINK_LIBRARIES
D:\\2016\\RandomSlicing\\Processing\\lib\\obliquePlane.lib
)
# The header files
SET(HEADERS
ObliquePlaneWrapper.h
obliquePlane.h
)
# The implementation files
SET(SOURCES
ObliquePlaneWrapper.cpp
)
# Find ITK.
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
# Add this as include directory
INCLUDE_DIRECTORIES(
${CMAKE_SOURCE_DIR}
${SOURCE_PATH}
${VXL_INCLUDE_DIRS}
)
# Main library
#ADD_EXECUTABLE(obliquePlane ${HEADERS} ${SOURCES})
ADD_LIBRARY(ObliquePlaneWrapper SHARED ${HEADERS} ${SOURCES})
TARGET_LINK_LIBRARIES(ObliquePlaneWrapper ${LINK_LIBRARIES} ${ITK_LIBRARIES})
I manually set this property in the All_build project and the corresponding .proj file. When i build the project it is searching for the ObliquePlaneWrapper.dll which it should be generating. Is this a problem because of some flag not set for common language runtime support
You can manually supply Compile Flags to specific sources to be compiled with specific flags. This includes \CLR for Visual C++. See example here.
https://cmake.org/pipermail/cmake/2011-April/043773.html

CMake cannot find QXmlSimpleReader

I have a C++ header file which has the following lines:
#include <QXmlSimpleReader>
#include <QXmlDefaultHandler>
and my cmake has the following lines:
find_package(Qt5Core REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Xml REQUIRED)
When running CMake I get the following error message:
QXmlSimpleReader: No such file or directory
#include <QXmlSimpleReader>
What am I doing wrong?
For some reason it do not adds to project include dirs.
Add this one to your cmake
INCLUDE_DIRECTORIES( ${Qt5Xml_INCLUDE_DIRS} )
I guess you forgot to link against Qt5xml. A working example from the documentation for cmake 2.8.11 and later, modified to link against Qt5Xml:
cmake_minimum_required(VERSION 2.8.11)
project(testproject)
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
# Find the QtWidgets library
find_package(Qt5Xml)
# Tell CMake to create the helloworld executable
add_executable(helloworld WIN32 main.cpp)
# Use the Widgets module from Qt 5.
target_link_libraries(helloworld Qt5::Xml)

Include Eigen library for Xcode project via CMake/CMakeLists.txt

I've got the following CMakeLists.txt (in my "project" folder) file for my project.
# define new project
PROJECT(SETUPMARKERTEST)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8.0 FATAL_ERROR)
if(UNIX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
endif(UNIX)
# Set static build for GLFW
SET(BUILD_SHED_LIBS OFF)
ADD_SUBDIRECTORY(ext/glfw-3.1.1)
# Set shared lib build for the rest
SET(BUILD_SHARED_LIBS ON)
# Find dependencies
SET(EIGEN_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/ext/Eigen-3.1.2")
FIND_PACKAGE(OpenCV REQUIRED)
# Set header and source files
SET(MAR_Test_SOURCES
src/main.cpp
src/MarkerTracker.h src/MarkerTracker.cpp
src/PoseEstimation.h src/PoseEstimation.cpp
)
# define executable
ADD_EXECUTABLE(${PROJECT_NAME} ${MAR_Test_SOURCES})
# define additional include directories and linking targets
INCLUDE_DIRECTORIES("ext/glfw-3.1.1/include" ${EIGEN_INCLUDE_DIR} ${OpenCV_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS} glfw ${OPENGL_glu_LIBRARY} ${GLFW_LIBRARIES})
And my Eigen folder is in "project/ext/Eigen/3.1.2/Eigen/".
Somehow when I created my project for Xcode (in "project/buildXcode/" with Cmake .. -G "Xcode") and run it, Xcode throws me the error:
So I guess there is some error in my CMakeLists.txt, unfortunately I received that file and I'm new to CMake and thus didn't write it on my own nor am I very skilled with CMake.
Do you know what causes the error and can you fix the CMakeLists.txt that my project runs with the Eigen library?
Unfortunately it looks like windows is having no problem with this, whereas mac is bleating.
You just have to use
#include <Eigen/Dense>
instead of
#include <Eigen\Dense>
...pretty stupid error.

Not sure how to add opencv library to CMakeLists.txt

I hope someone can help me.
I have a simple CMakeLists.txt in order to build my project on Ubuntu. I'm using CMake 2.8.1 and at the moment this is the code:
cmake_minimum_required(VERSION 2.4.6)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/user/workspace)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)
# Set the build type. Options are:
# Coverage : w/ debug symbols, w/o optimization, w/ code-coverage
# Debug : w/ debug symbols, w/o optimization
# Release : w/o debug symbols, w/ optimization
# RelWithDebInfo : w/ debug symbols, w/ optimization
# MinSizeRel : w/o debug symbols, w/ optimization, stripped binaries
#set(ROS_BUILD_TYPE RelWithDebInfo)
rosbuild_init()
find_package(OpenCV 2)
#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
#uncomment if you have defined messages
#rosbuild_genmsg()
#uncomment if you have defined services
#rosbuild_gensrv()
#common commands for building c++ executables and libraries
#rosbuild_add_library(${PROJECT_NAME} src/example.cpp)
#target_link_libraries(${PROJECT_NAME} another_library)
#rosbuild_add_boost_directories()
#rosbuild_link_boost(${PROJECT_NAME} thread)
#rosbuild_add_executable(example examples/example.cpp)
#target_link_libraries(example ${PROJECT_NAME})
include_directories(${CMAKE_SOURCE_DIR}/include
${OpenCV_INCLUDE_DIRS})
rosbuild_add_executable (RosPub src/paste.cpp)
target_link_libraries (RosPub openni_driver usb-1.0 ${OpenCV_LIBS})
I need to add opencv libraries on my project. I have added them but i can't still get my code to work. its keeps posting me this error:
‘struct MyOpenNIExample::ImgContext’ has no member named ‘image’
there is a few of them.
after i added find_package(OpenCV REQUIRED to the CMakeLists.txt,
i get this error
Adjust CMAKE_MODULE_PATH to find FindOpenCV.cmake or set OpenCV_DIR to the
directory containing a CMake configuration file for OpenCV. The file will
have one of the following names:
OpenCVConfig.cmake
opencv-config.cmake
what shld i do? I am using Apple and using Ubuntu 10.04.
Since i need
#include "opencv2\opencv.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
i added
find_package(OpenCV 2),
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/user/workspace) and
include_directories(${CMAKE_SOURCE_DIR}/include
${OpenCV_INCLUDE_DIRS})
target_link_libraries (RosPub openni_driver usb-1.0 ${OpenCV_LIBS})
#include "opencv2\opencv.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
the include files above are included in the vision_opencv in ROS. so to include it, add the opencv dependency in the manifest file.
That would help.
Get FindOpenCV.cmake from above link and put it anywhere on your computer.
After cmake_minimum_required add line set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} [path to folder where you put FindOpenCV.cmake])
Add find_package(OpenCV) to your CMakeLists.txt
On this step you can check for OpenCV_FOUND and other OpenCV variables in your CMakeLists.txt