生成 .so 和使用 .so

是哟ing两个最简单的项目演示使用 c++ 项目生成 .so 和在 c++ 项目中使用 .so

项目1:生成 .so 文件 (maker_so)

项目结构

复制代码
maker_so/
├── include/
│   └── calculator.h
├── src/
│   └── calculator.cpp
└── CMakeLists.txt

文件内容

include/calculator.h

cpp 复制代码
#ifndef CALCULATOR_H
#define CALCULATOR_H

extern "C" {
    int add(int a, int b);
    int subtract(int a, int b);
    int multiply(int a, int b);
    float divide(int a, int b);
}

#endif

src/calculator.cpp

cpp 复制代码
#include "calculator.h"

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int multiply(int a, int b) {
    return a * b;
}

float divide(int a, int b) {
    if (b == 0) return 0;
    return (float)a / b;
}

CMakeLists.txt

cmake 复制代码
cmake_minimum_required(VERSION 3.10)
project(maker_so)

# 设置库的输出目录为项目下的 lib
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)

# 包含头文件目录
include_directories(${CMAKE_SOURCE_DIR}/include)

# 创建动态库
add_library(calculator SHARED src/calculator.cpp)

message(STATUS "Building calculator shared library")

构建项目1

bash 复制代码
# 创建并进入项目目录
mkdir -p maker_so/{include,src,build}
cd maker_so

# 创建文件(使用上面的内容)
# ... 创建 calculator.h, calculator.cpp, CMakeLists.txt

# 构建
cd build
cmake ..
cmake --build .

# 查看生成的 .so
ls -l ../lib/
# 应该看到: libcalculator.so

项目2:使用 .so 文件 (user_so)

项目结构

复制代码
user_so/
├── lib/                    # 从项目1复制过来的 .so
│   └── libcalculator.so
├── include/                # 从项目1复制过来的头文件
│   └── calculator.h
├── src/
│   └── main.cpp
└── CMakeLists.txt

文件内容

include/calculator.h(从项目1复制)

cpp 复制代码
#ifndef CALCULATOR_H
#define CALCULATOR_H

extern "C" {
    int add(int a, int b);
    int subtract(int a, int b);
    int multiply(int a, int b);
    float divide(int a, int b);
}

#endif

src/main.cpp

cpp 复制代码
#include <iostream>
#include "calculator.h"

int main() {
    int a = 15, b = 4;
    
    std::cout << "=== Using calculator library ===\n";
    std::cout << a << " + " << b << " = " << add(a, b) << std::endl;
    std::cout << a << " - " << b << " = " << subtract(a, b) << std::endl;
    std::cout << a << " * " << b << " = " << multiply(a, b) << std::endl;
    std::cout << a << " / " << b << " = " << divide(a, b) << std::endl;
    
    return 0;
}

CMakeLists.txt

cmake 复制代码
cmake_minimum_required(VERSION 3.10)
project(user_so)

# 设置可执行文件输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)

# 设置 RPATH,让程序能在 lib 目录找到 .so
set(CMAKE_INSTALL_RPATH "${CMAKE_SOURCE_DIR}/lib")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)

# 包含头文件目录
include_directories(${CMAKE_SOURCE_DIR}/include)

# 指定库文件目录
link_directories(${CMAKE_SOURCE_DIR}/lib)

# 创建可执行文件
add_executable(calc_app src/main.cpp)

# 链接动态库
target_link_libraries(calc_app calculator)

message(STATUS "Building application that uses calculator.so")

完整的一键构建脚本

创建一个脚本 build_all.sh 来自动化整个过程:

bash 复制代码
#!/bin/bash

echo "=== 项目1: 生成 libcalculator.so ==="

# 创建项目1
mkdir -p maker_so/{include,src,build}

# 创建项目1的文件
cat > maker_so/include/calculator.h << 'EOF'
#ifndef CALCULATOR_H
#define CALCULATOR_H

extern "C" {
    int add(int a, int b);
    int subtract(int a, int b);
    int multiply(int a, int b);
    float divide(int a, int b);
}

#endif
EOF

cat > maker_so/src/calculator.cpp << 'EOF'
#include "calculator.h"

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int multiply(int a, int b) {
    return a * b;
}

float divide(int a, int b) {
    if (b == 0) return 0;
    return (float)a / b;
}
EOF

cat > maker_so/CMakeLists.txt << 'EOF'
cmake_minimum_required(VERSION 3.10)
project(maker_so)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
include_directories(${CMAKE_SOURCE_DIR}/include)
add_library(calculator SHARED src/calculator.cpp)
EOF

