[CMake] How to specify library dependencies?

Michael Jackson mike.jackson at bluequartz.net
Mon Nov 24 17:39:57 EST 2008

On Nov 24, 2008, at 5:08 PM, Robert Dailey wrote:

> On Mon, Nov 24, 2008 at 4:02 PM, Michael Jackson <mike.jackson at bluequartz.net 
> > wrote:
> On Nov 24, 2008, at 4:29 PM, Robert Dailey wrote:
> On Mon, Nov 24, 2008 at 2:41 PM, Michael Jackson <mike.jackson at bluequartz.net 
> > wrote:
> typically you do:
> add_executable(main  main.cpp)
> target_link_libraries(main a)
> and CMake _usually_ picks the correct library for the given platform  
> (a.lib, a.so, a.dylib... )
> Is that what you were asking?
> Yes, you did answer my question exactly, however I did not specify  
> the more complex issue.
> Some libraries we're using have different library names depending on  
> the platform. For example:
> a_windows.lib
> a_linux.o
> This is why I believed I would need the conditional logic. What  
> would you do in this case? Thanks for your help.
> I guess it would depend on if those other libraries were being  
> compiled in the same project as the current one.
> Basically when you use target_link_libraries (EXE  [lib1] [lib2]... )
> you need to supply everything between the platform prefix and the  
> platform suffix.
> So, if your library on Windows is a_windows.lib you would supply  
> "a_windows". If your library is liba_linux.so then supply "a_linux"  
> on linux.
> So, in practice you have:
> set(lib_a_name "a")
> if (WINDOWS)
>  set(lib_a_name "a_windows")
> elseif(APPLE)
>  set(lib_a_name "a_osx")
> elseif(LINUX)
>  set(lib_a_name "a_linux")
> endif()
> target_link_Libraries(exe ${lib_a_name})
> Thanks everyone for the help. Michael, in your example code, is  
> "if(WINDOWS)" pseudocode? Is WINDOWS a valid usage here? If not,  
> what would the actual conditional look like to check for windows/mac/ 
> linux? Thanks.

One of the better sources to look at is FindBoost.cmake:

Here are some CMake Predefined variables that you will find useful:

You can use the cmake --help-variable [variable] for more information  
if you have the command line handy.

You may also might need to parse the contents of the CMAKE_SYSTEM  
variable or the CMAKE_HOST_SYSTEM variable. Look at the docs for the  
differences between them.

CMAKE_HOST_SYSTEM_NAME and CMAKE_SYSTEM_NAME may also come in handy.
Mike Jackson                  mike.jackson at bluequartz.net
BlueQuartz Software                    www.bluequartz.net
Principal Software Engineer                  Dayton, Ohio

More information about the CMake mailing list