文件结构如下: —bin —lib —include ——Multiply.h —src ——Multiply. cpp ——main.cpp —CMakeLists.txt
—include ——Multiply.h的文件代码如下:
#ifndef _Multiply_H_#define _Multiply_H_class Multiply{public: Multiply(); ~Multiply(); int multiply(int a, int b);};#endif—src ——Multiply.cpp的文件代码如下:
#include"Multiply.h"Multiply::Multiply(){ ;}Multiply::~Multiply(){ ;}int Multiply::multiply(int a, int b){ return a*b;}—src ——main.cpp的文件代码如下:
#include<iostream>#include<opencv2/opencv.hpp>#include"Multiply.h"using namespace std;int main(){ int a,b; cin>>a>>b; Multiply mul; cout<<mul.multiply(a,b)<<endl; cout<<"Start OpenCV"<<endl; cv::Mat img1; img1=cv::imread("./src/main_page.png"); if(img1.data==NULL) { cout<<"Error"<<endl; return -1; } cv::namedWindow("source_image", CV_WINDOW_NORMAL); cv::imshow("source_image", img1); cv::waitKey(0); return 0;}—src ——CMakeLists.txt的文件代码编写如下:
# 增加opencv的依赖find_package(OpenCV REQUIRED)# 添加opencv的包含目录include_directories(${OpenCV_INCLUDE_DIRS})# 查找当前目录下的所有源文件,并将名称保存到DIR_SRCS变量aux_source_directory(. DIR_SRCS)# 指定生成的目标add_executable(Demo ${DIR_SRCS})# 链接OpenCV库target_link_libraries(Demo ${OpenCV_LIBS})—CMakeLists.txt的代码编写如下:
# cmake最低版本要求cmake_minimum_required (VERSION 2.8)# 项目信息PRoject(Demo4)# 设定可执行二进制文件的目录set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)# 设定编译得到的库文件目录set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)# 将库文件目录作为连接的目录link_directories(${PROJECT_SOURCE_DIR}/lib)# 设定头文件目录include_directories(${PROJECT_SOURCE_DIR}/include)# 添加src目录作为继续构建add_subdirectory(${PROJECT_SOURCE_DIR}/src)新闻热点
疑难解答