Last active
December 6, 2016 08:36
-
-
Save yossi-tahara/cd98245db5e0d372c4c1a36b95b86e4f to your computer and use it in GitHub Desktop.
C++形式の動的リンク・ライブラリの書き方(msvc編)の付録
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
project(Test) | |
add_library(sub SHARED sub.cpp dll.h) | |
add_executable(main main.cpp dll.h) | |
target_link_libraries(main sub) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if !defined(DLL_H) | |
#define DLL_H | |
#ifdef DLL_BODY | |
#define DLL_EXPORT __declspec(dllexport) | |
#else | |
#define DLL_EXPORT __declspec(dllimport) | |
#endif | |
// privateメンバがないのでC4251警告が出ない | |
class Foo0 | |
{ | |
public: | |
int mData; | |
}; | |
// privateメンバがあるC4251警告が出るが、特に危険なクラスではない | |
class Foo1 | |
{ | |
int mData; | |
}; | |
// privateメンバがないのでC4251警告が出ないが、危険なクラスである!! | |
class /*DLL_EXPORT*/ Bar | |
{ | |
static int& getStaticInt() | |
{ | |
static int sStaticInt=1; | |
return sStaticInt; | |
} | |
public: | |
void set(int x) { getStaticInt()=x; } | |
int get() { return getStaticInt(); } | |
}; | |
#include <vector> | |
namespace std | |
{ | |
struct DLL_EXPORT _Container_base12; | |
template class DLL_EXPORT _Vector_val<std::_Simple_types<int>>; | |
template class DLL_EXPORT _Compressed_pair<std::_Wrap_alloc<std::allocator<int>>,std::_Vector_val<std::_Simple_types<int>>,true>; | |
template class DLL_EXPORT _Vector_alloc<std::_Vec_base_types<int,std::allocator<int>>>; | |
template class DLL_EXPORT _Vector_alloc<std::_Vec_base_types<int,allocator<int>>>; | |
template class DLL_EXPORT vector<int,allocator<int>>; | |
} | |
// exportされたクラス | |
class DLL_EXPORT Baz | |
{ | |
public: | |
Foo0 mFoo0; | |
Foo1 mFoo1; // C4251警告 | |
Bar mBar; | |
void set(int x) { mBar.set(x); } | |
int get() { return mBar.get(); } | |
std::vector<int> mVector; | |
}; | |
//extern thread_local int DLL_EXPORT gGlobalInt; | |
DLL_EXPORT int& getGlobalInt(); | |
#endif // DLL_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "dll.h" | |
int main(int argc, char* argv[]) | |
{ | |
Bar aBar; | |
Baz aBaz; | |
std::cout << "aBar.get()=" << aBar.get() << "\n"; | |
std::cout << "aBaz.get()=" << aBaz.get() << "\n"; | |
aBar.set(10); | |
aBaz.set(20); | |
std::cout << "aBar.get()=" << aBar.get() << "\n"; | |
std::cout << "aBaz.get()=" << aBaz.get() << "\n"; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define DLL_BODY | |
#include "dll.h" | |
thread_local int gGlobalInt=0; | |
int& getGlobalInt() | |
{ | |
return gGlobalInt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment