cmake 编译 c++ dll 的一个例子(更新1)

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; 
}

1 360截图20160611132942849

 

 

 

 

 

 

 

 

 

 

 

 

 
posted @ 2016-06-11 13:35  佩雷尔曼的信徒  阅读(1122)  评论(0编辑  收藏  举报