[CMake] MinGW and import libraries to system libraries, was Re: Finding MinGW compiler libraries?

Werner Smekal smekal at iap.tuwien.ac.at
Sun Aug 31 08:19:40 EDT 2008


Hi Bill,

> For system libraries like this it is often better to just link them in 
> with -l.  The compiler should be able to find them if it is installed 
> correctly.  There is a good chance CMake will get it wrong.   So, the 
> correct CMake solution for system/compiler libraries is to just use the 
> name of the library.
> 
> Something like this:
> 
> if(MINGW)
> target_link_libraries(foo uuid ole32 shell32)
> endif(MINGW)

I have a similar problem trying to link system libraries with MinGW. 
Just add the library name will work, but then we get warnings because we 
don't use absolute paths to libraries. Hardcoding the library name (to 
the MinGW import libraries is not a good solution, if you want that it 
should work "out of the box" for most users (e.g. if have MinGW versions 
in C:\MinGW3.4.5 and C:\MinGW4.3.1). You could always set 
CMAKE_LIBRARY_PATH, but since the MinGW directory looks like this:

...\MinGW\
           bin  <- gcc is in there
           doc
           lib  <- import libraries
           man
           .....

one is able to get the lib library path from the CMAKE_C_COMPILER 
variable (our library is in C, so this must be always set). So my 
solution would be:

# We need the path to the MinGW compiler in order to find
# the import libraries for system libraries.
IF(MINGW)
	get_filename_component(MINGWBINPATH ${CMAKE_C_COMPILER} PATH)
	set(MINGWLIBPATH ${MINGWBINPATH}/../lib
			CACHE FILEPATH
			DOCSTRING "Path to MinGW import libraries")
ENDIF(MINGW)

and then

message(STATUS "Looking for gdi32 header and library")
find_library(GDI32_LIBRARY gdi32 HINTS ${MINGWLIBPATH})

We need the HINTS argument, otherwise cmake will find again gdi32.dll in 
c:\windows\system32, since this path is in the PATH environment variable 
- cmake will search there after HINTS paths but before PATHS paths.

I hope this helps others you have this problem.

Regards,
Werner

-- 
Dr. Werner Smekal
Institut fuer Allgemeine Physik
Technische Universitaet Wien
Wiedner Hauptstr 8-10
A-1040 Wien
Austria

email: smekal at iap.tuwien.ac.at
web:   http://www.iap.tuwien.ac.at/~smekal
phone: +43-(0)1-58801-13463 (office)
        +43-(0)1-58801-13469 (laboratory)
fax:   +43-(0)1-58801-13499


More information about the CMake mailing list