cmake 静态调用 c++ dll 的类的一个例子(Clion IDE)
CMakeLists.txt
project(aaa) add_library(aaa SHARED aaa.cpp) add_executable(bbb bbb.cpp) target_link_libraries(bbb aaa)
aaa.h
#pragma once #ifndef AAA_AAA_H #define AAA_AAA_H #endif #ifdef BUILD_AAA_DLL #define IO_AAA_DLL __declspec(export) #else #define IO_AAA_DLL __declspec(import) #endif IO_AAA_DLL class father { public: void hello(void); double sum(double a, double b); };aaa.cpp
#define BUILD_AAA_DLL #include "aaa.h" #include <iostream> using namespace std; IO_AAA_DLL void father::hello(void) { cout << "Hello from dll.class!\n" << endl; } IO_AAA_DLL double father::sum(double a, double b) { return a + b; }bbb.cpp
#include "aaa.h" #pragma comment(a, "C:\Users\Perelman\.CLion2016.1\system\cmake\generated\aaa-4d5bae38\4d5bae38\Debug\libaaa.a") #include <iostream> using namespace std; int main() { father child; child.hello(); cout << child.sum(10, 20) << endl; return 0; }