CLion ソフトウェアでのクラスのカプセル化とパッケージ化、CMakelists 設定

目次

トピック

方法 1 (Circle クラスと Point クラスを別々の .cpp&.h ファイルに分割しない)

プロジェクトのディレクトリ構造

main.cpp 

CMakelists.txt の設定:

方法 2 (Circle クラスと Point クラスを別々の .cpp&.h ファイルに分割する)

プロジェクトのディレクトリ構造

main.cpp

クリクル.cpp

cricle.h

point.cpp

point.h

CMakelists.txt の設定:


トピック


 


方法 1 (Circle クラスと Point クラスを別々の .cpp&.h ファイルに分割しない)

プロジェクトのディレクトリ構造

main.cpp 

class Point {
private:
    int m_x;
    int m_y;
public:
    void setX(int x) {
        m_x = x;
    }

    void setY(int y) {
        m_y = y;
    }

    int getX() {
        return m_x;
    }

    int getY() {
        return m_y;
    }
};

class Circle {
private:
    double Pi = 3.1415;
    int m_R;
    Point m_centre;

public:
    void setR(int r) {
        m_R = r;
    }

    int getR() {
        return m_R;
    }

    void setCentre(Point &centre) {
        m_centre = centre;
    }


    //利用成员函数判断两个立方体是否相等
    int isInCircle (Point &c) {
        double L = sqrt((c.getX() - m_centre.getX())*(c.getX() - m_centre.getX()) + (c.getY() - m_centre.getY())*(c.getY() - m_centre.getY()));
        if (m_R > L) {
            return 0;
        }
        else if (m_R == L) {
            return 1;
        }
        else {
            return 2;
        }
    }
};


int main() {
    Point Yuanx;
    Yuanx.setX(10);
    Yuanx.setY(10);
    Circle Y;
    Y.setR(8);
    Y.setCentre(Yuanx);

    Point D;
    D.setX(10);
    D.setY(3);


    int ret2 = Y.isInCircle(D);
    if (ret2 == 0) {
        cout << "点在圆内" <<endl;
    }
    else if (ret2 == 1){
        cout << "点在圆上" <<endl;
    }
    else
    {
        cout << "点在圆外" <<endl;
    }

    system("pause");

    return 0;
}

CMakelists.txt の設定:

cmake_minimum_required(VERSION 3.21)
project(Setion2)

#在windows上exe文件需要libgcc才能运行,修改CMAKElist文件,添加让链接器静态链接libgcc和libstdc++的指令
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

include_directories(work)

add_executable(Setion3
        work/main.cpp
        work/main.h)

方法 2 (Circle クラスと Point クラスを別々の .cpp&.h ファイルに分割する)

プロジェクトのディレクトリ構造

main.cpp

#include <iostream>
using namespace std;
#include "test/cricle.h"
#include "test/point.h"

int main() {
    Point Yuanx;
    Yuanx.setX(10);
    Yuanx.setY(10);
    Circle Y;
    Y.setR(8);
    Y.setCentre(Yuanx);

    Point D;
    D.setX(10);
    D.setY(3);


    int ret2 = Y.isInCircle(D);
    if (ret2 == 0) {
        cout << "点在圆内" <<endl;
    }
    else if (ret2 == 1){
        cout << "点在圆上" <<endl;
    }
    else
    {
        cout << "点在圆外" <<endl;
    }

    system("pause");

    return 0;
}

クリクル.cpp

#include "cricle.h"


void Circle::setR(int r) {
    m_R = r;
}

int Circle::getR() {
    return m_R;
}

void Circle::setCentre(Point &centre) {
    m_centre = centre;
}


//利用成员函数判断两个立方体是否相等
int Circle::isInCircle (Point &c) {
    double L = sqrt((c.getX() - m_centre.getX())*(c.getX() - m_centre.getX()) + (c.getY() - m_centre.getY())*(c.getY() - m_centre.getY()));
    if (m_R > L) {
        return 0;
    }
    else if (m_R == L) {
        return 1;
    }
    else {
        return 2;
    }
}

cricle.h

#pragma once  
//为了避免同一个头文件被包含(include)多次,C/C++中有两种宏实现方式:一种是#ifdef endif方式;另一种是#pragma once方式。

在能够支持这两种方式的编译器上,二者并没有太大的区别。但两者仍然有一些细微的区别。
#include "point.h"
#include "math.h"

class Circle {

    private:
        int m_R;
        Point m_centre;

    public:
        void setR(int r);

        int getR();

        void setCentre(Point &centre);


        //利用成员函数判断两个立方体是否相等
        int isInCircle (Point &c);

};

point.cpp

#include "point.h"
#pragma once

void Point::setX(int x) {
    m_x = x;
}

void Point::setY(int y) {
    m_y = y;
}

int Point::getX() {
    return m_x;
}

int Point::getY() {
    return m_y;
}

point.h

#pragma once
//为了避免同一个头文件被包含(include)多次,C/C++中有两种宏实现方式:一种是#ifdef endif方式;另一种是#pragma once方式。

class Point {
private:
    int m_x;
    int m_y;
public:
    void setX(int x);

    void setY(int y);

    int getX();

    int getY();
};

CMakelists.txt の設定:

cmake_minimum_required(VERSION 3.21)
project(Setion3)

#在windows上exe文件需要libgcc才能运行,修改CMAKElist文件,添加让链接器静态链接libgcc和libstdc++的指令
set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")

#1 编译静态库
#这一步需要将项目目录路径work/test下的源文件编译为静态库,那么需要获取编译此静态库需要的文件列表,可以使用set命令,或者file命令来进行设置。比如:
file(GLOB_RECURSE TEST_LIB_SRC
        work/test/*.cpp
        )

#通过add_library命令编译名为test的静态库,库的类型是第二个参数STATIC指定的。如果指定为SHARED则编译的就是动态链接库。
add_library(test STATIC ${TEST_LIB_SRC})

#通过add_executable命令来往构建系统中添加一个可执行构建目标,同样需要指定编译需要的源文件。但是对于可执行文件来说,有时候还会依赖其他的库,则需要使用target_link_libraries命令来声明构建此可执行文件需要链接的库。
#在示例项目中,main.cpp就使用了work/test下实现的一些函数接口,所以依赖于前面构建的test库。所以在CMakeLists.txt中添加以下内容:

add_executable(demo work/main.cpp)
target_link_libraries(demo test)
#第一行说明编译可执行文件demo需要的源文件(可以指定多个源文件,此处只是以单个文件作为示例);第二行表明对math库存在依赖。

#[[
目录格式:
---Section3
    --CMakeLists.txt
    --work
        --test
            -cricle.cpp
            -cricle.h
            -point.cpp
            -point.h
        --main.cpp

项目的构建任务为:

将test目录编译成静态库,命名为test
编译main.cpp为可执行文件命名为demo,依赖test静态库
]]

おすすめ

転載: blog.csdn.net/Yang_4881002/article/details/127240501