[CMake] Problem building libraries with no sources with Visual Studio 9 (2008)

Bill Hoffman bill.hoffman at kitware.com
Tue Dec 21 14:45:13 EST 2010


On 12/21/2010 2:17 PM, Michael Hertling wrote:

> AFAIK, this is because CMake does not know how to handle a .def file
> for incorporation in the target, i.e. ${library}.def has no LANGUAGE

Actually, it should...


Something like this should work:
(assumes you have a perl script to create a .def file)

cmake_minimum_required (VERSION 2.6)
project (myexe)

set (SOURCES mylib.cxx mylib2.cxx)

# create a list of all the object files
string (REGEX REPLACE "\\.cxx" ".obj" OBJECTS "${SOURCES}")

# create a shared library with the .def file
add_library (mylib SHARED ${SOURCES}
   ${CMAKE_CURRENT_BINARY_DIR}/mylib.def
   )
# set the .def file as generated
set_source_files_properties (
   ${CMAKE_CURRENT_BINARY_DIR}/mylib.def
   PROPERTIES GENERATED 1
   )

# create an executable
add_executable (myexe myexe.cxx)

# link the executable to the dll
target_link_libraries(myexe mylib)

#convert to windows slashes
set (OUTDIR
   ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}
   )

string (REGEX REPLACE "/" "\\\\" OUTDIR ${OUTDIR})

# create a custom pre link command that runs
# a perl script to create a .def file using dumpbin
add_custom_command (
   TARGET mylib PRE_LINK
   COMMAND perl
   ARGS ${CMAKE_CURRENT_SOURCE_DIR}/makedef.pl
   ${CMAKE_CURRENT_BINARY_DIR}\\mylib.def mylib
   ${OUTDIR} ${OBJECTS} )


More information about the CMake mailing list