时间:2024-4-11 作者:benojan 分类: c/c++
// calc.h
#ifdef EXPORT_DLL
#define CALC_API __declspec(dllexport)
#else
#define CALC_API __declspec(dllimport)
#endif
extern "C"
{
CALC_API int 加(int a, int b);
CALC_API int 减(int a, int b);
CALC_API int 乘(int a, int b);
CALC_API double 除(double a, double b);
}
// calc.cpp
#define EXPORT_DLL
#include "calc.h"
extern "C"
{
CALC_API int 加(int a, int b)
{
return a + b;
}
CALC_API int 减(int a, int b)
{
return a - b;
}
CALC_API int 乘(int a, int b)
{
return a * b;
}
CALC_API double 除(double a, double b)
{
return a / b;
}
}
// CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(calc)
set(CMAKE_CXX_STANDARD 11)
if(MSVC)
add_compile_options("/source-charset:utf-8")
endif()
include_directories(./include)
if(MSVC)
add_compile_options("/source-charset:utf-8")
endif()
file(GLOB SOURCES "./src/*.cpp")
add_library(${PROJECT_NAME} SHARED ${SOURCES})
// useCalc.cpp
#include <iostream>
#include "calc.h"
using namespace std;
int main()
{
cout << 加(3, 4) << endl;
}
// CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(useCalc)
set(CMAKE_CXX_STANDARD 11)
if(MSVC)
add_compile_options("/source-charset:utf-8")
endif()
file(GLOB SOURCES "src/*.cpp")
include_directories(include)
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} calc)