[Cmake] generating executables

Andy Cedilnik andy . cedilnik at kitware . com
27 Aug 2003 07:46:01 -0400


Hi Mónica,

Things should be defined in the right order and no lib in front of
library name. Here we go:

ADD_LIBRARY(myexample ${MYEXAMPLE_SOURCE_LIST})

LINK_LIBRARIES(myexample) # No lib

ADD_EXECUTABLE(demo demo.cc)

What you should really do is use TARGET_LINK_LIBRARIES:

ADD_LIBRARY(myexample ${MYEXAMPLE_SOURCE_LIST})

ADD_EXECUTABLE(demo demo.cc)

TARGET_LINK_LIBRARIES(demo myexample)

So, demo will be linked to myexample.

			Andy


On Wed, 2003-08-27 at 06:58, Mónica Hernández Giménez wrote:
> Hi:
> 
> I have a problem with the generation of my executables using cmake.
> 
> I have a project called myexample with folders include, src and applications.
> 
> The CMakeLists.txt from the main folder is:
> 
> # Name of project
> 
> PROJECT (MYEXAMPLE)
> 
> SUBDIRS(src applications)
> 
> ***************************************************************************************
> ***************************************************************************************
> 
> The CMakeLists.txt from the src folder is:
> 
> # Include directories (*.h)
> 
> INCLUDE_DIRECTORIES (${MYEXAMPLE_SOURCE_DIR}/include)
> 
> # Source directories (*.cc)
> 
> FILE(GLOB MYEXAMPLE_SOURCE_LIST ${MYEXAMPLE_SOURCE_DIR}/src/*.cc)
> 
> # Building myexample library
> 
> SET(LIBRARY_OUTPUT_PATH ${MYEXAMPLE_BINARY_DIR}/lib)
> ADD_LIBRARY(myexample ${MYEXAMPLE_SOURCE_LIST})
> 
> *******************************************************************************************
> *******************************************************************************************
> 
> The CMakeLists.txt from the applications folder is:
> 
> # Include directories (*.h)
> 
> INCLUDE_DIRECTORIES(${MYEXAMPLE_SOURCE_DIR}/include)
> 
> # Source directories (*.cc)
> 
> FILE(GLOB MYEXAMPLE_SOURCE_LIST ${MYEXAMPLE_SOURCE_DIR}/applications/*.cc)
> 
> #Link to myexample library
> 
> LINK_DIRECTORIES (${MYEXAMPLE_BINARY_DIR}/lib)
> 
> LINK_LIBRARIES(libmyexample)
> 
> # Building myexample library
> 
> SET(LIBRARY_OUTPUT_PATH ${MYEXAMPLE_BINARY_DIR}/lib)
> SET(EXECUTABLE_OUTPUT_PATH ${MYEXAMPLE_BINARY_DIR}/bin)
> 
> ADD_EXECUTABLE(demo demo.cc)
> 
> ADD_LIBRARY(myexample ${MYEXAMPLE_SOURCE_LIST})
> 
> ***********************************************************************************
> ***********************************************************************************
> 
> If I try to compile it, when building the executables I get this error:
> 
> Building executable /extra/local/usrsrc/monica/examples/MyExample/bin/demo...
> /usr/bin/ld: cannot find -llibmyexample
> collect2: ld returned 1 exit status
> gmake[3]: *** [/extra/local/usrsrc/monica/examples/MyExample/bin/demo] Error 1

Unfortunately globbing like what you doing is dangerous thing. The
problem is that cmake is not called every time you build, which means
you can add or remove files and the build system will not notice.

At this point doing *.cc for sources is a dangerous thing.

> If I have several *.cc in my applications folder and I want to build all of 
> them, is there an alternative way instead of doing ADD_EXECUTABLE *.cc by 
> *.cc to do the whole thing?


				Andy