# 构建项目1
cd maker_so
mkdir -p build
cd build
cmake ..
cmake --build .
cd ../..

echo -e "\n=== 项目2: 使用 libcalculator.so ==="

# 创建项目2
mkdir -p user_so/{include,src,build,lib}

# 从项目1复制 .so 和头文件到项目2
cp maker_so/lib/libcalculator.so user_so/lib/
cp maker_so/include/calculator.h user_so/include/

# 创建项目2的文件
cat > user_so/src/main.cpp << 'EOF'
#include <iostream>
#include "calculator.h"

int main() {
    int a = 15, b = 4;
    
    std::cout << "=== Using calculator library ===\n";
    std::cout << a << " + " << b << " = " << add(a, b) << std::endl;
    std::cout << a << " - " << b << " = " << subtract(a, b) << std::endl;
    std::cout << a << " * " << b << " = " << multiply(a, b) << std::endl;
    std::cout << a << " / " << b << " = " << divide(a, b) << std::endl;
    
    return 0;
}
EOF

cat > user_so/CMakeLists.txt << 'EOF'
cmake_minimum_required(VERSION 3.10)
project(user_so)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set(CMAKE_INSTALL_RPATH "${CMAKE_SOURCE_DIR}/lib")
set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
include_directories(${CMAKE_SOURCE_DIR}/include)
link_directories(${CMAKE_SOURCE_DIR}/lib)
add_executable(calc_app src/main.cpp)
target_link_libraries(calc_app calculator)
EOF

# 构建项目2
cd user_so
mkdir -p build
cd build
cmake ..
cmake --build .
cd ../..

echo -e "\n=== 运行结果 ==="
./user_so/bin/calc_app

echo -e "\n=== 项目文件列表 ==="
echo "项目1生成的 .so:"
ls -l maker_so/lib/
echo -e "\n项目2使用的 .so:"
ls -l user_so/lib/
echo -e "\n项目2生成的可执行文件:"
ls -l user_so/bin/

手动构建步骤(如果没有脚本)

构建项目1

bash 复制代码
cd maker_so
mkdir build && cd build
cmake ..
make
cd ../..

复制文件到项目2

bash 复制代码
mkdir -p user_so/lib user_so/include
cp maker_so/lib/libcalculator.so user_so/lib/
cp maker_so/include/calculator.h user_so/include/

构建项目2

bash 复制代码
cd user_so
mkdir build && cd build
cmake ..
make
cd ../..

运行

bash 复制代码
./user_so/bin/calc_app

验证 .so 文件

bash 复制代码
# 查看 .so 的符号
nm -D user_so/lib/libcalculator.so

# 查看可执行文件的依赖
ldd user_so/bin/calc_app

# 运行程序
./user_so/bin/calc_app

这样就完成了:

  1. 项目1 生成了 libcalculator.so
  2. 项目2 使用了这个 .so 文件
  3. 所有文件都放在各自的项目目录下,互不干扰
相关推荐
Stick_ZYZ1 分钟前
从 Prompt 到 Context Engineering:Agent 真正稳定的关键
大数据·人工智能·算法·ai·prompt
隔窗听雨眠4 分钟前
ORM框架选型指南:MyBatis与Hibernate的全面对比
java·开发语言·数据库
ZHW_AI课题组4 分钟前
使用Stable Diffusion v1.5文本引导与无分类器引导(CFG)算法实现条件生成图片
人工智能·python·算法·机器学习·stable diffusion
j7~12 分钟前
【C++】类和对象(上)--带你全面理解类和对象的概念,以及this指针的理解和相关面试题
java·开发语言·封装·this指针·类的实例化·访问限定符·类的命名
于先生吖13 分钟前
同城物流创业项目,Java源码搭建多车型搬家拉货、就近配货预约小程序
java·开发语言·小程序
码不停蹄的玄黓14 分钟前
Java 异常分类
java·开发语言
黎阳之光16 分钟前
数字孪生赋能智慧油站建设|黎阳之光全场景可视化安防管控平台落地应用
大数据·物联网·算法·安全·数字孪生
江湖中的阿龙19 分钟前
23种设计模式
java·开发语言·设计模式
可可嘻嘻大老虎20 分钟前
SpringBoot拦截器防重复提交实战
java·spring boot·后端
cpp_250122 分钟前
P11375 [GESP202412 六级] 树上游走
数据结构·c++·算法·题解·洛谷·树形结构·gesp六级