[CMake] import library

Brad King brad.king at kitware.com
Fri Jun 12 13:27:06 EDT 2009


Brad King wrote:
> VS is not complaining that it cannot *find* the .lib file...it's
> complaining that it cannot *create* it.  The reason is that the
> directory does not exist.  If I make the directory by hand before
> building then it works.
> 
> I'm investigating CMake internals to find the cause.

Okay, it's actually a bug in Visual Studio, not CMake.  The IDE
automatically creates directories for output files.  If I switch
my example to

   set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${FOO_BINARY_DIR}/bin)
   #add_executable(foo foo.c)
   add_library(foo SHARED foo.c)

then it works.  I can even manually delete the 'bin' directory
and the directory holding the import library, and VS recreates
them on the next build.

Since VS creates the directories CMake has never had to do so.
We'll have to teach CMake to work around the problem.  Please
submit a bug report here:

   http://www.cmake.org/Bug

and send me the bug number.

Meanwhile, you can add a work around directly to your project.

CMake always adds "ImportLibrary=..." to VS projects to tell
VS where to put the import library *if* it needs to create one.
VS will always try to create an import library if an object file
dllexport-s something whether or not the ImportLibrary setting
is present.  If the setting is not present then the import
library goes next to the executable.

If you didn't mean to really dllexport anything, then remove
the mark from the offending symbols.  If you really did mean
to dllexport something, then you need to let VS put the import
library somewhere.  One way to do this is to create the directory
where CMake tells VS to put the library (this is what our fix to
CMake will do).  In the CMakeLists.txt file containing this line:

   ADD_EXECUTABLE(mecan MeCAn.cpp ${HDRS})

add

   IF("${CMAKE_GENERATOR}" MATCHES "Visual Studio")
     # VS fails to create the exe's import library directory,
     # so make it now.
     FOREACH(cfg ${CMAKE_CONFIGURATION_TYPES})
       FILE(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${cfg}")
     ENDFOREACH(cfg)
   ENDIF("${CMAKE_GENERATOR}" MATCHES "Visual Studio")

-Brad


More information about the CMake mailing list