栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

[c++] CMakeLists教程与代码示例

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[c++] CMakeLists教程与代码示例

CMakeLists实例

提示:细节与注意事项先留坑,日后补充。


文章目录
  • CMakeLists实例
  • 前言
  • 一、CMakeLists编写细节
  • 二、程序框架
  • 三、代码示例
  • 总结


前言

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


一、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 
#include 
#include 
#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& vec, std::ofstream* ofil=0)
{
    for(int ix=0; ixvec[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 vec, std::ostream& os)
{
    for(int ix=0; ix 

Essential/bubble_sort/NumricSeq.h

void display(std::vector, std::ostream& = std::cout);
void swap(int&, int&, std::ofstream*);
void bubble_sort(std::vector&, 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 
#include 
#include 
#include "bubble_sort/NumericSeq.h"

int main()
{
    int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2};
    std::vector 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);
}

总结

提示:这里对文章进行总结:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/879215.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号