使用Makefile和CMake编译代码的区别

2021-12-26 00:00:00 c makefile cmake c++

我在 C/C++ 上编写代码并使用 (GNU) Makefile 来编译代码.我可以用 CMake 做同样的事情并得到一个 MakeFile.但是,使用 Makefile 和 CMake 编译代码有什么区别?

I code on C/C++ and use a (GNU) Makefile to compile the code. I can do the same with CMake and get a MakeFile. However, what is the difference between using Makefile and CMake to compile the code?

推荐答案

Make(或者更确切地说是 Makefile)是一个构建系统 - 它驱动编译器和其他构建工具来构建您的代码.

Make (or rather a Makefile) is a buildsystem - it drives the compiler and other build tools to build your code.

CMake 是构建系统的生成器.它可以生成 Makefile,可以生成 Ninja 构建文件,可以生成 KDEvelop 或 Xcode 项目,可以生成 Visual Studio 解决方案.从相同的起点,相同的 CMakeLists.txt 文件.因此,如果您有一个独立于平台的项目,CMake 也是一种使其独立于构建系统的方法.

CMake is a generator of buildsystems. It can produce Makefiles, it can produce Ninja build files, it can produce KDEvelop or Xcode projects, it can produce Visual Studio solutions. From the same starting point, the same CMakeLists.txt file. So if you have a platform-independent project, CMake is a way to make it buildsystem-independent as well.

如果您的 Windows 开发人员习惯了 Visual Studio 和 Unix 开发人员,他们对 GNU Make 发誓,那么 CMake 是(其中一种)可行的方法.

If you have Windows developers used to Visual Studio and Unix developers who swear by GNU Make, CMake is (one of) the way(s) to go.

如果您希望您的项目是多平台或广泛使用的,我总是建议使用 CMake(或其他构建系统生成器,但 CMake 是我个人的偏好).CMake 本身也提供了一些不错的功能,例如依赖项检测、库接口管理或与 CTest、CDash 和 CPack 的集成.

I would always recommend using CMake (or another buildsystem generator, but CMake is my personal preference) if you intend your project to be multi-platform or widely usable. CMake itself also provides some nice features like dependency detection, library interface management, or integration with CTest, CDash and CPack.

使用构建系统生成器可以让您的项目更加面向未来.即使您现在只使用 GNU-Make,如果您以后决定扩展到其他平台(无论是 Windows 还是嵌入式平台),或者只想使用 IDE,该怎么办?

Using a buildsystem generator makes your project more future-proof. Even if you're GNU-Make-only now, what if you later decide to expand to other platforms (be it Windows or something embedded), or just want to use an IDE?

相关文章