使用CMake构建库
Build library with CMake.
一个简单的类
// operations.hpp
#ifndef CMAKEHELLO_OPERATIONS_HPP
#define CMAKEHELLO_OPERATIONS_HPP
namespace math {
class operations{
public:
int sum(const int &a, const int &b);
int mult(const int &a, const int &b);
int div(const int &a, const int &b);
int sub(const int &a, const int &b);
};
}
#endif //CMAKEHELLO_OPERATIONS_HPP
// operations.cpp
#include <exception>
#include <stdexcept>
#include <iostream>
#include "operations.hpp"
int math::operations::sum(const int &a, const int &b){
return a + b;
}
int math::operations::mult(const int &a, const int &b){
return a * b;
}
int math::operations::div(const int &a, const int &b){
if(b == 0){
throw std::overflow_error("Divide by zero exception");
}
return a/b;
}
int math::operations::sub(const int &a, const int &b){
return a - b;
}Library和Target一起编译
单独编译math::operations
.so VS .a
编译成sub-module
Last updated