VS+CMake生成动态库

3 篇文章 0 订阅

说明,一个简短的小demo,记录下流程。

一 代码

源文件- C11线程池

#ifndef THREAD_POOL_H
#define THREAD_POOL_H

#include <vector>
#include <queue>
#include <memory>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <future>
#include <functional>
#include <stdexcept>

class ThreadPool {
public:
    ThreadPool(size_t num = 4);
    template<class F, class... Args>
    auto enqueue(F&& f, Args&&... args) 
        -> std::future<typename std::result_of<F(Args...)>::type>;
    ~ThreadPool();
private:
    // need to keep track of threads so we can join them
    std::vector< std::thread > workers;
    // the task queue
    std::queue< std::function<void()> > tasks;
    
    // synchronization
    std::mutex queue_mutex;
    std::condition_variable condition;
    bool stop;
};
 
// the constructor just launches some amount of workers
inline ThreadPool::ThreadPool(size_t  num )
    :   stop(false)
{
    for(size_t i = 0;i< num;++i)
        workers.emplace_back(
            [this]
            {
                for(;;)
                {
                    std::function<void()> task;

                    {
                        std::unique_lock<std::mutex> lock(this->queue_mutex);
                        this->condition.wait(lock,
                            [this]{ return this->stop || !this->tasks.empty(); });
                        if(this->stop && this->tasks.empty())
                            return;
                        task = std::move(this->tasks.front());
                        this->tasks.pop();
                    }

                    task();
                }
            }
        );
}

// add new work item to the pool
template<class F, class... Args>
auto ThreadPool::enqueue(F&& f, Args&&... args) 
    -> std::future<typename std::result_of<F(Args...)>::type>
{
    using return_type = typename std::result_of<F(Args...)>::type;

    auto task = std::make_shared< std::packaged_task<return_type()> >(
            std::bind(std::forward<F>(f), std::forward<Args>(args)...)
        );
        
    std::future<return_type> res = task->get_future();
    {
        std::unique_lock<std::mutex> lock(queue_mutex);

        // don't allow enqueueing after stopping the pool
        if(stop)
            throw std::runtime_error("enqueue on stopped ThreadPool");

        tasks.emplace([task](){ (*task)(); });
    }
    condition.notify_one();
    return res;
}

// the destructor joins all threads
inline ThreadPool::~ThreadPool()
{
    {
        std::unique_lock<std::mutex> lock(queue_mutex);
        stop = true;
    }
    condition.notify_all();
    for(std::thread &worker: workers)
        worker.join();
}

#endif

10年前的东西了,我还是写不出来。。。。。。
测试代码-头文件


#define DllExport   __declspec( dllexport )//宏定义

DllExport int show();
DllExport int test();

源文件

#include "test.h"
#include <iostream>
#include "ThreadPool.h"
int show()
{
	std::cout<<"hello world"<<std::endl;
	return 1;
}
static ThreadPool pool;

int test()
{
	return pool.enqueue(show).get();
}

CMakeLists

CMAKE_MINIMUM_REQUIRED(VERSION 3.1.1)
#解决方案名称
PROJECT(ThreadPoolDll)
#添加头文件路径
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
#添加源文件
AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} SRC_LIST)
#生成动态库
ADD_LIBRARY(DLL SHARED ${SRC_LIST})
#设置动态库存储路径
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

路径
在这里插入图片描述

二 步骤

进入到build文件夹,然后进入cmd中,

cmake …

在这里插入图片描述

然后打开叫DLL的解决方案,编就好了,记得编Release的。
用dumpbin检查一下

dumpbin -exports DLL.dll

检查导出符号,如果提示dumpbin 不是内部,外部符号啥的。
找到这两个文件
在这里插入图片描述
拖到命令行中,回车等下就好了。
在这里插入图片描述

三 测试

写个控制台就好了

#include <iostream>
#include "test.h"

int main()
{
    std::cout << test() << std::endl;
    std::cout << "Hello World!\n";
    return 0;
}

1 项目-属性-C/C++ -常规-附加包含目录包含库的头文件
2 项目-属性-连接器-常规-附加库目录lib文件的位置
3 项目-属性-连接器-输入-附加依赖性,添加库的名字,这里是DLL.lib

运行-通过
在这里插入图片描述

四 其他

上述编译环境为VS2019,如果VS2015跑不起来,试试把测试文件中的全局静态变量
static ThreadPool pool; 换成局部的试一下,反正我的2015跑不起来。最后用别的方法搞了

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值