1

It seems cmake can't determine which libraries can be used with the current compiler or rather I want cmake to only use libraries that have been compiled with the same compiler.

I am using find_library to find the needed libraries but it seems to be unable to determine if the library is actually usable. It managed to find the library by name but it was using the ".lib" extension when I was making a configuration for MinGW. Do I have to create folders for every compiler I compile libraries for and add if statements for each compiler in my script ? That just seems counter intuitive to what I believed find_library would be capable of. Perhaps I am misusing it ? Anyone have any better ideas ?

johndoe
  • 303
  • 2
  • 13

1 Answers1

2

It seems you're a bit confused: you're right when you suggest that you need different libraries for MinGW and Visual Studio on Windows. But you're wrong when saying that .lib files can't be used by MinGW. Both Visual Studio and MinGW use .lib files to link to libraries on Windows.

The find_library command purpose is only to find libraries. Nothing more, so it's doing its job here.

If you want to make sure that the libraries found can be used by your compiler, you'll have to check that those libraries can be used by your compiler using try_compile:

find_library(YOURLIB_LIBRARY yourlib)
if (YOURLIB_LIBRARY)
    try_compile(YOURLIB_WORKS
        bindir
        somefile.c
        LINK_LIBRARIES ${YOURLIB_LIBRARY})
    if (YOURLIB_WORKS)
        # do something with your library
    else()
        # warn the user of stop the configuration process
    endif()
endif()
Guillaume
  • 10,463
  • 1
  • 33
  • 47
  • Thank you for the reply, you are least answered some of my question. I was wondering more like if you are building a 64-bit executable then CMake will automatically change any directories with "/lib/" to "/lib64/" if you have some variable `FIND_LIBRARY_USE_LIB64_PATHS` i think it was. – johndoe Jun 24 '13 at 06:40
  • It does, but only on "platforms that are known to need it", I can't check that right now, but I'm pretty sure Windows is not in this list of platforms (as there's no standard location for libraries on Windows anyways...) – Guillaume Jun 24 '13 at 06:47
  • Accidentially pressed enter and it posted... only allowed to edit within 5 minutes so: `if(MSVC) find_library(MYLIB_LIBRARY mylib PATHS "prebuilt/lib-msvc/mylib/" elseif(MINGW) # uhh not sure how to check compilers (seems to only be provided for MSVC...) find_library(MYLIB_LIBRARY mylib PATHS "prebuilt/lib-mingw/mylib/" endif() ` Rather than writing if statements for each compiler (which idk it seems there's around about way for checking them ; at least i've seen string comparisons for the compiler ID which can change) if anyone knows a better way for checking complrs – johndoe Jun 24 '13 at 06:53
  • Yeah, you can use MSVC and MINGW to test your compiler. And you can use CMAKE_SIZEOF_VOID_P to test for 32 / 64 bits, see http://stackoverflow.com/a/17127764/1486118 – Guillaume Jun 24 '13 at 08:01
  • Where is MINGW in the documentation, I can see MSVC but not MINGW (http://www.cmake.org/cmake/help/v2.8.11/cmake.html#variable:MSVC). – johndoe Jun 24 '13 at 09:51
  • That's kind of weird... I never noticed it didn't appear in the doc. On the other hand, it's set by CMake here: https://github.com/Kitware/CMake/blob/master/Modules/CMakeCCompiler.cmake.in#L24 and it's used in standard CMake modules too. I guess that's a documentation bug. – Guillaume Jun 24 '13 at 10:05