• cmake 编译 c++ dll 的一个例子


    CMakeLists.txt

    project(xxx)
    add_library(xxx SHARED xxx.cpp)
    add_executable(yyy yyy.cpp)
    target_link_libraries(yyy xxx)

    xxx.h

    #ifndef XXX_XXX_H
    #define XXX_XXX_H
    #endif
    
    #pragma once
    #ifdef BUILD_XXX_DLL
    #define IO_XXX_DLL __declspec(export)
    #else
    #define IO_XXX_DLL __declspec(import)
    #endif
    
    extern "C"
    {
    IO_XXX_DLL void hello(void);
    }

    xxx.cpp

    #define BUILD_XXX_DLL
    #include "xxx.h"
    #include <iostream>
    
    IO_XXX_DLL void hello(void)
    {
        std::cout<<"Hello from dll!"<<std::endl;
        std::cin.get();
    }

    yyy.cpp

    #include <windows.h>
    #include "xxx.h"
    int main()
    {
        HINSTANCE handle = LoadLibrary("C:\Users\Perelman\.CLion2016.1\system\cmake\generated\xxx-4d5c076f\4d5c076f\Debug\libxxx.dll");
        typedef void (*pointer)(void);
        pointer f;
        f = (pointer)GetProcAddress(handle, "hello");
        f();
        FreeLibrary(handle);
        return 0;
    }

    360截图20160611000228834 360截图20160611000335691

  • 相关阅读:
    时间选择框(可用于Form)
    点击复制指定内容
    ajax中多个模板之间套用ajax
    Java学习路径
    Windows平台安装Python
    Python语法-第2关
    Python语法-第1关
    Python语法-第0关
    图像识别
    wx:for用法
  • 原文地址:https://www.cnblogs.com/blog-3123958139/p/5574514.html
Copyright © 2020-2023  润新知