cmake构建动态库

前言

linux下的动态库/静态库,以前就知道咋构建:头文件与库的关系。最近写win下动态库的时候,有些细节与linux下有点不同。

所以这篇文章简单介绍下,如果使用cmake构建一个可以跨平台编译使用的动态库。(我尝试使用cmake-option, 通过一个CMakeLists.txt,分别编译生成动态库/静态库。完全可以做到,但是要让头文件区分动态库/静态库有点麻烦。所以本文缩小范围,仅仅考虑动态库的跨平台构建)

详细代码见仓库:19-lib-demo

代码

代码结构

.
├── CMakeLists.txt
├── lib
│   ├── CMakeLists.txt
│   ├── hello.cpp
│   └── hello.h
└── test
    ├── CMakeLists.txt
    └── 

库文件代码

下面是头文件和头文件中接口的实现。包含全局变量,函数,类在动态库中的写法。

头文件

#pragma once

#include <iostream>
#include <string>

#ifdef _WIN32
    #ifdef _EXPORT_DLL_
        #define EXPORT __declspec(dllexport)
    #else
        #define EXPORT __declspec(dllimport)
    #endif
#else
    #define EXPORT
#endif

EXPORT extern const std::string var;

EXPORT void hello();

// EXPORT class hi {
class EXPORT hi {
public:
    hi() {
        std::cout << "hello construct." << std::endl;
    }
};

接口实现。

#include "hello.h"

const std::string var = "hello var";

void hello() {
    std::cout << "hello world" << std::endl;
}

下面按点罗列其中的注意点。

测试代码

测试代码如下。

#include "../lib/hello.h"

int main(int argc, char* argv[]) {
    hi h;
    hello();
    std::cout << var << std::endl;
    return 0;
}
cmake_minimum_required(VERSION 3.10.0)
 
project(test)

link_directories(${PROJECT_SOURCE_DIR}/bin)

add_executable(${PROJECT_NAME} test.cpp)
 
target_link_libraries(${PROJECT_NAME} hello)  

编译运行结果如下。

mkdir build
cd build
cmake ..
cmake --build . --config Release
..\bin\Release\test.exe
hello construct.
hello world
hello var

查看下程序依赖的动态库。

 dumpbin.exe /dependents  bin\Release\test.exe
  Image has the following dependencies:

    hello.dll  <- 这里
    MSVCP140.dll
    ....
    KERNEL32.dll

查看下动态库的导出信息。

 dumpbin.exe /EXPORTS .\bin\Release\hello.lib
 File Type: LIBRARY

     Exports

       ordinal    name

                  ??0hi@@QEAA@XZ (public: __cdecl hi::hi(void))
                  ??4hi@@QEAAAEAV0@$$QEAV0@@Z (public: class hi & __cdecl hi::operator=(class hi &&))
                  ??4hi@@QEAAAEAV0@AEBV0@@Z (public: class hi & __cdecl hi::operator=(class hi const &))
                  ?hello@@YAXXZ (void __cdecl hello(void))
                  ?var@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@B (class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const var)

下面按点罗列其中的注意点。

Linux下测试

# 编译
cmake -DCMAKE_BUILD_TYPE=release --build .
make

# 运行
../bin/test
hello construct.
hello world
hello var

# 查看程序依赖库
ldd ../bin/test
        linux-vdso.so.1 (0x00007ffdc1ffd000)
        libhello.so => xxx/bin/libhello.so (0x00007f761396d000) 《--这里
        libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f7613774000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7613582000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7613433000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f7613979000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7613418000)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

da1234cao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值