-2

I'm using cmake to build an executable to run on an Intel Galileo board.

My question is how do I include the external mraa library in the build process.

Mraa library

  1. When I download the library from git (1) do I need to build it as described here

Mraa compiling instructions

  1. What do I need to put in my CMakeLists.txt file to pick up the library?

This is what I have thus far in my CMakeLists.txt file but I believe it is incorrect.

### MRAA ###
add_subdirectory(mraa-master/src)
file(GLOB mraa_SRC
    "mraa-master/src/*.c"
)

include_directories( "${PROJECT_SOURCE_DIR}/mraa-master/include"  )
add_library( ${MRAA_LIBRARY_NAME} SHARED  ${mraa_SRC}  )

Thank you

cmake_minimum_required(VERSION 2.8)

MESSAGE( STATUS "Starting build process")

SET( CMAKE_VERBOSE_MAKEFILE on )

if (CMAKE_BUILD_TYPE EQUAL "Debug")
MESSAGE(STATUS "Building in debug mode")
elseif (CMAKE_BUILD_TYPE EQUAL "Release")
MESSAGE(STATUS "Building in release mode")
endif()

SET(PROJECT_NAME "TestProject")
SET(APPLICATION_NAME "TestApplication")
SET(SAFE_STRING_LIBRARY "SafeString")
SET(APPLICATION_LIBRARY "Applibrary")

PROJECT( ${PROJECT_NAME} )

MESSAGE( STATUS "PROJECT: " ${PROJECT_NAME} )


SET(WRSDK_PATH "$ENV{WINDRIVER_SDK_DIR}")
IF (WRSDK_PATH)
else()
SET(WRSDK_PATH /opt/windriver/wrlinux/5.0-intel-quark/)
endif()

SET(APPLIBRARY_NAME  ${APPLICATION_LIBRARY} )
SET(SAFE_STRING_LIBRARY_NAME ${SAFE_STRING_LIBRARY} )

### SAFE STRING ###
add_subdirectory(SafeStringStaticLibrary/safeclib) 
file(GLOB safestring_SRC
"SafeStringStaticLibrary/safeclib/*.c" 
)

include_directories( "${PROJECT_SOURCE_DIR}/SafeStringStaticLibrary /safeclib"  ) 
add_library( ${SAFE_STRING_LIBRARY_NAME} SHARED  ${safestring_SRC}  ) 

include(ExternalProject)
ExternalProject_Add(mraa
    GIT_REPOSITORY https://github.com/intel-iot-devkit/mraa.git
    GIT_TAG        v0.8.0
    UPDATE_COMMAND ""
    INSTALL_COMMAND "" )


file(GLOB app_SRC
"classes/*.cpp"
"Logger.cpp"
"sqlite3.c"
"shell.c"
)

add_library( ${APPLIBRARY_NAME} SHARED ${app_SRC})
include_directories(  "${CMAKE_SOURCE_DIR}"     "${CMAKE_SOURCE_DIR}/SafeStringStaticLibrary/include" )

