Jiyeqian.is-programmer.com/2011/7/4/cm…

First, appetizers

File structure in hello directory:

?

1234 ├ ─ ─ CMakeLists. TXT ` ` ├ ─ ─ hello. C ` ` ├ ─ ─ hello. H ` ` └ ─ ─ main. C

See the next section for the C code.

The simplest cmake configuration file:

?

123 project(HELLO)``set(SRC_LIST main.c hello.c)``add_executable(hello ${SRC_LIST})

To compile to a DEBUG version of GDB, add the following to the configuration file:

?

1 set(CMAKE_BUILD_TYPE Debug)

If you want to compile to a version that can be analyzed by gprof, add:

?

12 set(CMAKE_BUILD_TYPE Profile)`` set(CMAKE_CXX_FLAGS_PROFILE ``"-pg" )

The simplest compilation process (compiling in the Hello directory) :

?

12 cmake .``make

This generates the executable Hello and other cmake-related configuration files in the Hello directory.

To keep your code clean, use what’s called out-of-source compilation:

?

1234 mkdir build``cd build``cmake .. ``make

All files generated by this compilation are in the build.

Two, a small project Hello demonstration

This example mainly covers the compilation and installation of independent library files, executable files and related issues.

File structure in the Hello directory:

?

12345678 ├ ─ ─ CMakeLists. TXT ` ` ├ ─ ─ libhello ` ` │ ├ ─ ─ CMakeLists. TXT ` ` │ ├ ─ ─ hello. C ` ` │ └ ─ ─ hello. H ` ` └ ─ ─ the SRC ` ` ├ ─ ─ CMakeLists. TXT ` ` └ ─ ─ main. C

The content of the document is as follows:

?

12345 Project (HELLO) ` ` cmake_minimum_required (VERSION 2.8) ` ` set (CMAKE_INSTALL_PREFIX /tmp/hello)``add_subdirectory(libhello)``add_subdirectory(src)

Set (CMAKE_INSTALL_PREFIX: Sets the installation directory of the program with a higher priority than cmake command parameters.

?

12345678910 set(LIB_SRC hello.c)``add_definitions( "-DLIBHELLO_BUILD" )``set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)``add_library(libhello SHARED ${LIB_SRC})``add_library(hello_static STATIC ${LIB_SRC})``install(TARGETS libhello LIBRARY DESTINATION lib)``install(TARGETS hello_static ARCHIVE DESTINATION lib)``install(FILES hello.h DESTINATION include)`` set_target_properties(libhello PROPERTIES OUTPUT_NAME ``"hello" )`` set_target_properties(hello_static PROPERTIES OUTPUT_NAME ``"hello" )

(1) add_library: generate library file, SHARED stands for dynamic link library; (2) set(LIBRARY_OUTPUT_PATH: the path to the generated library file. PROJECT_BINARY_DIR and CMAKE_BINARY_DIR, < projectName >_BINARY_DIR are both directories for compilation; (3) install: install header files and library files to the corresponding directory; (4) set_target_properties(libhello: the generated library file is called libhello.so, otherwise the library file is liblibhello.so.

?

12345678 include_directories(${PROJECT_SOURCE_DIR}/libhello)``link_directories(${CMAKE_INSTALL_PREFIX}/lib)``aux_source_directory (. APP_SRC)``set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)``add_executable(hello ${APP_SRC})``target_link_libraries(hello libhello)``install(TARGETS hello RUNTIME DESTINATION bin)``set_property(TARGET hello PROPERTY INSTALL_RPATH_USE_LINK_PATH TRUE)

(1) include_directories: specifies a header path; Link_directories: specifies a library directory. (3) aux_source_directory: place the current directory (.) Assign all file names to APP_SRC; EXECUTABLE_OUTPUT_PATH: specifies the path of the executable file to be generated. PROJECT_BINARY_DIR, CMAKE_BINARY_DIR, and < projectName >_BINARY_DIR are directories for compilation. Add_executable: generate executable files. PROJECT_SOURCE_DIR, CMAKE_SOURCE_DIR, < projectName >_SOURCE_DIR are all top-level directories of the project. And (6) target_link_libraries: libhello. / libhello CMakeLists. TXT libhello of corresponding; (7) install(TARGETS: install program to ${CMAKE_INSTALL_PREFIX}/bin directory; Cannot open shared object file: No such file or directory

?

12345678910111213 #ifndef DBZHANG_HELLO_``#define DBZHANG_HELLO_``#if defined _WIN32``     #if LIBHELLO_BUILD``         #define LIBHELLO_API __declspec(dllexport)``     #else``         #define LIBHELLO_API __declspec(dllimport)``     #endif``#else``     #define LIBHELLO_API``#endif`` LIBHELLO_API ``void hello( const char * name); ``#endif //DBZHANG_HELLO_

?

1234567 #include <stdio.h>``#include "hello.h" void hello( const char *name)``{``      printf ( "Hello %s! \n" , name); ` `}

?

1234567 #include "hello.h" int main( int argc, ``char *argv[])``{``      hello( "World" ); ` ` return 0; ` `}

Compile the program in the Hello directory:

?

12345 mkdir build``cd build``cmake .. -DCMAKE_INSTALL_PREFIX= /tmp``make``make install

(1) -dcmake_install_prefix =/ TMP change the installation directory of the program to/TMP /bin, default CMAKE_INSTALL_PREFIX=/usr/local; (2) If set(CMAKE_INSTALL_PREFIX/TMP /hello) is set in cmakelists. TXT, this setting is invalid.

After compiling, the main file structure in the Hello directory is:

?

12345678910111213141516 ├ ─ ─ build ` ` │ ├ ─ ─ bin ` ` │ │ └ ─ ─ the hello ` ` │ ├ ─ ─ lib ` ` │ │ ├ ─ ─ libhello. So ` ` │ │ └ ─ ─ libhello. A ` ` │ ├ ─ ─ libhello ` ` │ └ ─ ─ the SRC ` ` ├ ─ ─ CMakeLists. TXT ` ` ├ ─ ─ libhello ` ` │ ├ ─ ─ CMakeLists. TXT ` ` │ ├ ─ ─ hello. C ` ` │ └ ─ ─ hello. H ` ` └ ─ ─ the SRC ` ` ├ ─ ─ CMakeLists. TXT ` ` └ ─ ─ main. C

The build directory has four subdirectories. The libhello and SRC directories are intermediate files generated during cmake for the corresponding directories of source files.

/ TMP /hello

?

1234567 ├ ─ ─ bin ` ` │ └ ─ ─ the hello ` ` ├ ─ ─ the include ` ` │ └ ─ ─ hello. H ` ` └ ─ ─ lib ` ` ├ ─ ─ libhello. A ` ` └ ─ ─ libhello. So

Resources: blog.csdn.net/dbzhang800/… Blog.csdn.net/dbzhang800/… www.cmake.org/Wiki/CMake_…

\