CMake基础 第4节 动态库
介紹#
繼續(xù)展示一個(gè)hello world示例,它將首先創(chuàng)建并鏈接一個(gè)共享庫。
這里還顯示了如何創(chuàng)建別名目標(biāo)
本教程中的文件如下:
$ tree . ├── CMakeLists.txt ├── include │ └── shared │ └── Hello.h └── src├── Hello.cpp└── main.cpp-
[CMakeLists.txt] - 包含要運(yùn)行的 CMake 命令
cmake_minimum_required(VERSION 3.5)project(hello_library)############################################################ # Create a library #############################################################Generate the shared library from the library sources add_library(hello_library SHARED src/Hello.cpp ) add_library(hello::library ALIAS hello_library)target_include_directories(hello_libraryPUBLIC ${PROJECT_SOURCE_DIR}/include )############################################################ # Create an executable ############################################################# Add an executable with the above sources add_executable(hello_binarysrc/main.cpp )# link the new hello_library target with the hello_binary target target_link_libraries( hello_binaryPRIVATE hello::library ) -
[include/shared/Hello.h] - 要包含的頭文件
#ifndef __HELLO_H__ #define __HELLO_H__class Hello { public:void print(); };#endif -
[src/Hello.cpp] - 要編譯的源文件
#include <iostream>#include "shared/Hello.h"void Hello::print() {std::cout << "Hello Shared Library!" << std::endl; } -
[src/main.cpp] - 具有main的源文件
#include "shared/Hello.h"int main(int argc, char *argv[]) {Hello hi;hi.print();return 0; }
概念#
添加共享庫#
與前面的靜態(tài)庫示例一樣,add_library()函數(shù)也用于從一些源文件創(chuàng)建共享庫。它的用法如下:
add_library(hello_library SHAREDsrc/Hello.cpp )這將使用傳遞給add_library()函數(shù)的源碼文件創(chuàng)建一個(gè)名為libhello_library.so的共享庫。
別名目標(biāo)#
顧名思義,別名目標(biāo)是目標(biāo)的替代名稱,可以在只讀上下文中替代真實(shí)的目標(biāo)名稱。
add_library(hello::library ALIAS hello_library)ALIAS類似于“同義詞”。ALIAS目標(biāo)只是原始目標(biāo)的另一個(gè)名稱。因此ALIAS目標(biāo)的要求是不可修改的——您無法調(diào)整其屬性、安裝它等。
如下所示,這允許你在將目標(biāo)鏈接到其他目標(biāo)時(shí)使用別名引用該目標(biāo)。
鏈接共享庫#
鏈接共享庫與鏈接靜態(tài)庫相同。創(chuàng)建可執(zhí)行文件時(shí),請使用target_link_library()函數(shù)指向庫。
add_executable(hello_binarysrc/main.cpp )target_link_libraries(hello_binaryPRIVATEhello::library )這告訴CMake使用別名目標(biāo)名稱將hello_library鏈接到hello_binary可執(zhí)行文件。
鏈接器調(diào)用它的一個(gè)示例是:
/usr/bin/c++ CMakeFiles/hello_binary.dir/src/main.cpp.o -o hello_binary -rdynamic libhello_library.so -Wl,-rpath,/home/matrim/workspace/cmake-examples/01-basic/D-shared-library/build構(gòu)建示例#
$ 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! 與50位技術(shù)專家面對面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的CMake基础 第4节 动态库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CMake基础 第3节 静态库
- 下一篇: CMake基础 第5节 安装项目