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 Span style= "COLOR: #800000" > " c " Span style= "COLOR: #000000" > {io_xxx_dll void hello (void ); }
Xxx.cpp
#define"xxx.h"<iostream> using namespace std; void Hello (void) { cout<<"Hello from dll! "<<Endl; Cin. Get (); }
Yyy.cpp
#include <windows.h>#include<iostream>using namespacestd; intMain () {hinstance h= LoadLibrary ("c:\\users\\perelman\\. Clion2016.1\\system\\cmake\\generated\\xxx-4d5c076f\\4d5c076f\\debug\\libxxx.dll"); typedefvoid(*p) (void); P F= (p) GetProcAddress (H,"Hello"); f (); FreeLibrary (h); return 0; }
CMake An example of compiling a C + + DLL (update 1)