[CMake] un circle dependency

Michael Hertling mhertling at online.de
Fri Nov 12 09:11:22 EST 2010


On 11/12/2010 02:47 PM, luxInteg wrote:
> On Thursday 11 November 2010 21:55:31 Alexander Neundorf wrote:
>> On Thursday 11 November 2010, luxInteg wrote:
>>> Greetings
>>>
>>> I am learning cmake.
>>>
>>>   I think I now know how to generate libraries and executables but  I
>>>   have
>>>
>>> not yet grasped  how  the tree and   directory structure is handled and
>>> by conseqence how one navigates therein.
>>>
>>>
>>> I have this problem.  Lets say my   build tree has these directories
>>>
>>> L---$originalSourceTree/build/lib
>>> M---$originalSourceTree/build/bin
>>>
>>> libraries   say libA.so libB.a are in L.  I want to generate  
>>> executables A.bin and B.bin in M but
>>> they each need  to link to  newly generated  libraries  libA.so libB.a
>>>
>>>
>>> a) how do I direct cmake  (in the CMakeLists.txt/base directory) to build
>>> the libraries in ( L )above   first  -i.e. before atempting to do finds
>>> of nonexistant libraries  ?
>>
>> The add_library() commands must be before the add_executable() commands,
>> otherwise the libraries "A" and "B" are not yet know when you do
>> target_link_libraries(exe A B)
>>
>> Beside that, the dependencies in the generated makefiles are complete, so
>> whenever you do a build, cmake makes sure that the libraries are up-to-date
>> before it builds the executable.
>>
>>> b) if I   insert lines such 'find_library(LIB  libA  path ../lib'   in
>>> the CmakeLists.txt in the base directory  this  results in an error  so
>>> what does one insert?
>>
>> find_library() is not necessary, you can simply use the names of the
>> library targets when linking against them.
> 
> thanks  for the help,
> 
> I added 
> 
> ADD_DEPENDENCIES(libA  ../path/to/A_exec )
> 
> to the CMakeLists.txt in  ~/lib directory (i.e.L)

Besides the fact that this line would establish a dependency *of* libA
*on* A_exec - from your initial post, I cannot see such a necessity -
it actually does nothing as "../path/to/A_exec" is no logical target
in your project, I suppose, but ADD_DEPENDENCIES() operates on such
targets only. BTW, are you sure you have a circular dependency, i.e.
A_exec depends on libA depends on A_exec?

> ---AND
> add_executable(A.exec fileA.c)
> target_link_libraries(A.exec  libA )
> 
> to the CMakeLists.txt  in the ~/bin directory (i.e.M) a
> 
> and it seems to have worked

The TARGET_LINK_LIBRARIES() is absolutely right and also sufficient, IMO.

> -----A   related problem is   described below
> 
> I have this  file:- (orig_A_exec.out) in in the ~/bin (of the source tree)
> after building A_exec  as explained above   I tried running the shall command 
> below
> 
> EXECUTE_PROCESS(COMMAND ./A_exec > new_A_exec.out  
> - diff orig_A_exec.out   new_A_exec.out )
> 
> but I did not see      neither new_A_exec.out  nor orig_A_exec.out in ~/bin in 
> the build directory (i.e.M) i.e nothing happened.
> 
> 
> I am using a bash  shell.  some guidance on how to execute bash  commands in 
> cmake  would be appreciated.

EXECUTE_PROCESS() is run at configuration time, i.e. when CMake runs,
but at that time, your A_exec hasn't been built, yet, or stems from a
previous build in which case it is outdated, so you don't want to run
it in order to check its output. If you want to run an executable after
it has been built use ADD_CUSTOM_COMMAND(TARGET ...) or ADD_TEST() with
"make test" or CTest as suggested recently for your BouncyBall example.

Regards,

Michael


More information about the CMake mailing list