I recently did my first steps with cmake and it wasn’t easy as there’re not much sources on how to use it with visual studio. After some try and error I got some things done which I wanted to document here.
Variables
1 2 |
CMAKE_SOURCE_DIR path to CMakeLists.txt CMAKE_BINARY_DIR path to the compiled target file |
Build Debug or Release dll’s
cmake offers different ways for building release and debug configs with visual studio depending on the generator.
nmake makefiles:
1 2 |
cmake -G "NMake Makefiles" -DBUILD_SHARED_LIBS=1 -DCMAKE_BUILD_TYPE=Release .. cmake --build . |
Visual Studio:
1 2 |
cmake -G "Visual Studio 10".. cmake --build . --config Release |
Add preprocessor definitions
1 |
ADD_DEFINITIONS(-DTARGET_WINDOWS -Dwhatever) |
Download a file and check the status
Downloads the file BINDING_FILE from http://ws0.org/xbmc/win32/ and puts it into the build directory.
1 2 3 4 5 6 7 |
file(DOWNLOAD http://ws0.org/xbmc/win32/${BINDING_FILE} ${CMAKE_BINARY_DIR}/${BINDING_FILE} STATUS STATUSLIST SHOW_PROGRESS) LIST(GET STATUSLIST 0 VALUE) IF(${VALUE} STRGREATER "0") LIST(GET STATUSLIST 1 VALUE) message (STATUS "failed to download XBMC bindings: " ${VALUE}) ENDIF(${VALUE} STRGREATER "0") |
Unpack zip file
execute_process executes the command during compile time and not on configure.
1 2 3 |
execute_process( COMMAND ${CMAKE_COMMAND} -E tar xzf ${CMAKE_BINARY_DIR}/${BINDING_FILE} ) |
Create an archive with cpack
1 2 3 4 5 6 |
INSTALL(DIRECTORY visualization.waveform DESTINATION ./) INSTALL(PROGRAMS ${CMAKE_BINARY_DIR}/waveform.dll DESTINATION visualization.waveform) SET(CPACK_GENERATOR "ZIP") SET(CPACK_INCLUDE_TOPLEVEL_DIRECTORY OFF) #SET(CPACK_PACKAGE_FILE_NAME "waveform.test") -> would give the package a different name than default INCLUDE(CPack) |
Adds a new target ‘package’ which could be called with ‘nmake package’ or ‘cmake –build . –target=PACKAGE.’
Inside the zip is the directory visualization.waveform with the contents
of the directory visualization.waveform plus the waveform.dll file.
Use external Visual Studio project file
1 |
INCLUDE_EXTERNAL_MSPROJECT(milkdrop ${CMAKE_SOURCE_DIR}/src/Plugin.sln) |
Generates a target ‘milkdrop’ for use like add dependencies. Visual Studio generators only.
INCLUDE_DIRECTORIES has no affect on those targets.
Set Visual Studio compile parameters
1 |
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Oi /Ob1 /Zi /arch:SSE") |
Set compile language per source
1 |
SET_SOURCE_FILES_PROPERTIES( ${EVALLIBSRC} PROPERTIES LANGUAGE C ) |
Sets the compiler language to C for all sources in ${EVALLIBSRC}
Add additionale include and linker directories
1 2 |
INCLUDE_DIRECTORIES(${XBMC_INCLUDE_DIR} $ENV{DXSDK_DIR}Include) LINK_DIRECTORIES($ENV{DXSDK_DIR}Lib/x86) |