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

原文地址:https://www.cnblogs.com/blog-3123958139/p/5574514.html