[CMake] Using find_package() portably?

Carsten Fuchs carsten.fuchs at cafu.de
Mon Dec 4 05:36:33 EST 2017


Thanks to all!

Well, I guess I'm among the people mentioned by Lectem at 
https://www.mail-archive.com/cmake-developers@cmake.org/msg18267.html

I'll try to dig my way through it, but at this point I consider staying 
with SCons under Linux, where finding packages is important and the 
command line is used anyway, and using CMake under Windows only, where 
quasi all external libraries are shipped along with the project and the 
real goal is to obtain a Visual Studio solution. (I already have a 
complete build with SCons that works under Windows and Linux for my 
project which is of average complexity.)

Best regards,
Carsten

$ ls Cafu/ExtLibs/ -1
bullet
Cg
fbx
fmod
freealut
freetype
glfw
jpeg
libogg
libpng
libvorbis
lua
lwo
MersenneTwister.h
minizip
mpg123
noise
openal-soft
openal-win
pstdint.h
tclap
wxWidgets
zlib



Am 29.11.2017 um 21:17 schrieb Florent Castelli:
> On 29/11/2017 12:02, Johannes Zarl-Zierl wrote:
>> Hello Carsten,
>>
>> On Samstag, 25. November 2017 11:46:44 CET Carsten Fuchs wrote:
>>> Thanks for your reply, but how can I proceed from here?
>>>
>>> In order to be able to write in the parent script something that works
>>> in either case, that is,
>>>
>>> target_link_libraries(main_program <either JPEG::JPEG or ${JPEG...} 
>>> or ???>)
>> Well, that's one way, but that doesn't really provide any benefit 
>> above the
>> original solution (i.e. just define the JPEG_INCLUDES and JPEG_LIBRARIES
>> variables accordingly).
>>
>>> it seems that I either
>>>
>>>     - have to make a copy of FindJPEG.cmake, customize it to define
>>> target JPEG::JEPG and ship it along with my project,
>> Either that or you define it yourself:
>> find_package(JPEG)
>> if (JPEG_FOUND)
> 
> I'd say "if(JPEG_FOUND AND NOT TARGET JPEG::JPEG)"
> If someone improves FindJPEG.cmake, you won't break your build.
> 
>>   add_library(JPEG::JPEG UNKNOWN IMPORTED)
>>   set_property(
>>    TARGET JPEG::JPEG
>>    PROPERTY IMPORTED_LOCATION ${JPEG_LIBRARIES}
>>    )
>>   set_property(
>>    TARGET JPEG::JPEG PROPERTY
>>    INTERFACE_INCLUDE_DIRECTORIES ${JPEG_INCLUDES}
>>   )
>> endif()
>>
>> I admit it's not a huge benefit over just using the variables. The 
>> advantages:
>> - you get slightly better error handling
>> - your target_link_libraries commands are slightly more readable.
>>
>>>     - or write my `../ExtLibs/libjpeg/CMakeLists.txt` so that it 
>>> globally
>>> defines JPEG_LIBRARIES and JPEG_INCLUDE_DIRS
>> That will work, too.
>>
>>
>> Cheers,
>>    Johannes
>>
> 



More information about the CMake mailing list