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!
" << endl;
}

IO_AAA_DLL double father::sum(double a, double b)
{
    return a + b;
}

bbb.cpp

#include "aaa.h"
#pragma comment(a, "C:UsersPerelman.CLion2016.1systemcmakegeneratedaaa-4d5bae384d5bae38Debuglibaaa.a")
#include <iostream>

using namespace std;

int main()
{
    father child;
    child.hello();
    cout << child.sum(10, 20) << endl;
    return 0;
}

360截图20160613153603265 360截图20160613154002230

原文地址:https://www.cnblogs.com/blog-3123958139/p/5580855.html