add_executable( ${APPLICATION_NAME} ${APPLICATION_NAME}.cpp

include_directories(  "${CMAKE_SOURCE_DIR}"   "${CMAKE_SOURCE_DIR}/SafeStringStaticLibrary/include"  "${CMAKE_SOURCE_DIR}/classes")

TARGET_LINK_LIBRARIES(  ${APPLICATION_NAME}  ${APPLIBRARY_NAME} ${SAFE_STRING_LIBRARY_NAME} -lrt -lpthread -lgcov -ldl)
Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
riverrock
  • 141
  • 3
  • 12
  • here is your solution check out here 1) http://stackoverflow.com/questions/24570916/add-external-libraries-to-cmakelist-txt-c – BrotskyTv Sep 29 '15 at 13:29
  • @riverrock: For build external library within your project, you can use [ExternalProject_Add](https://cmake.org/cmake/help/v3.0/module/ExternalProject.html). If you are new in CMake and using of this function is not clear for you, I recommend you to build external library outside of your project, and link with it using its full path. There are many resources which describes how to link with external library. – Tsyvarev Sep 29 '15 at 20:10
  • Hi @Tsyvarev , I built the mraa library outside of my project using the instructions provided here [link](http://iotdk.intel.com/docs/master/mraa/building.html). Then I included these two lines in my CMakeLists.txt file. `include_directories( "${PROJECT_SOURCE_DIR}/mraa-master/include" ) TARGET_LINK_LIBRARIES( ${APPLICATION_NAME} ${CMAKE_SOURCE_DIR}/mraa-master/build/src/libmraa.so)` . The error that I'm getting is **mraa-master/build/src/libmraa.so: could not read symbols: File in wrong format** – riverrock Oct 05 '15 at 11:29
  • If you crosscompile your project, then you should also crosscompile `mraa` library. – Tsyvarev Oct 05 '15 at 12:19
  • @Tsyvarev the mraa library is a cmake-enabled library. How do I include it within my cmake enabled project so that it gets cross compiled with the rest of the code? Thanks – riverrock Oct 05 '15 at 13:31
  • If you build `mraa` library outside of your project, just use same environment for build `mraa`. Probably, you need to additionally set `BUILDARCH` variable for `mraa` library build. Without knowing way, how you build yout project, it is difficult for give more concrete advice. – Tsyvarev Oct 05 '15 at 13:44
  • @Tsyvarev Could I use this code in my project's cmakelists.txt file? `include(ExternalProject) ExternalProject_Add(mraa GIT_REPOSITORY https://github.com/intel-iot-devkit/mraa.git GIT_TAG v0.8.0 )` – riverrock Oct 05 '15 at 13:57
  • You may try. Unless your project has common options with `mraa` one, it should work. But it seems that you should set `BUILDARCH` in any case. – Tsyvarev Oct 05 '15 at 14:03
  • @Tsyvarev I've posted my cMakeLists.txt file for the project. You can see it creates an executable called "TestApplication" and a shared library called libApplibrary.so. You can see I build the safestring library and link it on the last line. I've brought in the mraa library using ExternalProject_Add() with no errors. What should I include in TARGET_LINK_LIBRARIES( ) to link with the mraa library? Thanks – riverrock Oct 05 '15 at 16:05
  • You should use full path to `mraa` library to build with it. Something like `${CMAKE_CURRENT_BINARY_DIR}/mraa-prefix/src/mraa-build/src/libmraa.so`. My previous comment actually correct for the case, when you build `mraa` using `add_subdirectory(mraa)` way. command (previously you should downloaded mraa sources into `mraa` directory in source tree). In case of ExternalProject_Add I am unsure that `mraa` will be crosscompiled as main project. – Tsyvarev Oct 05 '15 at 20:52
  • @Tsyvarev ok thanks I will not use `ExternalProject_Add()`. Three more questions if you don't mind :) 1) what directory should I put in `add_subdirectory()` ? 2) Do I need to use `include_directories()` for the header files also? 3) Should `TARGET_LINK_LIBRARIES( )` look like this? `TARGET_LINK_LIBRARIES( ${APPLICATION_NAME} ${APPLIBRARY_NAME} ${SAFE_STRING_LIBRARY_NAME} ${CMAKE_CURRENT_BINARY_DIR}/mraa-prefix/src/mraa-build/src/libmraa.so -lrt -lpthread -lgcov -ldl)` – riverrock Oct 06 '15 at 09:28
  • You deleted almost everything from your question, why did you do that? If you have accedentally pressed `Submit` button while edited, you can restore original content of the question from the history of edits. – Tsyvarev Oct 06 '15 at 17:59

1 Answers1

-1

The simplest way for build your project alongside with 3d party project is add_subdirectory() that subproject. Approach below implies that you (manually) download(git clone) sources of mraa project into mraa-lib subdirectory of your project's sources.

CMakeLists.txt:

cmake_minimum_required(VERSION 2.8)

MESSAGE( STATUS "Starting build process")

# Configure subproject before definition of variables for main project.
#
# Subproject defines *mraa* library target.
add_subdirectory(mraa-lib)

# Before installation, public mraa headers are contained under *api* subdirectory.
include_directories(mraa-lib/api)

... # create executable ${APPLICATION_NAME}

# Linking with mraa library is straightforward.
target_link_libraries(${APPLICATION_NAME} mraa)

That way mraa subproject will be configured, built and installed alongside with your project. So, if you cross-compile your project(using toolchain file, or inside IDE), the subproject will also be cross-compiled.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153