C代码调用封装的C++代码,使用Cmakelist.txt将C++生成可调用的动态库windows

文章展示了如何创建一个C++的头文件(id_check_tool.h)用于提供外部调用的接口,并在对应的cpp文件中实现该接口。在C代码(main.c)中调用了这个接口进行功能执行。此外,CMakeLists.txt文件用于构建动态库,依赖于OpenCV、yaml-cpp、glog和ceres等库。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

提供外部调用的.h头文件

样例代码 id_check_tool.h

#ifndef _ERROR_CODE_WRAPPER_H__
#define _ERROR_CODE_WRAPPER_H__

#define EXPORT_API __declspec(dllexport)

#ifdef __cplusplus
extern "C" {
#endif

    EXPORT_API  extern void online_process(char *dir_img,char *dir_hash_table,char *dir_head_yaml,char *dir_left_yaml,char *dir_right_yaml, int left_right_hand, uint64_t *names, int *leds);//对外接口

#ifdef __cplusplus
}
#endif

#endif

对应外部调用.h头文件的.cpp代码

样例代码 id_check_tool.cpp

#include <iostream>
#include <core/id_generator.h> //项目中使用到的头文件
#include "id_check_tool.h" //对应对外提供的头文件

#ifdef __cplusplus
extern "C" {
#endif

void online_process(char *dir_img, char *dir_hash_table, char *dir_head_yaml, char *dir_left_yaml, char *dir_right_yaml,
                    int left_right_hand, uint64_t *names, int *leds) {
                    
    static nolo_hand::IDGenerator generator(dir_hash_table, dir_head_yaml);
    generator.set_sensor_type(left_right_hand);
    std::vector<std::string> v_names;
    std::vector<int> v_leds;
    std::string hand_calib_path;
    
     // c代码的char *转c++代码的string // 
    if (left_right_hand == 1) {
        hand_calib_path = std::string(dir_left_yaml);
    } else {
        hand_calib_path = std::string(dir_right_yaml);
    }

    generator.online_process(std::string(dir_img),
                             v_names,
                             v_leds,
                             hand_calib_path);
   // 校验输出内容 //          
    for (int i = 0; i < v_names.size(); ++i) {
        std::istringstream iss(v_names[i]);
        iss >> names[i];
        cout << v_names[i] << endl;
    }

    for (int i = 0; i < v_leds.size(); ++i) {
        leds[i] = v_leds[i];
        cout << v_leds[i];
    }
    cout << endl;

}

#ifdef __cplusplus
}
#endif

c代码调用

样例代码 main.c调用

#include <stdio.h>
#include <stdint.h>
#include "id_check_tool.h"
#include <inttypes.h>

int main(int argc, char *argv[]) {
    char *dir_img = "D:\\datasets\\led_uniformity_extract\\test_ui_datasets\\url2";
    char *dir_head = "./head.yaml";
    char *calib_left = "./left_hand.yaml";
    char *calib_right = "./right_hand.yaml";
    char *dir_hash_table = "./hash_table.bin";

    int left_right_hand = 2; 
    uint64_t names[4];
    int leds[15];
    online_process(dir_img,dir_hash_table,dir_head,calib_left,calib_right,left_right_hand, names, leds);

    printf("%" PRId64 "\n", names[0]);
    printf("%" PRId64 "\n", names[1]);
    printf("%" PRId64 "\n", names[2]);
    printf("%" PRId64 "\n", names[3]);

    for (int i = 0; i < 15; ++i) {
        printf("%d", leds[i]);
    }
}

CMakelist.txt生成动态库

cmake_minimum_required(VERSION 3.2)
project(blob_extract_tools)

set(CMAKE_BUILD_TYPE release)

set(SOURCES
        id_check_tool.h
        src/core/id_recognition.cpp
        src/core/id_recognition.h
        src/core/blob_detector.h
        src/core/blob_detector.cpp
        )

set(RECOGNITION_SRCS
        ${CMAKE_CURRENT_SOURCE_DIR}/src/fusion/M2Kalman.h
        ${CMAKE_CURRENT_SOURCE_DIR}/src/fusion/M2Kalman.cpp
        )

add_compile_definitions(NOMINMAX)
add_definitions(-DGLOG_NO_ABBREVIATED_SEVERITIES)

if (CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL "Debug"))
    set(THIRD_LIBRARY
            yaml-cppd.lib
            glogd.lib
            ceres-debug.lib)
else ()
    set(THIRD_LIBRARY
            yaml-cpp.lib
            glog.lib
            ceres.lib)
endif ()
message(STATUS "third librarys: ${THIRD_LIBRARY}")
include_directories(D:\\projectLib\\eigen\\eigen)

set(CMAKE_CXX_STANDARD 14)
set(OpenCV_DIR D:\\projectLib\\opencv\\build\\x64\\vc15\\lib)

find_package(OpenCV 4 REQUIRED)
include_directories(
        ${OpenCV_INCLUDE_DIRS}
        ${CMAKE_CURRENT_SOURCE_DIR}/src
        ${CMAKE_CURRENT_SOURCE_DIR}/3rd/include
)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/out)

link_directories(
        ${CMAKE_CURRENT_SOURCE_DIR}/3rd/libs/debug
        ${CMAKE_CURRENT_SOURCE_DIR}/3rd/libs/release
)

add_subdirectory(src/eva)

# 添加动态库使用到的所有代码文件
add_library(led_checkcpp SHARED id_check_tool.cpp ${SOURCES} ${RECOGNITION_SRCS})

# 动态库链接其他三方库
target_link_libraries(led_checkcpp
        ${OpenCV_LIBS}
        ${THIRD_LIBRARY})

add_executable(id_check_tool main.c)

# 链接动态库
target_link_libraries(id_check_tool
        led_checkcpp)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值