当前位置: 首页 > news >正文

网站开发收费重庆网

网站开发收费,重庆网,网站不用模板如何更新文章,手机怎么防止网站自动跳转使用一个简单的示例来应用cmake&#xff0c;无任何三方库的单一的应用程序项目 你可以收获 使用cmake生成VS项目生成mingw项目(makefile) 1 首先新建一个cpp&#xff0c;我们要做一个控制台应用程序 #include<iostream> void main(){std::cout<<"hello cm…

使用一个简单的示例来应用cmake,无任何三方库的单一的应用程序项目

你可以收获

  • 使用cmake生成VS项目
  • 生成mingw项目(makefile)

1 首先新建一个cpp,我们要做一个控制台应用程序

#include<iostream>
void main(){std::cout<<"hello cmake\n";
}

下面是CMakeLists.txt,要和main.cpp放在同一目录下
当你只有一个文件 main.cpp 并想要生成一个可执行程序时,你的 CMakeLists.txt 文件可以非常简单。以下是一个示例:

# 设置 CMake 最低版本要求
cmake_minimum_required(VERSION 3.12)# 项目名称
project(MyExecutable)# 添加可执行文件
add_executable(my_executable main.cpp)

这个 CMakeLists.txt 文件包含了三个主要部分:

  1. cmake_minimum_required(VERSION 3.12):指定 CMake 的最低版本要求。这个版本号可以根据你的需求进行修改。

  2. project(MyExecutable):定义项目名称。在这里,项目名被设置为 “MyExecutable”,你可以根据需要修改。

  3. add_executable(my_executable main.cpp):添加可执行文件。这一行告诉 CMake 创建一个名为 my_executable 的可执行文件,它的源代码是 main.cpp。确保 main.cpp 文件与 CMakeLists.txt 文件在同一目录下,或者根据实际情况提供正确的路径。

2 在项目目录中创建一个 build 目录,并在其中运行 CMake 和构建命令:

mkdir build
cd build
cmake ..
cmake --build .

这样就会在 build 目录中生成一个名为 my_executable 的可执行文件。
cmake …:配置项目,生成构建系统文件。

cmake --build .:使用生成的构建系统文件实际构建项目。这个可不用执行,因为VS提供可视化的编译。只用打开生成的sln文件,再使用VSbuild。

执行之后

PS E:\workspace\cmake_demo\simple_demo\build> cmake ..
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.22000.0 to target Windows 10.0.22631.
-- The C compiler identification is MSVC 19.29.30153.0
-- The CXX compiler identification is MSVC 19.29.30153.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: E:/VS/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: E:/VS/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (6.3s)
-- Generating done (0.0s)
-- Build files have been written to: E:/workspace/cmake_demo/simple_demo/build
PS E:\workspace\cmake_demo\simple_demo\build> cmake --build .
用于 .NET Framework 的 Microsoft (R) 生成引擎版本 16.11.2+f32259642
版权所有(C) Microsoft Corporation。保留所有权利。my_executable.vcxproj -> E:\workspace\cmake_demo\simple_demo\build\Debug\my_executable.exeBuilding Custom Rule E:/workspace/cmake_demo/simple_demo/CMakeLists.txt
PS E:\workspace\cmake_demo\simple_demo\build>

build 目录下新增了如下文件:

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        2023/12/16      2:50                CMakeFiles
d-----        2023/12/16      2:44                Debug
d-----        2023/12/16      2:44                my_executable.dir
d-----        2023/12/16      2:44                x64
-a----        2023/12/16      2:42          38740 ALL_BUILD.vcxproj
-a----        2023/12/16      2:42            290 ALL_BUILD.vcxproj.filters
-a----        2023/12/16      2:44            168 ALL_BUILD.vcxproj.user
-a----        2023/12/16      2:42          13988 CMakeCache.txt
-a----        2023/12/16      2:42           1459 cmake_install.cmake
-a----        2023/12/16      2:42           3126 MyExecutable.sln
-a----        2023/12/16      2:42          48205 my_executable.vcxproj
-a----        2023/12/16      2:42            583 my_executable.vcxproj.filters
-a----        2023/12/16      2:44            168 my_executable.vcxproj.user
-a----        2023/12/16      2:42          38830 ZERO_CHECK.vcxproj
-a----        2023/12/16      2:42            533 ZERO_CHECK.vcxproj.filters

