[c++] CMakeLists代码示例

CMakeLists实例



在这里插入图片描述

前言

利用Essential c++ ch2中冒泡排序法作为例,搭建相关的CMakeLists结构。主要涉及,程序结构、静态库的建立、库与目标文件建立联系、各个变量以及关键字的使用与注意事项。


程序框架

  • Essential
    • CMakeLists.txt
    • bubble_sort
      • CMakeLists.txt
      • NumricSeq.cpp
      • NumricSeq.h
    • samples
      • CMakeLists.txt
      • ch2
        • bubble.cpp
        • CMakeLists.txt

代码示例

Essential/CMakeLists.txt

cmake_minimum_required(VERSION 3.22.0)
project(InvokeFunction)

add_subdirectory(bubble_sort)
add_subdirectory(samples)

Essential/bubble_sort/CMakeLists.txt

project(bubble_sort)

set(HEADERS
    ${CMAKE_CURRENT_SOURCE_DIR}/NumericSeq.h
)

set(SOURCE
    ${CMAKE_CURRENT_SOURCE_DIR}/NumericSeq.cpp
)

add_library(bubble_sort ${HEADERS} ${SOURCE})

Essenetial/bubble/NumricSeq.cpp

#include <fstream>
#include <vector>
#include <iostream>
#include "NumericSeq.h"

void swap(int& val1, int& val2, std::ofstream* ofil=0)
{
    if(ofil != 0)
    {
        *ofil << "swap(" << val1
         << "," << val2 << " )\n";
    }

    int temp = val1;
    val1 = val2;
    val2 = temp;

    if(ofil != 0)
    {
        *ofil << "after swap(): val1" << val1
             << " val2: " << val2 << "\n";
    }
}

void bubble_sort(std::vector<int>& vec, std::ofstream* ofil=0)
{
    for(int ix=0; ix<vec.size(); ix++)
    {
        for(int jx = ix+1; jx<vec.size(); jx++)
        {
            if(vec[ix]>vec[jx])
            {
                // 调试用的输出信息
                if(ofil != 0)
                {
                    (*ofil) << "about to call swap!"
                                        << " ix: " << ix << " jx: " << jx << '\t'
                                        << " swapping: " << vec[ix]
                                        << " with " << vec[jx] << std::endl;
                }
                swap(vec[ix], vec[jx], ofil);
                display(vec);
            }
        }
    }
}

void display(std::vector<int> vec, std::ostream& os)
{
    for(int ix=0; ix<vec.size(); ix++)
    {
        os << vec[ix] << " ";
    }
    os << std::endl;
}

Essential/bubble_sort/NumricSeq.h

void display(std::vector<int>, std::ostream& = std::cout);
void swap(int&, int&, std::ofstream*);
void bubble_sort(std::vector<int>&, std::ofstream*);

Essential/samples/CMakeLists.txt

add_subdirectory(ch2)

Esstial/samples/ch2/CMakeLists.txt

project(ch2)

include_directories("../..")

set(BUBBLE
    bubble.cpp
)
add_executable(bubble ${BUBBLE})
target_link_libraries(bubble bubble_sort)

Essential/samples/ch2/bubble.cpp

#include <iostream>
#include <vector>
#include <fstream>
#include "bubble_sort/NumericSeq.h"

int main()
{
    int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2};
    std::vector<int> vec(ia, ia+8);   //  利用数组初始化vector的方法
    std::ofstream ofil("text_out1");

    std::cout << "vector before sort: ";
    display(vec);

    bubble_sort(vec, &ofil);

    std::cout << "vector after sort: ";
    display(vec);
}

总结

相关命令含义请参考:https://blog.csdn.net/LY_970909/article/details/124833796?spm=1001.2014.3001.5502

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值