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> using namespace std; IO_XXX_DLL void hello(void) { cout<<"Hello from dll!"<<endl; cin.get(); }
yyy.cpp
#include <windows.h> #include <iostream> using namespace std; int main() { HINSTANCE h = LoadLibrary("C:\Users\Perelman\.CLion2016.1\system\cmake\generated\xxx-4d5c076f\4d5c076f\Debug\libxxx.dll"); typedef void (*p)(void); p f = (p)GetProcAddress(h, "hello"); f(); FreeLibrary(h); return 0; }