RoboMaster視覺筆記CMake(四)Shared Library

RoboMaster視覺筆記CMake(四)Shared Library

本CMake系列是依據github上的cmake-examples進行翻譯總結。同時對於不懂的地方進行總結與標註。希望本系列能節省你學習CMake的時間。

英文github地址:https://github.com/ttroy50/cmake-examples

CMake英文官方教程: https://cmake.org/cmake/help/latest/guide/tutorial/index.html


本文自己創建動態庫的操作,應該暫時用不到。但是關於如何添加路徑,鏈接庫的命令,還是需要掌握的。

一 文件樹

├── CMakeLists.txt
├── include
│   └── shared
│       └── Hello.h
└── src
    ├── Hello.cpp
    └── main.cpp

1.1 Hello.h

/*聲明瞭Hello類,Hello的方法是print(),*/
#ifndef __HELLO_H__
#define __HELLO_H__

class Hello
{
public:
    void print();
};

#endif

1.2 Hello.cpp

/*實現了Hello::print()*/
#include <iostream>

#include "shared/Hello.h"

void Hello::print()
{
    std::cout << "Hello Shared Library!" << std::endl;
}


1.3 main.cpp

#include "shared/Hello.h"

int main(int argc, char *argv[])
{
    Hello hi;
    hi.print();
    return 0;
}

1.4 CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(hello_library)
############################################################
# Create a library
############################################################
#根據Hello.cpp生成動態庫
add_library(hello_library SHARED 
    src/Hello.cpp
)
#給動態庫hello_library起一個別的名字hello::library
add_library(hello::library ALIAS hello_library)
#爲這個庫目標,添加頭文件路徑,PUBLIC表示包含了這個庫的目標也會包含這個路徑
target_include_directories(hello_library
    PUBLIC 
        ${PROJECT_SOURCE_DIR}/include
)
############################################################
# Create an executable
############################################################
#根據main.cpp生成可執行文件
add_executable(hello_binary
    src/main.cpp
)
#鏈接庫和可執行文件,使用的是這個庫的別名。PRIVATE 表示
target_link_libraries( hello_binary
    PRIVATE 
        hello::library
)

二 CMake解析

2.1 創建動態庫

add_library()函數用於從某些源文件創建一個動態庫,默認生成在構建文件夾。 寫法如下:

add_library(hello_library SHARED
    src/Hello.cpp
)

在add_library調用中包含了源文件,用於創建名稱爲libhello_library.so的動態庫。

NOTE 如前面的示例所述,將源文件直接傳遞給add_library調用,這是modern CMake的建議。(而不是先把Hello.cpp賦給一個變量)

2.2 創建別名目標

顧名思義,別名目標是在只讀上下文中可以代替真實目標名稱的替代名稱。

add_library(hello::library ALIAS hello_library)

如下所示,當您將目標鏈接到其他目標時,使用別名可以引用目標。

鏈接共享庫與鏈接靜態庫相同。 創建可執行文件時,請使用target_link_library()函數指向您的庫 。

add_executable(hello_binary
    src/main.cpp
)

target_link_libraries(hello_binary
    PRIVATE
        hello::library
)

這告訴CMake使用別名目標名稱將hello_library鏈接到hello_binary可執行文件。

三 構建示例

$ 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!

微信公衆號

歡迎大家關注我的個人公衆號,現階段主要總結Robomaster相關的計算機視覺知識。
公衆號名稱:三豐雜貨鋪
公衆號二維碼

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章