Export "Hello World" as a static library (.a)
The procedure to build static library is similar to build shared (dynamic) library as the previous post. But the only difference is CMakeLists.txt for generating .a library
CMakeLists.txt
# -------------------------------------------
# The minimum version that required to be able to read this CMakeList.txt
# -------------------------------------------
cmake_minimum_required(VERSION 2.8.12)
# -------------------------------------------
# Project name often named as the binary (executable) file
# -------------------------------------------
project (multilang_greetings)
# -------------------------------------
# declare the directories that store project's file headers (.h)
# -------------------------------------
include_directories(include)
# -------------------------------------
# Adding the needed source files
# -------------------------------------
file(GLOB SOURCES "source/*.cpp")
# -------------------------------------------
# Define which source files are include into the library
# Type of the library is STATIC
# name of the library can be differed from project name
# -------------------------------------------
add_library(greetings_encnvi STATIC ${SOURCES})
Note:
Static library is defined in CMake by using STATIC keyword in add_library() to indicate the type of library that will be generated.
Compare to generating .so library, generating .a file is not much different. The result is a .a library
[root@cmake_practice_03_project_to_lib_a]# tree -L 2
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── libgreetings_encnvi.a
│ └── Makefile
├── CMakeLists.txt
├── include
│ ├── greetings_chinese.h
│ ├── greetings_english.h
│ └── greetings_vietnamese.h
└── source
├── greetings_chinese.cpp
├── greetings_english.cpp
└── greetings_vietnamese.cpp
Checking the library content
To double check the linking inside the library, we can run the command ar –t as below
[root@build]# ar -t libgreetings_encnvi.a
greetings_vietnamese.cpp.o
greetings_english.cpp.o
greetings_chinese.cpp.o
Source code can be found HERE