[CMake] Custom command with included header file

Reza Housseini reza.housseini at gmx.ch
Mon Sep 24 07:35:43 EDT 2012


On Fri, Sep 21, 2012 at 2:26 PM, Reza Housseini <reza.housseini at gmx.ch> wrote:
> Hello
> I want to add my own library libsieve.so with the header file Sieve.h
> to the custom target. I have the following setup:
>
> find_program(MKOCTFILE_EXECUTABLE mkoctfile)
> if(NOT MKOCTFILE_EXECUTABLE)
>   message(SEND_ERROR "Failed to find mkoctfile, is it in your $PATH?")
> endif()
>
> set(OCT_FILE "multidecksieve.oct")
> set(OCT_SRC "${CMAKE_CURRENT_SOURCE_DIR}/multidecksieve.cpp")
> set(CPPLIB_DIR "${CMAKE_SOURCE_DIR}/../core/build")
> set(CPPLIB_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/../core")
>
> find_library(CPPLIB_SIEVE_LIBRARY NAMES sieve PATHS ${CPPLIB_DIR})
> find_path(CPPLIB_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/../core)
>
> if ("${CPPLIB_SIEVE_LIBRARY}" STREQUAL "CPPLIB_SIEVE_LIBRARY-NOTFOUND")
>   message(FATAL_ERROR "One of the libraries wasn't found!")
> endif ()
>
> include_directories(CPPLIB_INCLUDE_DIR)
>
> add_custom_command(
>   OUTPUT ${OCT_FILE}
>   COMMAND ${MKOCTFILE_EXECUTABLE} "${OCT_SRC}"
>   DEPENDS ${CPPLIB_INCLUDE_DIR}/Sieve.h ${CPPLIB_SIEVE_LIBRARY}
>   COMMENT "Generating ${OCT_FILE}"
>   VERBATIM)
>
> add_custom_target(multidecksieve ALL
>   DEPENDS ${OCT_FILE})
>
> But it gives me the error that it can't find the Sieve.h file:
> fatal error: Sieve.h: No such file or directory
>
> What is the correct way to do it?
>
> Thanks and best wishes
> Reza

I was able to make it to work:

find_program(MKOCTFILE_EXECUTABLE mkoctfile)
if(NOT MKOCTFILE_EXECUTABLE)
  message(SEND_ERROR "Failed to find mkoctfile, is it in your $PATH?")
endif()

## Compile the octfile
set(OCT_CXXFLAGS "-O2 -Wall -Wextra -std=c++0x")
set(ENV{CXXFLAGS} ${OCT_CXXFLAGS})
set(OCT_FILE "${PROJECT_BINARY_DIR}/multidecksieve.oct")
set(OCT_SRC ${CMAKE_CURRENT_SOURCE_DIR}/multidecksieve.cpp)
set(CPPLIB_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../core/build)
set(CPPLIB_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../core")

find_library(CPPLIB_SIEVE_LIBRARY NAMES sieve PATHS ${CPPLIB_DIR})

if ("${CPPLIB_SIEVE_LIBRARY}" STREQUAL "CPPLIB_SIEVE_LIBRARY-NOTFOUND")
  message(FATAL_ERROR "One of the libraries wasn't found!")
endif ()

set(CPPLIB_LIBRARIES "${CMAKE_CURRENT_SOURCE_DIR}/../core/build")
set(CPPLIB_HINT_INCLUDE_DIR ${CPPLIB_DIR})

set(DEPENDENCIES ${OCT_SRC} ${CPPLIB_SIEVE_LIBRARY})
add_custom_command(
  OUTPUT ${OCT_FILE}
  DEPENDS ${DEPENDENCIES}
  COMMAND ${MKOCTFILE_EXECUTABLE}
  ARGS ${OCT_SRC}
  ARGS -I${CPPLIB_INCLUDE_DIR} -L${CPPLIB_DIR} -lsieve
  COMMENT "Generating ${OCT_FILE}"
  VERBATIM)

add_custom_target(multidecksieve ALL
  DEPENDS ${OCT_FILE})

It compiles fine, but when I run the created file (in GNU Octave) I
get an error message saying the library libsieve.so is not found
although it exists on the provided path. What is the problem here?

Thanks for any answer
Reza


More information about the CMake mailing list