123 lines
2.8 KiB
Plaintext
123 lines
2.8 KiB
Plaintext
= Static Library
|
|
|
|
:toc:
|
|
:toc-placement!:
|
|
|
|
toc::[]
|
|
|
|
|
|
[[intro]]
|
|
Introduction
|
|
------------
|
|
|
|
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!
|
|
----
|