[CMake] How to add a libaray for another libaray?

James Bigler jamesbigler at gmail.com
Wed Dec 10 15:34:17 EST 2008


There are a couple of things that either you have done wrong or I don't
understand.  This is how I would implement things:

core/CMakeLists.txt

SET(SRC_LIST ConfigHolder.cpp DictItem.cpp Reciter.cpp ForgetCurve.cpp
Task.cpp Dict.cpp Manager.cpp WordList.cpp)
ADD_LIBRARY(core ${SRC_LIST})

ui/CMakeLists.txt

# You can also use CMAKE_SOURCE_DIR if FREERECITE_SOURCE_DIR is the top of
your CMake tree
ADD_DIRECTORIES(${CMAKE_SOURCE_DIR}/core)

ADD_LIBRARY(ui Cui.cpp)
# Tell CMake that ui needs to link agains core
TARGET_LINK_LIBRARIES(ui core)

CMakeLists.txt
ADD_EXECUTABLE(myprogram main.cpp)
# myprogram depends on ui library
TARGET_LINK_LIBRARIES(myprogram ui)

You don't need to use LINK_DIRECTORIES, because CMake knows where core was
built and how to link it against ui.

James

On Tue, Dec 9, 2008 at 3:40 PM, Kermit Mei <kermit.mei at gmail.com> wrote:

> Hello, My project is layout like this:
>
> Linux-cmd$ tree
>
> .
> |-- CMakeLists.txt
> |-- core
> |   |-- CMakeLists.txt
> |   |-- ... ... ...
> |   |-- (Some source files to implement the internal function.)
> `-- ui
>  |-- CMakeLists.txt
>  |-- ... ... ...
>  |-- (Some source files to impliment the UI)
> |-- main.cpp
>
>
> Their dependences is     main.cpp -> ui -> core
>
> What's wrong with my CMakeLists.txt so that the files under the directory
> "ui" can't
> find the headers under the directory "core".
>
> 1.ui/CMakeLists.txt :
>
> # Make sure the compiler can find include files from our ui library.
> INCLUDE_DIRECTORIES(${FREERECITE_SOURCE_DIR}/core)
>
> # Make sure the linker can find the ui library once it is built.
> LINK_DIRECTORIES(${FREERECITE_BINARY_DIR}/core)
>
> TARGET_LINK_LIBRARIES(Cui.o core)
> ADD_LIBRARY(ui Cui.cpp)
>
>
>
> 2. core/CMakeLists.txt:
>
> # Set a variable $SRC_LIST to declare the source files.
>
> SET(SRC_LIST ConfigHolder.cpp DictItem.cpp Reciter.cpp ForgetCurve.cpp
> Task.cpp Dict.cpp Manager.cpp WordList.cpp)
>
>
> # Create a library called "Core" which includes the source files.
> # The extension is already found. Any number of sources could be listed
> here.
>
> ADD_LIBRARY(core ${SRC_LIST})
>
> Thanks.
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20081210/f395ad18/attachment.htm>


More information about the CMake mailing list