Skip to content

Include "Hello World" as library

Scenario:​

  • At this point, we already has our own library, in both dynamic and static types.​

  • So, how to import the libs into our project using CMake​ ​

Result:​

  • Using CMake to import the "hello world" project into our project and build an executable/binary file with the Makefile generated.

Source tree

Lets firstly have a look at what we have for the source tree.

# tree​
.​
├── build​
├── CMakeLists.txt​
├── include​
   ├── greetings_chinese.h​
   ├── greetings_english.h​
   ├── greetings_spanish.h​
   └── greetings_vietnamese.h​
├── lib​
   └── libgreetings_encnvi.so​
└── source​
    ├── greetings_spanish.cpp​
    └── mainapp.cpp

Source code

Content of the each file is as follow

mainapp.cpp
#include<stdio.h>
#include<greetings_english.h>
#include<greetings_chinese.h>
#include<greetings_vietnamese.h>
#include<greetings_spanish.h>
int main(int argc, char * argv[])
{
    greeting_en();
    greeting_cn();
    greeting_vi();
    greeting_es();
    return 0;
}
greetings_english.h
#include<stdio.h>
void greeting_en();
greetings_chinese.h
#include<stdio.h>
void greeting_cn();
greetings_vietnamese.h
#include<stdio.h>
void greeting_vi();
greetings_spanish.h
#include<stdio.h>
void greeting_es();
greetings_spanish.h
1
2
3
4
5
#include<stdio.h>
void greeting_es()
{
    printf("Voila!\n");
}

CMakeLists.txt for .so library

CMakeLists.txt for generating Makefile with .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 the library (.so)​
# -------------------------------------​
set (PROJECT_LINK_LIBS libgreetings_encnvi.so)
link_directories(lib)
# -------------------------------------​
# declare the directories that store project's file headers (.h)​
# -------------------------------------​
include_directories(include)
# -------------------------------------​
# Adding the needed source files​
# -------------------------------------​
file(GLOB SOURCES "source/*.cpp")
# -------------------------------------------​
# Define the relationship between the the source code and the binary file​
# -------------------------------------------​
add_executable(helloworld_in_4lang ${SOURCES})
# -------------------------------------------​
# Needed library is declared via PROJECT_LINK_LIBS​
# -------------------------------------------​
target_link_libraries(helloworld_in_4lang ${PROJECT_LINK_LIBS})

Note:​

  • Compare to Know how 2, there is no library added to the project, in this case, we only need to declare in the CMakefile.txt the library that we use​

  • And the link between the build file and the library.

Build Project

We navigate to build folder, type cmake .. and make as usual:​

[root@base06 cmake_practice_04_include_so_lib_to_project]# cd build/​

[root@base06 build]# cmake ..​
-- The C compiler identification is GNU 4.8.5​
-- The CXX compiler identification is GNU 4.8.5​
-- 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/henryle/CMake_Practice/cmake_practice_04_include_so_lib_to_project/build​
[root@base06 build]# make​
Scanning dependencies of target helloworld_in_4lang​
[ 50%] Building CXX object CMakeFiles/helloworld_in_4lang.dir/source/greetings_spanish.cpp.o​
[100%] Building CXX object CMakeFiles/helloworld_in_4lang.dir/source/mainapp.cpp.o​
Linking CXX executable helloworld_in_4lang​
[100%] Built target helloworld_in_4lang​

Execute

Execute the binary file, and checking the result​

[root@base06 build]# ./helloworld_in_4lang​
Hello!​
Nihao!​
Xin chao!​
Voila!​

Static .a to project

Note:​

There is not much different when include a .so and but replacing the .a library to the project.​

[root@base06 cmake_practice_04_include_a_lib_to_project]# tree​
.​
├── build​
├── CMakeLists.txt​
├── include​
│   ├── greetings_chinese.h​
│   ├── greetings_english.h​
│   ├── greetings_spanish.h​
│   └── greetings_vietnamese.h​
├── lib​
│   └── libgreetings_encnvi.a​
└── source​
    ├── greetings_spanish.cpp​
    └── mainapp.cpp​

CMakeLists.txt for .a library

CMakeLists.txt for generating Makefile with .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 the library (.so)​
# -------------------------------------​
set (PROJECT_LINK_LIBS libgreetings_encnvi.a)
link_directories(lib)
# -------------------------------------​
# declare the directories that store project's file headers (.h)​
# -------------------------------------​
include_directories(include)
# -------------------------------------​
# Adding the needed source files​
# -------------------------------------​
file(GLOB SOURCES "source/*.cpp")
# -------------------------------------------​
# Define the relationship between the the source code and the binary file​
# -------------------------------------------​
add_executable(helloworld_in_4lang ${SOURCES})
# -------------------------------------------​
# Needed library is declared via PROJECT_LINK_LIBS​
# -------------------------------------------​
target_link_libraries(helloworld_in_4lang ${PROJECT_LINK_LIBS})

Note:​

  • In this case, we only need to declare in the CMakefile.txt the library that we use​

  • And the link between the build file and the library.​

  • To build binary file, the operation is absolute just like including with .so lib​

Source code can be found HERE

Share this page:

LinkedIn Twitter Facebook

Comments