最新的新开传奇网站,东莞短视频推广是哪个,成都私人做网站建设的公司,平台推广图片使用一个简单的示例来应用cmake#xff0c;无任何三方库的单一的应用程序项目
你可以收获
使用cmake生成VS项目生成mingw项目(makefile)
1 首先新建一个cpp#xff0c;我们要做一个控制台应用程序
#includeiostream
void main(){std::couthello cm…使用一个简单的示例来应用cmake无任何三方库的单一的应用程序项目
你可以收获
使用cmake生成VS项目生成mingw项目(makefile)
1 首先新建一个cpp我们要做一个控制台应用程序
#includeiostream
void main(){std::couthello 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 文件包含了三个主要部分 cmake_minimum_required(VERSION 3.12)指定 CMake 的最低版本要求。这个版本号可以根据你的需求进行修改。 project(MyExecutable)定义项目名称。在这里项目名被设置为 “MyExecutable”你可以根据需要修改。 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.2f32259642
版权所有(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\buildbuild 目录下新增了如下文件
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_TYPERelease .. 64可以换成32Release 可以换成Debug
后面的文章中在讨论项目配置的问题这篇文章主要是体会一下cmake生成和编译的步骤