├── .gitignore ├── CMake Practice.pdf ├── LICENSE ├── README.md ├── t1 ├── CMakeLists.txt └── main.c ├── t2 ├── CMakeLists.txt ├── COPYRIGHT ├── README ├── doc │ └── hello.txt ├── runhello.sh └── src │ ├── CMakeLists.txt │ └── main.c ├── t3 ├── CMakeLists.txt └── lib │ ├── CMakeLists.txt │ ├── hello.c │ └── hello.h ├── t4 ├── CMakeLists.txt └── src │ ├── CMakeLists.txt │ └── main.c ├── t5 ├── CMakeLists.txt └── src │ ├── CMakeLists.txt │ └── main.c └── t6 ├── CMakeLists.txt ├── cmake └── FindHELLO.cmake └── src ├── CMakeLists.txt └── main.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Compiled Dynamic libraries 8 | *.so 9 | *.dylib 10 | *.dll 11 | 12 | # Compiled Static libraries 13 | *.lai 14 | *.la 15 | *.a 16 | *.lib 17 | 18 | # Executables 19 | *.exe 20 | *.out 21 | *.app 22 | -------------------------------------------------------------------------------- /CMake Practice.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/xfun68/cmake-practice-source-code/d5758df0437d0b6fdbb66682b7dd68fa6da3392c/CMake Practice.pdf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | cmake-practice-source-code 2 | ========================== 3 | 4 | Source code for cmake-practice.pdf 5 | 6 | This source code is written by me when I read this pdf: http://sewm.pku.edu.cn/src/paradise/reference/CMake%20Practice.pdf 7 | 8 | I also committed one copy of the pdf in this repo, so please feel free to download it and refer to the source code. 9 | 10 | At last, I want to give a bit thanks to the autor of this pdf. THANK YOU, CJacker. 11 | 12 | -------------------------------------------------------------------------------- /t1/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | PROJECT (HELLO) 2 | 3 | SET(SRC_LIST main.c) 4 | 5 | MESSAGE(STATUS "This is BIANRY dir " ${HELLO_BINARY_DIR}) 6 | MESSAGE(STATUS "This is SOURCE dir " ${HELLO_SOURCE_DIR}) 7 | 8 | ADD_EXECUTABLE(hello ${SRC_LIST}) 9 | 10 | 11 | # project(hello) 12 | # add_executable(hello main.c) 13 | 14 | -------------------------------------------------------------------------------- /t1/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | printf("Hello World\n"); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /t2/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project (hello) 4 | 5 | add_subdirectory(src bin) 6 | 7 | install(FILES COPYRIGHT README DESTINATION share/doc/cmake/t2) 8 | install(PROGRAMS runhello.sh DESTINATION bin) 9 | install(DIRECTORY doc/ DESTINATION share/doc/cmake/t2) 10 | 11 | -------------------------------------------------------------------------------- /t2/COPYRIGHT: -------------------------------------------------------------------------------- 1 | Tue Jun 10 13:00:24 CST 2014 2 | -------------------------------------------------------------------------------- /t2/README: -------------------------------------------------------------------------------- 1 | Tue Jun 10 13:00:29 CST 2014 2 | -------------------------------------------------------------------------------- /t2/doc/hello.txt: -------------------------------------------------------------------------------- 1 | Hello World 2 | -------------------------------------------------------------------------------- /t2/runhello.sh: -------------------------------------------------------------------------------- 1 | ./hello 2 | 3 | -------------------------------------------------------------------------------- /t2/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(hello main.c) 2 | 3 | install( 4 | TARGETS hello 5 | RUNTIME DESTINATION bin 6 | ) 7 | 8 | -------------------------------------------------------------------------------- /t2/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | printf("Hello World\n"); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /t3/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(HelloLib) 4 | 5 | add_subdirectory(lib) 6 | 7 | -------------------------------------------------------------------------------- /t3/lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LIBHELLO_SRC hello.c) 2 | 3 | add_library(hello SHARED ${LIBHELLO_SRC}) 4 | add_library(hello_static STATIC ${LIBHELLO_SRC}) 5 | 6 | set_target_properties(hello_static PROPERTIES OUTPUT_NAME "hello") 7 | 8 | set_target_properties(hello PROPERTIES CLEAN_DIRECT_OUTPUT 1) 9 | set_target_properties(hello_static PROPERTIES CLEAN_DIRECT_OUTPUT 1) 10 | 11 | set_target_properties(hello PROPERTIES VERSION 1.2 SOVERSION 1) 12 | set_target_properties(hello_static PROPERTIES VERSION 1.2 SOVERSION 1) 13 | 14 | install(TARGETS hello hello_static 15 | LIBRARY DESTINATION lib 16 | ARCHIVE DESTINATION lib) 17 | 18 | install(FILES hello.h DESTINATION include/hello) 19 | 20 | -------------------------------------------------------------------------------- /t3/lib/hello.c: -------------------------------------------------------------------------------- 1 | #include "hello.h" 2 | 3 | void HelloFunc() { 4 | printf("Hello World from HelloLib\n"); 5 | } 6 | 7 | -------------------------------------------------------------------------------- /t3/lib/hello.h: -------------------------------------------------------------------------------- 1 | #ifndef HELLO_H 2 | #define HELLO_H 3 | 4 | #include 5 | 6 | void HelloFunc(); 7 | 8 | #endif 9 | 10 | -------------------------------------------------------------------------------- /t4/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(NEWHELLO) 4 | 5 | add_subdirectory(src) 6 | 7 | -------------------------------------------------------------------------------- /t4/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(main main.c) 2 | 3 | include_directories(hello) 4 | 5 | target_link_libraries(main hello) 6 | 7 | -------------------------------------------------------------------------------- /t4/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | HelloFunc(); 5 | return 0; 6 | } 7 | 8 | -------------------------------------------------------------------------------- /t5/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(CURLTEST) 4 | 5 | add_subdirectory(src) 6 | 7 | -------------------------------------------------------------------------------- /t5/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(curltest main.c) 2 | 3 | find_package(curl) 4 | 5 | if(CURL_FOUND) 6 | include_directories(${CURL_INCLUDE_DIR}) 7 | target_link_libraries(curltest ${CURL_LIBRARY}) 8 | elseif(CURL_FOUND) 9 | message(fatal_error "CURL library not found") 10 | endif(CURL_FOUND) 11 | 12 | -------------------------------------------------------------------------------- /t5/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | FILE *fp; 7 | 8 | int write_data(void *ptr, size_t size, size_t nmemb, void *stream) 9 | { 10 | int written = fwrite(ptr, size, nmemb, (FILE *)fp); 11 | return written; 12 | } 13 | 14 | int main() 15 | { 16 | const char * path = "/tmp/curl-test"; 17 | const char * mode = "w"; 18 | 19 | fp = fopen(path, mode); 20 | curl_global_init(CURL_GLOBAL_ALL); 21 | 22 | CURL *curl = curl_easy_init(); 23 | curl_easy_setopt(curl, CURLOPT_URL, "http://www.baidu.com"); 24 | curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); 25 | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); 26 | 27 | CURLcode res = curl_easy_perform(curl); 28 | 29 | curl_easy_cleanup(curl); 30 | 31 | return 0; 32 | } 33 | 34 | -------------------------------------------------------------------------------- /t6/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | 3 | project(hello) 4 | 5 | set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) 6 | 7 | add_subdirectory(src) 8 | 9 | -------------------------------------------------------------------------------- /t6/cmake/FindHELLO.cmake: -------------------------------------------------------------------------------- 1 | find_path(HELLO_INCLUDE_DIR hello.h /usr/include/hello /usr/local/include/hello) 2 | find_library(HELLO_LIBRARY NAMES hello PATH /usr/lib /usr/local/lib) 3 | 4 | if(HELLO_INCLUDE_DIR AND HELLO_LIBRARY) 5 | SET(HELLO_FOUND true) 6 | endif(HELLO_INCLUDE_DIR AND HELLO_LIBRARY) 7 | 8 | if(HELLO_FOUND) 9 | if(NOT HELLO_FIND_QUIETLY) 10 | message(STATUS "Found Hello: ${HELLO_INCLUDE_DIR} ${HELLO_LIBRARY}") 11 | endif(NOT HELLO_FIND_QUIETLY) 12 | else(HELLO_FOUND) 13 | if(HELLO_FIND_REQUIRED) 14 | message(FATAL_ERROR "Could not find hello library") 15 | endif(HELLO_FIND_REQUIRED) 16 | endif(HELLO_FOUND) 17 | 18 | -------------------------------------------------------------------------------- /t6/src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | find_package(HELLO) 2 | 3 | if(HELLO_FOUND) 4 | add_executable(hello main.c) 5 | include_directories(${HELLO_INCLUDE_DIR}) 6 | target_link_libraries(hello ${HELLO_LIBRARY}) 7 | endif(HELLO_FOUND) 8 | 9 | -------------------------------------------------------------------------------- /t6/src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | HelloFunc(); 5 | return 0; 6 | } 7 | 8 | --------------------------------------------------------------------------------