Export "Hello World" as a shared library (.so)
Scenario:
We want "hello world" is exported as a library to other project's usage
Result:
Using CMake to export the "hello world" project to 2 types of library, dynamic library and static lirary
Source tree
Lets firstly have a look at what we have for the source tree. The files structure is similar to the previous post.
[root@base06 cmake_practice_03_project_to_lib_so]# tree
.
├── build
├── CMakeLists.txt
├── include
│ ├── greetings_chinese.h
│ ├── greetings_english.h
│ └── greetings_vietnamese.h
└── source
├── greetings_chinese.cpp
├── greetings_english.cpp
└── greetings_vietnamese.cpp
Content of the each file is as follow
Header files
The same as previous post
Source files
The same as previous post
CMakeLists.txt
There is not much different from the post to the previous, but only we do not have main() function in any source files because we are building a library.
CMakeLists.txt
CMakeLists.txt for generating .so library
# -------------------------------------------
# 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 SHARED
# name of the library can be differed from project name
# -------------------------------------------
add_library(greetings_encnvi SHARED ${SOURCES})
Note:
A dynamic library or a share object is shared between processes in system at runtime. CMake uses SHARED keyword in add_library() to indicate the type of library that will be generated.
Build
Run CMake and is as the same as previous post
We change the directory to build folder and run the command cmake .. After build the project, the .so library is the file that we expect as the result.
[root@ cmake_practice_03_project_to_lib_so]# tree -L 2
.
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ ├── libgreetings_encnvi.so
│ └── Makefile
├── CMakeLists.txt
├── include
│ ├── greetings_chinese.h
│ ├── greetings_english.h
│ └── greetings_vietnamese.h
└── source
├── greetings_chinese.cpp
├── greetings_english.cpp
└── greetings_vietnamese.cpp
The file libgreetings_encnvi.so is generated in build
Checking the library with ldd
To double check the linking inside the library, we can run the command ldd as below
[root@base06 build]# ldd libgreetings_encnvi.so
linux-vdso.so.1 => (0x00007ffc957c4000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f8f33d2f000)
libm.so.6 => /lib64/libm.so.6 (0x00007f8f33a2d000)
libc.so.6 => /lib64/libc.so.6 (0x00007f8f33449000)
/lib64/ld-linux-x86-64.so.2 (0x00007f8f34239000)
Conlusion
We have going through all the steps to have the library build project, as the result is the library that built with the linking is checked.
Source code can be found HERE