其中ALL_BUILD和ZERO_CHECK是cmake附加的,一个是可以编译所有项目的项目,一个是重新生成VS项目

3 刚刚生成了VS的文件,现在生成makefile文件

使用-G命令来指定编译器
-G <generator-name> = Specify a build system generator.
使用cmake -h来查看帮助,看看编译器的名字

GeneratorsThe following generators are available on this platform (* marks default):Visual Studio 17 2022        = Generates Visual Studio 2022 project files.Use -A option to specify architecture.
* Visual Studio 16 2019        = Generates Visual Studio 2019 project files.Use -A option to specify architecture.MinGW Makefiles              = Generates a make file for use withmingw32-make.
(多余的我就删去了,不然太长)

以下是一个在 MinGW 下使用 Makefile 的示例:

mkdir build_mingw
cd build_mingw
cmake -G "MinGW Makefiles" ..

执行之后:

PS E:\workspace\cmake_demo\simple_demo\build_mingw> cmake -G "MinGW Makefiles" ..
-- The C compiler identification is GNU 8.1.0
-- The CXX compiler identification is GNU 8.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: E:/MinGW/mingw64/bin/gcc.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: E:/MinGW/mingw64/bin/c++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (2.4s)
-- Generating done (0.0s)
-- Build files have been written to: E:/workspace/cmake_demo/simple_demo/build_mingw

至此以及生成了makefile

然后,你可以使用 mingw32-make 命令来构建项目:
生成了可执行文件

[ 50%] Building CXX object CMakeFiles/my_executable.dir/main.cpp.obj
[100%] Linking CXX executable my_executable.exe
[100%] Built target my_executable

在makefile的环境下因为没有IDE可视化的支持,因此一般将cmake 和make(mingw32-make)连用,在Cmake生成makefile时就指定64位还是32位,debug还是release。
cmake -G "MinGW Makefiles" -DCMAKE_C_FLAGS=-m64 -DCMAKE_BUILD_TYPE=Release ..
64可以换成32,Release 可以换成Debug

后面的文章中在讨论项目配置的问题,这篇文章主要是体会一下cmake生成和编译的步骤

http://www.tj-hxxt.cn/news/9619.html

相关文章:

  • 设计手机网站热门搜索关键词
  • 做营销网站seo百度搜索热度排名
  • 企业管理咨询服务是什么seo自学网
  • .ent做的网站有哪些seo营销服务
  • app网站模板西地那非能提高硬度吗
  • 怎么用mvc架构做网站全媒体广告代理加盟
  • 河南简介网站设计朝阳网络推广
  • 浙江省住房和城乡建设行业网站seo咨询服务价格
  • php做彩票网站网店运营推广登录入口
  • 怎么看公司网站做的好不好哦seo课程排行榜
  • 个人名义做网站能备案吗app开发网站
  • 怎么做淘宝卷网站泉州seo
  • 高大上网站设计成都最新消息今天
  • wordpress capability manager优化网络培训
  • 兰州做网站哪家专业php免费开源crm系统
  • 白佛网站建设做网站的外包公司
  • 青岛大型门户网站建设推广百度推广图片
  • 泸州网站公司楚雄今日头条新闻
  • 网站开发开发语言百度帐号申请注册
  • 中铁建设集团有限公司门户网站整合网络营销是什么
  • 国际交友网站做英文客服百度竞价一个月5000够吗
  • 下载wordpress建站程序seo网站地图
  • 免费网站制作软件的app疫情防控最新信息
  • 专门做qq小工具的网站想做推广哪个平台好
  • 怎么将自己房子投入网站做民宿数据分析一般用什么软件
  • 广州网站建设优化百度搜索推广方案
  • 北海做网站注册城乡规划师教材
  • 注册公司在哪个网站注册南京seo建站
  • 崔凯 本地wordpress昆明seo外包
  • 乐清网站建设服务域名注册需要多久