diff --git a/01-basic/B-hello-headers/README.adoc b/01-basic/B-hello-headers/README.adoc index 831e300..a524242 100644 --- a/01-basic/B-hello-headers/README.adoc +++ b/01-basic/B-hello-headers/README.adoc @@ -4,11 +4,11 @@ toc::[] -[[hello-cmake]] -Hello CMake ------------ +[[hello-headers]] +Hello Headers +------------- -Shows a hello world example, which uses a different folder for source and include +Shows a hello world example which uses a different folder for source and include files. The files in this tutorial are below: diff --git a/01-basic/C-static-library/CMakeLists.txt b/01-basic/C-static-library/CMakeLists.txt index a1c946f..3caef11 100644 --- a/01-basic/C-static-library/CMakeLists.txt +++ b/01-basic/C-static-library/CMakeLists.txt @@ -33,4 +33,4 @@ add_executable(hello_binary ${binary_SOURCES}) # link the new hello_library target with the hello_binary target target_link_libraries( hello_binary hello_library -) \ No newline at end of file +) diff --git a/01-basic/C-static-library/README.adoc b/01-basic/C-static-library/README.adoc index 8b13789..ddb8156 100644 --- a/01-basic/C-static-library/README.adoc +++ b/01-basic/C-static-library/README.adoc @@ -1 +1,120 @@ +:toc: +:toc-placement!: +toc::[] + + +[[static-lib]] +Static Library +-------------- + +Shows a hello world example which first creates and links a static library + +The files in this tutorial are below: + +``` +$ tree +. +├── CMakeLists.txt +├── inc +│   └── Hello.h +└── src + ├── Hello.cpp + └── main.cpp +``` + + * CMakeLists.txt - Contains the CMake commands you wish to run + * inc/Hello.h - The header file to include + * src/Hello.cpp - A source file to compile + * src/main.cpp - The source file with main + +[[concepts]] +Concepts +~~~~~~~~ + +[[adding-static-library]] +Adding a Static Library +^^^^^^^^^^^^^^^^^^^^^^^ + +The +add_library()+ function is used to create a library from some source files. +This is called as follows: + +[source,cmake] +==== +set(library_SOURCES + src/Hello.cpp +) + +add_library(hello_library STATIC ${library_SOURCES}) +==== + +This will be used to create a static library with the name libhello_library.a with +the sources from the +library_SOURCES+ variable. + +[[linking-library]] +Linking a Library +^^^^^^^^^^^^^^^^^ + +When creating an executable that will use your library you must tell the compiler +about the library. This can be done using the +target_link_library()+ function. + +[source,cmake] +==== +add_executable(hello_binary ${binary_SOURCES}) + +target_link_libraries( hello_binary + hello_library +) +==== + +This tells CMake to link the hello_library against the hello_binary executable +during link time. + +An example of this being called by the compiler is + +``` +/usr/bin/c++ CMakeFiles/hello_binary.dir/src/main.cpp.o -o hello_binary -rdynamic libhello_library.a +``` + + +[[building-the-example]] +Building the Example +~~~~~~~~~~~~~~~~~~~~ + +[source,bash] +---- +$ mkdir build + +$ cd build + +$ cmake .. +-- The C compiler identification is GNU 4.8.4 +-- The CXX compiler identification is GNU 4.8.4 +-- Check for working C compiler: /usr/bin/cc +-- Check for working C compiler: /usr/bin/cc -- works +-- Detecting C compiler ABI info +-- Detecting C compiler ABI info - done +-- Check for working CXX compiler: /usr/bin/c++ +-- Check for working CXX compiler: /usr/bin/c++ -- works +-- Detecting CXX compiler ABI info +-- Detecting CXX compiler ABI info - done +-- Configuring done +-- Generating done +-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/C-static-library/build + +$ make +Scanning dependencies of target hello_library +[ 50%] Building CXX object CMakeFiles/hello_library.dir/src/Hello.cpp.o +Linking CXX static library libhello_library.a +[ 50%] Built target hello_library +Scanning dependencies of target hello_binary +[100%] Building CXX object CMakeFiles/hello_binary.dir/src/main.cpp.o +Linking CXX executable hello_binary +[100%] Built target hello_binary + +$ ls +CMakeCache.txt CMakeFiles cmake_install.cmake hello_binary libhello_library.a Makefile + +$ ./hello_binary +Hello Static Library! +---- diff --git a/01-basic/D-shared-library/README.adoc b/01-basic/D-shared-library/README.adoc index 8b13789..1600103 100644 --- a/01-basic/D-shared-library/README.adoc +++ b/01-basic/D-shared-library/README.adoc @@ -1 +1,122 @@ +:toc: +:toc-placement!: + +toc::[] + + +[[shared-lib]] +Shared Library +-------------- + +Shows a hello world example which first creates and links a shared library + +The files in this tutorial are below: + +``` +$ tree +. +├── CMakeLists.txt +├── inc +│   └── Hello.h +└── src + ├── Hello.cpp + └── main.cpp +``` + + * CMakeLists.txt - Contains the CMake commands you wish to run + * inc/Hello.h - The header file to include + * src/Hello.cpp - A source file to compile + * src/main.cpp - The source file with main + +[[concepts]] +Concepts +~~~~~~~~ + +[[adding-shared-library]] +Adding a Shared Library +^^^^^^^^^^^^^^^^^^^^^^^ + +As with the previous example on static libraries, the +add_library()+ function +is also used to create a shared library from some source files. +This is called as follows: + +[source,cmake] +==== +set(library_SOURCES + src/Hello.cpp +) + +add_library(hello_library SHARED ${library_SOURCES}) +==== + +This will be used to create a shared library with the name libhello_library.so with +the sources from the +library_SOURCES+ variable. + +[[linking-library]] +Linking a Shared Library +^^^^^^^^^^^^^^^^^^^^^^^^ + +Linking a shared library is the same as linking a static library. When creating your +executable use the the +target_link_library()+ function to point to your library + +[source,cmake] +==== +add_executable(hello_binary ${binary_SOURCES}) + +target_link_libraries( hello_binary + hello_library +) +==== + +This tells CMake to link the hello_library against the hello_binary executable +during link time. + +An example of this being called by the linker is + +``` +/usr/bin/c++ CMakeFiles/hello_binary.dir/src/main.cpp.o -o hello_binary -rdynamic libhello_library.so -Wl,-rpath,/home/matrim/workspace/cmake-examples/01-basic/D-shared-library/build +``` + + +[[building-the-example]] +Building the Example +~~~~~~~~~~~~~~~~~~~~ + +[source,bash] +---- +$ mkdir build + +$ cd build + +$ cmake .. +-- The C compiler identification is GNU 4.8.4 +-- The CXX compiler identification is GNU 4.8.4 +-- Check for working C compiler: /usr/bin/cc +-- Check for working C compiler: /usr/bin/cc -- works +-- Detecting C compiler ABI info +-- Detecting C compiler ABI info - done +-- Check for working CXX compiler: /usr/bin/c++ +-- Check for working CXX compiler: /usr/bin/c++ -- works +-- Detecting CXX compiler ABI info +-- Detecting CXX compiler ABI info - done +-- Configuring done +-- Generating done +-- Build files have been written to: /home/matrim/workspace/cmake-examples/01-basic/D-shared-library/build + +$ make +Scanning dependencies of target hello_library +[ 50%] Building CXX object CMakeFiles/hello_library.dir/src/Hello.cpp.o +Linking CXX shared library libhello_library.so +[ 50%] Built target hello_library +Scanning dependencies of target hello_binary +[100%] Building CXX object CMakeFiles/hello_binary.dir/src/main.cpp.o +Linking CXX executable hello_binary +[100%] Built target hello_binary + +$ ls +CMakeCache.txt CMakeFiles cmake_install.cmake hello_binary libhello_library.so Makefile + +$ ./hello_binary +Hello Shared Library! +----