1

I know that CMake automatically somehow figures out the dependency tree. It will build things in the right order normally.

I decided to use options and that has caused it to guess poorly.

I have a variant of this SO question following:

/CMakeLists.txt
  /src/CMakeLists.txt
  /testpackage1/CMakeLists.txt
  /testpackage2/CMakeLists.txt

In my case, mylib has two kinds -- a static and a dynamic.

OPTION(MYLIB_BUILD_STATIC "Build static library" TRUE)
IF(MYLIB_BUILD_STATIC)
    ADD_LIBRARY(mylib_static STATIC ${MyLib_SRCS})
    ...
    SET_TARGET_PROPERTIES(mylib_static PROPERTIES OUTPUT_NAME "mylib")
    SET(MYLIB_LIB_TARGET mylib_static)
ENDIF(MYLIB_BUILD_STATIC)


OPTION(MYLIB_BUILD_SHARED "Build shared library" TRUE)
IF(MYLIB_BUILD_SHARED)
    ADD_LIBRARY(mylib SHARED ${MyLib_SRCS})
    ...
    SET(MYLIB_LIB_TARGET mylib)
ENDIF(MYLIB_BUILD_SHARED)

At this point ${MYLIB_LIB_TARGET} has either mylib or mylib_static in it

In the unit test packages....

OPTION(MYLIB_BUILD_TEST "Build test program." TRUE)
IF(MYLIB_BUILD_TEST)
    INCLUDE_DIRECTORIES(${MYLIB_SOURCE_DIR})
    ADD_EXECUTABLE(test_mylib main.c)
    IF(NOT MYLIB_LIB_TARGET)
        MESSAGE(FATAL_ERROR "MYLIB_LIB_TARGET is not specified")
    ENDIF() 
    TARGET_LINK_LIBRARIES(test_mylib ${MYLIB_LIB_TARGET})
ENDIF(MYLIB_BUILD_TEST)

In the parent:

ADD_SUBDIRECTORY("src")
ADD_SUBDIRECTORY("testpackage1")
ADD_SUBDIRECTORY("testpackage2")

What I get is: MYLIB_LIB_TARGET is not specified from the unit test project

I think it's because the options are messing with the automatic dependency tree logic in CMake.

Community
  • 1
  • 1
101010
  • 14,866
  • 30
  • 95
  • 172
  • 2
    Where you state "At this point.." what value has `MYLIB_LIB_TARGET`? Can you check with `message("actual value: ${MYLIB_LIB_TARGET}")` and tell us the output? Maybe the same before you check in in the unit test. – usr1234567 Feb 28 '15 at 06:48
  • It tends to run the test projects first. I get the FATAL_ERROR and it never gets into the part where it says "At this point". – 101010 Mar 06 '15 at 02:25
  • Have you included the subdirectory too early? Difficult to tell. Can you reduce your project to the bare minimum and post more complete files? – usr1234567 Mar 06 '15 at 09:19
  • Probably you need to call SET(MYLIB_LIB_TARGET mylib PARENT_SCOPE) that will ensure the variable exists in your test packages. It looks to me like you're setting a CMake variable in one file and expecting it to exits in a parallel file. Variable scope exists in the current CMakeLists.txt and all subdirectory's CMakeLists.txt files. As stated above, message() is you friend here. – StAlphonzo Mar 18 '15 at 11:25

0 Answers0