#!/bin/bash # # http://fedetft.wordpress.com/2009/12/21/cmake-part-2-compiler-flags/ http://www.cmake.org/Wiki/CMake:How_To_Write_Platform_Checks How_to_get_SubVersion_Date_Info ? How_to_get_current_date_from_cmake ? Try_compile_example cmake_variables_list cmake_commands # # CMake tutorial: # CMakeLists file: cmake_help(){ cat < cmake [options] CMakeLists.txt file: cmake_minimum_required (VERSION 2.6) project (HELLO) # Makefile will be generated only if PROJECT() exists. add_executable (hello main.cpp foo.cpp) To compile do: $ ls main.cpp foo.cpp $ mkdir build $ cd build $ cmake ../ $ make Tip : make VERBOSE=1 ; shows c++ commandline flags being used; Tip : set(CMAKE_BUILD_TYPE Release) adds -O3 Tip : set(CMAKE_BUILD_TYPE Debug) adds -g flag Tip : set(CMAKE_BUILD_TYPE Debug) adds -g flag Tip : To control CFLAGS directly: ## Compiler flags if(CMAKE_COMPILER_IS_GNUCXX) list(APPEND CMAKE_CXX_FLAGS -O2) ## Optimize list(APPEND CMAKE_EXE_LINKER_FLAGS -s) ## Strip binary endif() Linking with libraries: There is find_package() and find_library() commands; Tip: Using Thread library ## Target set(TEST_SRCS main.cpp) add_executable(test ${TEST_SRCS}) ## Link libraries find_package(Threads REQUIRED) target_link_libraries(test ${CMAKE_THREAD_LIBS_INIT}) Note: Thread is a 'special' type library which auto-defines, CMAKE_THREAD_LIBS_INIT as the thread library. Usually the library defines libname_LIBRARY variable. Tip: Using a predefined library (package) ## Link libraries find_package(PNG REQUIRED) include_directories(${PNG_INCLUDE_DIR}) target_link_libraries(test ${PNG_LIBRARY}) Tip: Adding collection of libraries ## Link libraries set(BOOST_LIBS thread date_time system) find_package(Boost COMPONENTS ${BOOST_LIBS} REQUIRED) target_link_libraries(test ${Boost_LIBRARIES}) find_package(Threads REQUIRED) target_link_libraries(test ${CMAKE_THREAD_LIBS_INIT}) Tip: See cmake --system-information to see the system information. Tip: Adding our own option: # should we use our own math functions? option (USE_MYMATH "Use tutorial provided math implementation" ON) if (USE_MYMATH) .... endif (USE_MYMATH) Tip: install (TARGETS MathFunctions DESTINATION bin) install (FILES MathFunctions.h DESTINATION include) Tip: The CMake variable CMAKE_INSTALL_PREFIX is used to determine the root of where the files will be installed. Tip: does this system provide the log and exp functions? include (CheckFunctionExists.cmake) check_function_exists (log HAVE_LOG) check_function_exists (exp HAVE_EXP) Next we modify the TutorialConfig.h.in to define those values if CMake found them on the platform as follows: // does the platform provide exp and log functions? #cmakedefine HAVE_LOG #cmakedefine HAVE_EXP Tip: Building installer # build a CPack driven installer package include (InstallRequiredSystemLibraries) set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt") set (CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}") set (CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}") include (CPack) Tip: Add the dir to the list of dirs for the build: add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL]) CMakeCache.txt is a cache cmake file; Tip: Display simple message: IF(CMAKE_SIZEOF_VOID_P MATCHES 8) MESSAGE(STATUS "Detected 64 bit platform") SET (PLATFORM X64) ENDIF(CMAKE_SIZEOF_VOID_P MATCHES 8) EOF } cmake_help_more(){ cat < cmake [options] Options -C = Pre-load a script to populate the cache. -D := = Create a cmake cache entry. -U = Remove matching entries from CMake cache. -G = Specify a makefile generator. -Wno-dev = Suppress developer warnings. -Wdev = Enable developer warnings. -E = CMake command mode. -i = Run in wizard mode. -L[A][H] = List non-advanced cached variables. -N = View mode only. -P = Process script mode. --graphviz=[file] = Generate graphviz of dependencies. --system-information [file] = Dump information about this system. --debug-trycompile = Do not delete the try compile directories.. --debug-output = Put cmake in a debug mode. --help-command cmd [file] = Print help for a single command and exit. --help-command-list [file] = List available listfile commands and exit. --help-commands [file] = Print help for all commands and exit. --help-compatcommands [file]= Print help for compatibility commands. --help-module module [file] = Print help for a single module and exit. --help-module-list [file] = List available modules and exit. --help-modules [file] = Print help for all modules and exit. --help-custom-modules [file]= Print help for all custom modules and exit. --help-property prop [file] = Print help for a single property and exit. --help-property-list [file] = List available properties and exit. --help-properties [file] = Print help for all properties and exit. --help-variable var [file] = Print help for a single variable and exit. --help-variable-list [file] = List documented variables and exit. --help-variables [file] = Print help for all variables and exit. --copyright [file] = Print the CMake copyright and exit. --help = Print usage information and exit. --help-full [file] = Print full help and exit. --help-html [file] = Print full help in HTML format. --help-man [file] = Print full help as a UNIX man page and exit. --version [file] = Show program name/version banner and exit. Generators The following generators are available on this platform: Unix Makefiles = Generates standard UNIX makefiles. CodeBlocks - Unix Makefiles = Generates CodeBlocks project files. Eclipse CDT4 - Unix Makefiles = Generates Eclipse CDT 4.0 project files. KDevelop3 = Generates KDevelop 3 project files. KDevelop3 - Unix Makefiles = Generates KDevelop 3 project files. Tip: System modules are found here: /usr/share/cmake-2.6/Modules/ Tip: To define your own command: add_custom_command(OUTPUT output1 [output2 ...] COMMAND command1 [ARGS] [args1...] [COMMAND command2 [ARGS] [args2...] ...] [MAIN_DEPENDENCY depend] [DEPENDS [depends...]] [IMPLICIT_DEPENDS depend1 ...] [WORKING_DIRECTORY dir] [COMMENT comment] [VERBATIM] [APPEND]) Tip: add_definitions adds -D define flags to the compilation of source files. add_definitions(-DFOO -DBAR ...) Tip: add_dependencies add a dependency between top-level targets. add_dependencies(target-name depend-target1 depend-target2 ...) Make a top-level target depend on other top-level targets. A top-level target is one created by ADD_EXECUTABLE, ADD_LIBRARY, or ADD_CUSTOM_TARGET. add_library: Add a library to the project using the specified source files. Tip: Find the full path to a file: find_path( name1 [path1 path2 ...]) Tip: Find a library. find_library( name1 [path1 path2 ...]) Tip: Load settings for an external project. find_package( [major[.minor[.patch]]] [EXACT] [QUIET] [[REQUIRED|COMPONENTS] [components...]]) Tip: Variables that provide info: CMAKE_EXECUTABLE_SUFFIX CMAKE_GENERATOR CMAKE_IMPORT_LIBRARY_PREFIX CMAKE_IMPORT_LIBRARY_SUFFIX CMAKE_LINK_LIBRARY_SUFFIX CMAKE_SHARED_LIBRARY_PREFIX CMAKE_SHARED_LIBRARY_SUFFIX CMAKE_SHARED_MODULE_PREFIX CMAKE_SHARED_MODULE_SUFFIX CMAKE_STATIC_LIBRARY_PREFIX CMAKE_STATIC_LIBRARY_SUFFIX These are env variables supported: CMAKE_PREFIX_PATH FIND_LIBRARY(TCL_LIBRARY NAMES tcl tcl84 tcl83 tcl82 tcl80 PATHS /usr/lib /usr/local/lib) In CMake all variables are of the type string. Nevertheless CMake can deal also with lists. If a string contains semicolons, these semicolons can be interpreted as separators of string values. set(MyString "Hello world") This sets MyString to "Hello world", which is always only one string. set(MyList Hello world) This sets MyList to a list with the items "Hello" and "world". It will be stored as a string "Hello;world". * CMakeLists.txt - root directory: # Project name is not mandatory, but you should use it project(helloworld) # States that CMake required version must be >= 2.6 cmake_minimum_required(VERSION 2.6) # Appends the cmake/modules path inside the MAKE_MODULE_PATH variable which stores # directories of additional CMake modules (eg MacroOutOfSourceBuild.cmake): set(CMAKE_MODULE_PATH ${helloworld_SOURCE_DIR}/cmake/modules ${CMAKE_MODULE_PATH}) # The macro below forces the build directory to be different from source directory: include(MacroOutOfSourceBuild) macro_ensure_out_of_source_build("${PROJECT_NAME} requires an out of source build.") add_subdirectory(src) * CMakeLists.txt - src/ directory: # Include the directory itself as a path to include directories set(CMAKE_INCLUDE_CURRENT_DIR ON) # Create a variable called helloworld_SOURCES containing all .cpp files: set(helloworld_SOURCES helloworld.cpp main.cpp) # For a large number of source files you can create it in a simpler way # using file() function: # file(GLOB hellworld_SOURCES *.cpp) # Create an executable file called helloworld from sources: add_executable(helloworld ${helloworld_SOURCES}) Linking with static when dynamic lib is present : You could try something like this. 1. Find the HDF static library with FIND_LIBRARY 2. Copy the static library into a directory in your build tree with EXEC_PROGRAM( ${CMAKE_COMMAND} -E copy_if_different ${HDF_LIB} ${PROJECT_BINARY_DIR}/HDFStaticLib) 3. Add the link directory for HDFStaticLib first with LINK_DIRECTORIES(${PROJECT_BINARY_DIR}/HDFStaticLib) 4. Add the library like this: TARGET_LINK_LIBRARIES(foo ${PROJECT_BINARY_DIR}/HDFStaticLib/HDF) -Bill When searching for a library the command now tries the name given by the user as an explicit file name before trying prefixes and suffixes. One may now write find_library(MATH_LIBRARY NAMES libm.a) Microsoft Windows Microsoft Windows will check the registry to determine the proper place to find an ActiveX DLL, but for other DLLs it will check the directory where it loaded the program from; the current working directory; any directories set by calling the SetDllDirectory() function; the System32, System, and Windows directories; and finally the directories specified by the PATH environment variable.[8] Applications written for the .NET Framework framework (since 2002), also check the Global Assembly Cache as the primary store of shared dll files to remove the issue of DLL hell. when linking against .LIB file in Windows one must first know if it is a regular static library or an import library. In the latter case, a .DLL file must be present at runtime. MAC OS uses .dylib suffix; DLL examples: http://en.wikipedia.org/wiki/Dynamic-link_library Windows MySQL mysql lib client: http://dev.mysql.com/doc/refman/5.0/en/windows-client-compiling.html You can either link your code with the dynamic libmysql.lib library, which is just a wrapper to load in libmysql.dll on demand, or link with the static mysqlclient.lib library. The MySQL client libraries are compiled as threaded libraries, so you should also compile your code to be multi-threaded. EOF } mkdir -p /tmp/cmake cat > /tmp/cmake/CMakeLists.txt < /tmp/cmake/example.cc < #include int main (int argc, char *argv[]) { printf("tutorial prog version is: %d\n", PROG_VERSION); printf("hello world!!!\n"); } EOF cat > /tmp/cmake/Tutorial.h.in < #include #include #include int main() { off_t offset; struct stat s; /* see if have 64-bit off_t */ if (sizeof(offset) < 8) return 1; printf(\" off_t is %d bytes\n\", sizeof(off_t)); /* see if have 64-bit stat */ if (sizeof(s.st_size) < 8) { printf(\" s.st_size is %d bytes\n\", sizeof(s.st_size)); return 2; } return 3; } ") TRY_COMPILE(COMPILE_OK ${MY_TMP} "conftest.c" COPY_FILE "${MY_TMP}/conftest.bin" ) IF(COMPILE_OK) TRY_RUN(...) ENDIF() Instead, you can directly use macro: CHECK_C_SOURCE_RUNS(SOURCE, VAR) using CheckCSourceRuns.cmake module. ============================================================================== cmake_commands: COMMANDS ================================================================= _cmake_determine_compiler_abi _cmake_determine_compiler_id _cmake_determine_compiler_id_build _cmake_determine_compiler_id_check _cmake_determine_compiler_id_write add_custom_command add_custom_target add_definitions add_dependencies add_executable add_library add_subdirectory add_test adjust_cmake_system_variables aux_source_directory break build_command build_name cmake_determine_compiler_abi cmake_determine_compiler_id cmake_determine_compiler_id_build cmake_determine_compiler_id_check cmake_determine_compiler_id_write cmake_minimum_required cmake_policy configure_file create_test_sourcelist define_property else elseif enable_language enable_testing endforeach endfunction endif endmacro endwhile exec_program execute_process export export_library_dependencies file find_file find_library find_package find_path find_program fltk_wrap_ui foreach function get_cmake_property get_directory_property get_filename_component get_property get_source_file_property get_target_property get_test_property if include include_directories include_external_msproject include_regular_expression install install_files install_programs install_targets link_directories link_libraries list load_cache load_command macro make_directory mark_as_advanced math message option output_required_files project qt_wrap_cpp qt_wrap_ui remove remove_definitions return separate_arguments set set_directory_properties set_property set_source_files_properties set_target_properties set_tests_properties site_name source_group string subdir_depends subdirs target_link_libraries try_compile try_run use_mangled_mesa utility_source variable_requires variable_watch while write_file ============================================================================== cmake_variables_list CMAKE_FILES_DIRECTORY "/CMakeFiles" CMAKE_HOME_DIRECTORY "/home/thava/bzr4root/single/src/__cmake_systeminformation" CMAKE_HOST_SYSTEM "Linux-2.6.27-7-generic" CMAKE_HOST_SYSTEM_NAME "Linux" CMAKE_FILES_DIRECTORY "/CMakeFiles" CMAKE_EDIT_COMMAND "/usr/bin/ccmake" CMAKE_C_PLATFORM_ID "Linux" CMAKE_C_FLAGS "" CMAKE_C_FLAGS_DEBUG "-g" CMAKE_C_FLAGS_DEBUG_INIT "-g" CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG" CMAKE_C_FLAGS_MINSIZEREL_INIT "-Os -DNDEBUG" CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG" CMAKE_C_FLAGS_RELEASE_INIT "-O3 -DNDEBUG" CMAKE_C_FLAGS_RELWITHDEBINFO "-O2 -g" CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "-O2 -g" CMAKE_COMMAND "/usr/bin/cmake" CMAKE_BUILD_TOOL "/usr/bin/make" CMAKE_STATIC_LIBRARY_PREFIX == "lib" CMAKE_STATIC_LIBRARY_SUFFIX == ".a" CMAKE_SHARED_LIBRARY_PREFIX == "lib" CMAKE_SHARED_LIBRARY_SUFFIX == ".so" CMAKE_SHARED_MODULE_PREFIX == "lib" CMAKE_SHARED_MODULE_SUFFIX == ".so"