[CMake] Executable using a library (.a or .so)

houssen houssen at ipgp.fr
Sat Nov 14 10:05:10 EST 2015


is this library required to be separate from the executable?
No in this specific case, but, the question still holds from a general 
comprenhension of CMake (I'd like to get !...) and the answer may be yes 
in futur cases.

I tested your proposal (need to add target_include_directories to find 
headers) :
# in MyLib/CmakeLists.txt
     add_library( MyLib <STATIC/SHARED; default STATIC> libsource1.c 
libsource2.cpp )
# in MyExe/CmakeLists.txt
     add_exectubable( MyExe exesource1.c )
     target_include_directories(myExe PUBLIC ../myLib/hdr)
     target_link_libraries( MyExe MyLib )

This is working, so OK I can use that. But what if the user wants to 
build myLib as a .a or a .so ? It seems it not possible to change that 
from ccmake (I've just checked). It should be possible when using 
Find*.cmake, no ?

... But, precisely, when typing target_include_directory you need to 
know the path to headers directory (+ path to libraries directory ?): 
this is why I felt like "Okay, I need to write a FindMyLib.cmake to 
handle this". My understanding is that a  (internal or external) library 
is a library, so, it should be managed with a FindMyLib.cmake. Am I 
correct ? (I try to understand the "CMake way" to do thing - I am more 
an autotools user, so, I just try to understand CMake)

Franck

Le 2015-11-13 18:53, J Decker a écrit :
> On Fri, Nov 13, 2015 at 9:51 AM, J Decker <d3ck0r at gmail.com> wrote:
>> is this library required to be separate from the executable?
>> If not; it's far easier...
>>
> # in MyLib/CmakeLists.txt
>> add_library( MyLib <STATIC/SHARED; default STATIC> libsource1.c 
>> libsource2.cpp )
>
> # in MyExe/CmakeLists.txt
>> add_exectubable( MyExe exesource1.c )
>> target_link_libraries( MyExe MyLib )
>>
>
> theose can be spread ito sepatate CMakeLists... with a root
> CmakeLists.txt like...
>
> ---
> cmake_minimum_version(something)
>
> add_subdirectory( mylib )
> add_subdirectory( myExe)
>
>
>
>>
>> On Fri, Nov 13, 2015 at 9:28 AM, houssen <houssen at ipgp.fr> wrote:
>>> Hello,
>>>
>>> How to write a library (myLib as .a or .so) that can be used from 
>>> an
>>> executable (myExe) ?
>>>
>>> I read the CMake doc but, obviously, I still miss some major points 
>>> !... My
>>> understanding is that, I need :
>>> - to write a FindMyLib.cmake for myExe to find myLib (= finding
>>> include/library directories, and, libraries to link with)
>>> - to use find_package from myExe to find myLib
>>>
>>> I tried to write a mini-project (attached to this mail) to do that 
>>> but it
>>> doesn't work: find_package doesn't find FindMyLib.cmake although
>>> CMAKE_MODULE_PATH has been updated.
>>>
>>> Can somebody help me on making this project to work ? (not able to 
>>> find
>>> FindMyLib.cmake, not sure to know the "good" way to write 
>>> FindMyLib.cmake,
>>> others... - I would appreciate any remarks to explain me the "good" 
>>> way to
>>> do thing)
>>>
>>> Thanks,
>>>
>>> Franck
>>>
>>> Note: I set CMAKE_MODULE_PATH in the CMakeLists.txt of myLib (doing 
>>> that
>>> from myExe sounds not relevant: if you know where the package is, 
>>> no need to
>>> call find_package).
>>> Note: my understanding is that when this will work, I would be 
>>> possible to
>>> build myLib as a .a or a .so, am I correct ?
>>> --
>>>
>>> Powered by www.kitware.com
>>>
>>> Please keep messages on-topic and check the CMake FAQ at:
>>> http://www.cmake.org/Wiki/CMake_FAQ
>>>
>>> Kitware offers various services to support the CMake community. For 
>>> more
>>> information on each offering, please visit:
>>>
>>> CMake Support: http://cmake.org/cmake/help/support.html
>>> CMake Consulting: http://cmake.org/cmake/help/consulting.html
>>> CMake Training Courses: http://cmake.org/cmake/help/training.html
>>>
>>> Visit other Kitware open-source projects at
>>> http://www.kitware.com/opensource/opensource.html
>>>
>>> Follow this link to subscribe/unsubscribe:
>>> http://public.kitware.com/mailman/listinfo/cmake



More information about the CMake mailing list