For C/C++/Java
需要为任何子目录建立一个 CMakeLists.txt
http://now-code.com/archives/208
http://www.4ucode.com/Study/Topic/858092
http://www.cmake.org/cmake/help/cmake_tutorial.html
http://www.cnblogs.com/sinojelly/archive/2010/05/22/1741337.html
Out source build
>in source build for test or easy build, output and generated files all over
>out source build for large project, setup the output path
CMake cache
- Configuration file set of flags passed to the configure command.
- The first time CMake is run, it produces a CMakeCache.txt file, you may edit it by CMake GUI, or the file directly.
- CMake will not alter an existing entry in the cache file itself.
Utility Targets
- For Visual Studio projects, automatically created: ALL_BUILD and RUN_TESTS.
Command
- ccmake CMakeLists.txt; ccmake.
To run in interactive mode, just pass the option "-i" to cmake
-make clean
To remove the built stuffs
CMakeList
>PROJECT_NAME
>command (args...), Upper, lower, and mixed case commands are supported by CMake
- set(VAR a;b;c) set(VAR a b c)
set the list of the strings which can be iterated with the foreach command or manipulated with the list command
set(Foo a b c) : command(${Foo}) = command(a b c) ; command("${Foo}") = command( "a b c" ).
-if
if(var)
some_command(...)
endif(var)
- foreach
set(VAR a b c)
# loop over a, b,c with the variable f
foreach(f ${VAR})
message(${f})
endforeach(f)
-macro
# define a macro hello
macro(hello MESSAGE)
message(${MESSAGE})
endmacro(hello)
# call the macro with the string "hello world"
-function
hello("hello world")
# define a function hello
function(hello MESSAGE)
message(${MESSAGE})
endfunction(hello)
option (USE_MYMATH
"Use tutorial provided math implementation" ON)
#use Marco to set optional library
if (USE_MYMATH) endif (USE_MYMATH)
in the source code like file.h.in
"#cmakedefine USE_MYMATH"
-SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
-SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib
在哪里ADD_EXECUTABLE或ADD_LIBRARY,
如果需要改变目标存放路径,就在哪里加入上述的定义。
<projectname>_BINARY_DIR = PROJECT_BINARY_DIR = CMAKE_BINARY_DIR
<projectname>_SOURCE_DIR = PROJECT_SOURCE_DIR = CMAKE_SOURCE_DIR
#in source = top level, out source = current
ADD_DEPENDENCIES(target-name depend-target1
depend-target2 ...
Install
CMAKE_INSTALL_PREFIX 的默认定义是/usr/loca
INSTALL(TARGETS targets...
ADD_LIBRARY(libname [SHARED|STATIC|MODULE]...
SET_TARGET_PROPERTIES(target1 target2 ... #change output name
GET_TARGET_PROPERTY(VAR target property)
INCLUDE_DIRECTORIES([AFTER|BEFORE... #Add the given directories to those searched by the compiler for include files
LINK_DIRECTORIES(directory1 directory2 ...
TARGET_LINK_LIBRARIES(target library1.. #link to the library
# if you have dependency, need define the dependency by this
add_dependencies(target-name depend-target1 depend-target2 ...)
In CMakeList
set (Tutorial_VERSION_MAJOR 1) #lib.1
set (Tutorial_VERSION_MINOR 0) #lib.1.0
In header.in
"#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@"
"#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@"
the Marco will be replaced by the value in the CMakeList
Example
-cmake_minimum_required (VERSION 2.6) #project version
-add_subdirectory (Hello) #Recurse into the "Hello" subdirectories
-add_library (Hello hello.cxx)#link library Hello.lib for hello.c
-include_directories (${HELLO_SOURCE_DIR}/Hello) #compiler can find include files
-link_directories (${HELLO_BINARY_DIR}/Hello) #linker can find the Hello library
-target_link_libraries (helloDemo Hello) #Link the executable to the Hello library
-PROJECT (HELLO) #project name
-SET(SRC_LIST main.c) #cpp files set
#show the message
-MESSAGE(STATUS "This is BINARY dir " ${HELLO_BINARY_DIR})
MESSAGE(STATUS "This is SOURCE dir "${HELLO_SOURCE_DIR})
-ADD_EXECUTABLE(hello SRC_LIST) #link hello.exe for the src/cpp
#myrun to bin, mylib to lib and mstaticlib to libstatic
INSTALL(TARGETS myrun mylib mystaticlib
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION libstatic
)
[SHARED|STATIC|MODULE]
.so|.a|.exe
ADD_LIBRARY(hello SHARED ${LIBHELLO_SRC}
#the name can't be duplicated for the libs, this line won't work
ADD_LIBRARY(hello STATIC ${LIBHELLO_SRC}
#workaround to build same name lib for both static and shared
ADD_LIBRARY(hello_static STATIC ${LIBHELLO_SRC}
SET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME "hello")
#cmake would remove the duplicated lib, so the .so would be removed
SET_TARGET_PROPERTIES(hello PROPERTIES CLEAN_DIRECT_OUTPUT 1)
SET_TARGET_PROPERTIES(hello_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.2 SOVERSION 1)
VERSION指代动态库版本,SOVERSION 指代 API 版本
LIBRARY_OUTPUT_PATH
#by default add a sub folder and put binary into it
ADD_SUBDIRECTORY(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
SUBDIRS(src)#only add a sub folder, binary stay
#set binary output path
EXECUTABLE_OUTPUT_PATH
LIBRARY_OUTPUT_PATH
>link
TARGET_LINK_LIBRARIES(main hello) or TARGET_LINK_LIBRARIES(main libhello.so)
TARGET_LINK_LIBRARIES(main libhello.a)
IF(WIN32)
#do something related to WIN32
ELSEIF(UNIX)
#do something related to UNIX
ELSEIF(APPLE)
#do something related to APPLE
ENDIF(WIN32)
Handle windows rc
Just put the .rc files in the source list of the target. CMakeSetup dialog is a good example:
CMake with Qt
http://qtnode.net/wiki/Qt_with_cmake
http://developer.qt.nokia.com/quarterly/view/using_cmake_to_build_qt_projects
http://cuijiemin.iteye.com/blog/900170
Qt old version
#which qmake can lead you to the path of the old qt
If there is old Qt installed, check the /usr/lib; /usr/lib64; /usr/bin; /usr/include...
remove them or the building tools will go to defalut path to seek the lib or header, make the wrong version mess up the building
MOC
#if you add like #include moc_file.cpp into the c++ src, don't add moc file(qrc uic) to add_library
or need Add_LIBRARY(lib moc_file.cpp)
# find and setup Qt4 for this project (default method)
FIND_PACKAGE(Qt4 REQUIRED); FIND_PACKAGE(Qt4)
SET(QT_USE_QTXML 1)
# the path to a CMake file that can be included to compile Qt 4 applications and libraries. By default, the QtCore and QtGui libraries are loaded.
INCLUDE(${QT_USE_FILE})
ADD_DEFINITIONS(${QT_DEFINITIONS})
QT4_WRAP_CPP(outfiles inputfile ... OPTIONS ...)
QT4_WRAP_UI(outfiles inputfile ... OPTIONS ...)QT4_ADD_RESOURCES(outfiles inputfile ... OPTIONS ...)
QT4_ADD_RESOURCES(outfiles inputfile ... OPTIONS ...)
# default behavior is to generate the X.cxx files, can't be X.cpp, need customize the macro to generate the file name
http://web.archiveorange.com/archive/v/5y7Pky2MQcb6g0UJZ6e3