[CMake] how do I test if a library target is static or shared?

Mike Jackson mike.jackson at bluequartz.net
Mon Sep 22 20:41:51 EDT 2008


Not totally sure what you are after BUT one possible implementation is  
something like the following.

If you have a Library A that you want to include in Executable B and  
Both are there own projects based on CMake then there is "some"  
integration that can happen.

So for library A you have a normal CMakeLists.txt file for that  
project. Then for the executable project you simply "include" the  
CMakeLists.txt file from library A.

If the "Library A" project you have:
add_library (A SHARED ${sources})

Then in the executable project you have something like:

add_executable (myexec ${sources} )
target_link_libraries(myexec A)

When you build the executable project CMake will automatically build  
the "A" library first. This is one way to achieve this. ( I do this in  
a few of my projects ).



Some helper code that might be handy:
# This code goes in the Executable projects CMakeLists.txt file
# --- Get the LibA Project by looking for a folder called "src" in the  
LibraryA folder
GET_FILENAME_COMPONENT(CURRENT_SOURCE_PARENT $ 
{CMAKE_CURRENT_SOURCE_DIR} PATH)
FIND_PATH(A_SOURCE_DIR src
   "${CURRENT_SOURCE_PARENT}/LibraryA"
)
IF (NOT A_SOURCE_DIR)
     MESSAGE(FATAL_ERROR "Could not find LibraryA Directory. Please  
set the directory")
ENDIF(NOT MXA_SOURCE_DIR)

Now. if you can not do something like the above or you don't want to  
tie the projects together like that then you can "configure" a header  
file from LibraryA that can be used in the executable project to  
"relay" information about the library, like if it was built as a  
shared or static library. Again, this is also done in lots of projects.

The last way to "relay" information would be to have CMake write the  
information into a simple text file that the executable project can  
then parse.

Keep asking.. we'll help as much as we can.
---
Mike Jackson - Principal Software Engineer
www.bluequartz.net




On Sep 22, 2008, at 5:46 PM, Aleksander Demko wrote:

> Dang, that won't work for me. I'm in effect trying to make my own
> EXPORT like command (export doesn't maintain C flags or other stuff I
> need) so I need to be able to gleam a bunch of info from the library
> targets dynamically.
>
> Basically I want to quickly build one library in one cmake session,
> than import it in another (without have to write custom Find-.cmake
> files etc). Maybe this exists already in cmake, but I can't seem to
> find it.
>
>
> On Mon, Sep 22, 2008 at 4:32 PM, Mike Jackson
> <mike.jackson at bluequartz.net> wrote:
>> You will probably have to set your own variable into the cache and  
>> then
>> check that variable later on in your scripts
>>
>>
>>
>> On Sep 22, 2008, at 5:23 PM, Aleksander Demko wrote:
>>
>>> On Mon, Sep 22, 2008 at 4:15 PM, Mike Jackson
>>> <mike.jackson at bluequartz.net> wrote:
>>>>
>>>> Assuming you mean a library that you are creating the default is  
>>>> static.
>>>> If
>>>> you want a dynamic library then use the "SHARED" keyword:
>>>>
>>>> add_library (mylib SHARED libsource.cpp)
>>>>
>>>
>>> I understand that. But later in my script I want to check if mylib  
>>> is
>>> shared or static with an IF command. I want the command to know if  
>>> the
>>> add_library was the default static, shared or shared because
>>> BUILD_SHARE_LIBS is true.
> _______________________________________________
> CMake mailing list
> CMake at cmake.org
> http://www.cmake.org/mailman/listinfo/cmake



More information about the CMake mailing list