├── README ├── hello_world ├── CMakeLists.txt └── main.c ├── hello_world_clear ├── CMakeLists.txt ├── COPYRIGHT ├── README ├── doc │ └── hello.txt ├── runhello.sh └── src │ ├── .CMakeLists.txt.swp │ ├── CMakeLists.txt │ └── main.c ├── hello_world_lib ├── CMakeLists.txt └── src │ ├── CMakeLists.txt │ ├── hello.c │ └── hello.h └── hello_world_share ├── CMakeLists.txt ├── include └── hello │ └── hello.h ├── lib ├── libhello.a └── libhello.dylib └── src ├── CMakeLists.txt └── main.c /README: -------------------------------------------------------------------------------- 1 | This is a simple CMake practice project which contains four different scenarios 2 | 3 | 1. hello_world 4 | Demo a simplest CMake project 5 | 6 | 2. hello_world_clear 7 | Separate the output files and src files 8 | 9 | 3. hello_world_lib 10 | Demo how to make a static/shared library 11 | 12 | 4. hello_world_share 13 | Demp how to utilize external static/shared library 14 | 15 | More info refers to http://sewm.pku.edu.cn/src/paradise/reference/CMake%20Practice.pdf 16 | 17 | If any question or recommendation, feel free to contact me, thanks :) -------------------------------------------------------------------------------- /hello_world/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | PROJECT (HELLO) 4 | 5 | SET(SRC_LIST main.c) 6 | 7 | MESSAGE(STATUS "This is BINARY dir " ${PROJECT_BINARY_DIR}) 8 | MESSAGE(STATUS "This is SOURCE dir " ${PROJECT_SOURCE_DIR}) 9 | 10 | ADD_EXECUTABLE(hello ${SRC_LIST}) 11 | 12 | -------------------------------------------------------------------------------- /hello_world/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | printf("Hello World!\n"); 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /hello_world_clear/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | PROJECT (HELLO) 4 | ADD_SUBDIRECTORY(src) 5 | 6 | INSTALL(FILES COPYRIGHT README DESTINATION share/doc/cmake/hello_world_out) 7 | INSTALL(PROGRAMS runhello.sh DESTINATION bin) 8 | INSTALL(DIRECTORY doc/ DESTINATION share/doc/cmake/hello_world_out) 9 | 10 | 11 | -------------------------------------------------------------------------------- /hello_world_clear/COPYRIGHT: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/browny/cmake-practice/d46fb62238fe86b13ba3cc1bca5cda33fa229560/hello_world_clear/COPYRIGHT -------------------------------------------------------------------------------- /hello_world_clear/README: -------------------------------------------------------------------------------- 1 | 1. Output exe file in separate folder 2 | 2. Show how to install different output to destnation -------------------------------------------------------------------------------- /hello_world_clear/doc/hello.txt: -------------------------------------------------------------------------------- 1 | Hello World! 2 | -------------------------------------------------------------------------------- /hello_world_clear/runhello.sh: -------------------------------------------------------------------------------- 1 | hello 2 | -------------------------------------------------------------------------------- /hello_world_clear/src/.CMakeLists.txt.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/browny/cmake-practice/d46fb62238fe86b13ba3cc1bca5cda33fa229560/hello_world_clear/src/.CMakeLists.txt.swp -------------------------------------------------------------------------------- /hello_world_clear/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | ADD_EXECUTABLE(hello main.c) 4 | SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) 5 | 6 | -------------------------------------------------------------------------------- /hello_world_clear/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | int main() { 3 | printf("Hello World!\n"); 4 | return 0; 5 | } 6 | -------------------------------------------------------------------------------- /hello_world_lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | PROJECT(HELLOLIB) 4 | ADD_SUBDIRECTORY(src) 5 | 6 | -------------------------------------------------------------------------------- /hello_world_lib/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | SET(LIBHELLO_SRC hello.c) 4 | 5 | ADD_LIBRARY(hello_dynamic SHARED ${LIBHELLO_SRC}) 6 | ADD_LIBRARY(hello_static STATIC ${LIBHELLO_SRC}) 7 | 8 | SET_TARGET_PROPERTIES(hello_dynamic PROPERTIES OUTPUT_NAME "hello") 9 | SET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME "hello") 10 | 11 | SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) 12 | -------------------------------------------------------------------------------- /hello_world_lib/src/hello.c: -------------------------------------------------------------------------------- 1 | #include "hello.h" 2 | void HelloFunc() { 3 | printf("Hello World!\n"); 4 | } 5 | -------------------------------------------------------------------------------- /hello_world_lib/src/hello.h: -------------------------------------------------------------------------------- 1 | #ifndef HELLO_H 2 | #define HELLO_H 3 | #include 4 | void HelloFunc(); 5 | #endif 6 | -------------------------------------------------------------------------------- /hello_world_share/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | PROJECT(NEWHELLO) 4 | ADD_SUBDIRECTORY(src) 5 | -------------------------------------------------------------------------------- /hello_world_share/include/hello/hello.h: -------------------------------------------------------------------------------- 1 | #ifndef HELLO_H 2 | #define HELLO_H 3 | #include 4 | void HelloFunc(); 5 | #endif 6 | -------------------------------------------------------------------------------- /hello_world_share/lib/libhello.a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/browny/cmake-practice/d46fb62238fe86b13ba3cc1bca5cda33fa229560/hello_world_share/lib/libhello.a -------------------------------------------------------------------------------- /hello_world_share/lib/libhello.dylib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/browny/cmake-practice/d46fb62238fe86b13ba3cc1bca5cda33fa229560/hello_world_share/lib/libhello.dylib -------------------------------------------------------------------------------- /hello_world_share/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | ADD_EXECUTABLE(main main.c) 4 | 5 | INCLUDE_DIRECTORIES(../include/hello) 6 | 7 | FIND_LIBRARY(HELLO_LIB NAMES hello PATHS "../lib/") 8 | MESSAGE(STATUS "Library path HELLO_LIB is " ${HELLO_LIB}) 9 | 10 | TARGET_LINK_LIBRARIES(main ${HELLO_LIB}) 11 | 12 | SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) 13 | -------------------------------------------------------------------------------- /hello_world_share/src/main.c: -------------------------------------------------------------------------------- 1 | #include "hello.h" 2 | int main() 3 | { 4 | HelloFunc(); 5 | return 0; 6 | } 7 | --------------------------------------------------------------------------------