C++ 将 Makefile 项目转换为 Cmake

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18680977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:07:52  来源:igfitidea点击:

Converting a Makefile project to Cmake

c++makefilecmake

提问by Harrison

I'm learning how to use cmake for this class but the documentation is extremely verbose and dense. A lot of the tutorials are either too simple to be useful (cmake with just one file) or too complicated.

我正在学习如何在这门课上使用 cmake,但文档非常冗长和密集。很多教程要么太简单而无用(cmake 只有一个文件)要么太复杂。

the original Makefile for the project looks like:

该项目的原始 Makefile 如下所示:

# Some optimization settings
# see: http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

# Standard all target
all: hw1_p2

# Simple program to do brute-force k-nearest neighbor searches against a signature file
hw1_p2: prob2.o ParseRecord.o Sorter.o Timing.o
        g++ -o hw1_p2 prob2.o ParseRecord.o Sorter.o Timing.o

prob2.o: prob2.cpp utility_templates.hpp
        g++ -Wall -c prob2.cpp

ParseRecord.o: ParseRecord.cpp ParseRecord.hpp utility_templates.hpp
        g++ -Wall -c ParseRecord.cpp

Sorter.o: Sorter.cpp Sorter.hpp
        g++ -Wall -c Sorter.cpp

# Timing class
Timing.o: Timing.hpp Timing.cpp
        g++ -Wall -c Timing.cpp

# Clean code-derived files
clean:
        rm -f *.o hw1_p2 

The project is organized like this

该项目是这样组织的

  • cmakelearn
    • CMakeLists.txt
    • build
    • src
      • CMakeLists.txt
      • ParseRecord.hpp
      • Timing.cpp
      • small.dat
      • Makefile
      • Sorter.cpp
      • Timing.hpp
      • utility_templates.hpp
      • ParseRecord.cpp
      • Sorter.hpp
      • prob2.cpp
  • 学习
    • CMakeLists.txt
    • 建造
    • 源文件
      • CMakeLists.txt
      • 解析记录文件
      • 计时.cpp
      • 小数据
      • 生成文件
      • 排序器.cpp
      • 时序.hpp
      • 实用程序模板.hpp
      • 解析记录文件
      • 排序器.hpp
      • 问题2.cpp

So far, from the reading that I've done I understand that you need a CMakelists.txt file in every directory. The build folder is there so that I can go into it and run cmake ..

到目前为止,根据我所做的阅读,我了解到您需要在每个目录中都有一个 CMakelists.txt 文件。build 文件夹在那里,这样我就可以进入它并运行 cmake ..

In the top-level CMakelists.txt file I have

在顶级 CMakelists.txt 文件中,我有

cmake_minimum_required (VERSION 2.6)
project (CMAKETEST)
add_subdirectory(src)

and in the src dir CMakelist.txt here is my attempt:

在 src 目录 CMakelist.txt 中,这是我的尝试:

include_directories(${CMAKETEST_SOURCE_DIR}/src)
link_directories(${CMAKETEST_BINARY_DIR}/src)

add_executable(hw1_p2 prob2.cpp ParseRecord.cpp Sorter.cpp Timing.cpp)

I don't even really know where to begin other than that. This comment from a tutorial pretty much sums up my thoughts about cmake and the documentation right now:

除此之外,我什至不知道从哪里开始。这个来自教程的评论几乎总结了我现在对 cmake 和文档的想法:

Not nearly detailed enough to be useful. Having said that, I don't like CMake much anyway; too many separate, arbitrary Things you Just Have to Know. At least that's my impression. Like, ‘add_definitions(-std=c99)'. Really? Does that name jump out at you as how to add flags to a compiler? And is ‘set(VAR a b c)' any more intuitive than VAR=a b c or some such? Beh on the article, and on CMake. Yet I know CMake is taking the build-world by storm.

不够详细以至于没有用。话虽如此,反正我不太喜欢 CMake;您只需要知道太多独立的、随意的事情。至少这是我的印象。比如,'add_definitions(-std=c99)'。真的吗?这个名字是否让您想到如何向编译器添加标志?'set(VAR abc)' 是否比 VAR=abc 或类似的更直观?Beh 在文章和 CMake 上。然而我知道 CMake 正在风靡建筑界。

Still, I really want to learn and figure this out so any help that you can provide will be much appreciated and helpful. Thanks in advance.

尽管如此,我真的很想学习并解决这个问题,因此您可以提供的任何帮助都将受到赞赏和帮助。提前致谢。

采纳答案by David Brown

The add_executablecommand is for building executables, but you seem to be trying to use it to build object (.o) files as well. Since you are only building one executable you only need one add_executablecommand:

add_executable命令用于构建可执行文件,但您似乎.o也在尝试使用它来构建对象 ( ) 文件。由于您只构建一个可执行文件,因此您只需要一个add_executable命令:

add_executable(hw1_p2 prob2.cpp ParseRecord.cpp Sorter.cpp Timing.cpp)

回答by user1283078

You do not have to create cmakelists.txt files in every subdirectory. For a project that simple it should be enough to put everything into the toplevel cmakelists.txt

您不必在每个子目录中创建 cmakelists.txt 文件。对于这么简单的项目,将所有内容放入顶级 cmakelists.txt 就足够了

cmake_minimum_required (VERSION 2.6)
project (CMAKETEST C CXX)

add_executable(hw1_p2 src/prob2.cpp src/ParseRecord.cpp src/Sorter.cpp src/Timing.cpp)

this simple cmakelists.txt should get the job done.

这个简单的 cmakelists.txt 应该可以完成工作。