【问题标题】:How to organize Cmake file properly for the many source and headers? [duplicate]如何为许多源代码和头文件正确组织 Cmake 文件? [复制]
【发布时间】:2019-06-07 11:21:04
【问题描述】:

我知道这个问题已经被问过很多次了。我已经检查过它们,但我是 CMake 的新手。因此我无法弄清楚如何解决这个问题。

我的文件组织是这样的;

e0
|-CMakeLists.txt
|-grade.cpp
|-median.cpp
|-student_info.cpp
|-main.cpp
|-include
  |-grade.h
  |-median.h
  |-student_info.h

我的 CMakeLists.txt 文件如下图。

cmake_minimum_required (VERSION 2.6)

project(E0)

set(SOURCE_FILES main.cpp)
include_directories(headers)
add_executable(E0 main.cpp)
target_link_libraries(E0 headers)

当我运行 make 时,我已经面对了

/usr/bin/ld: cannot find -lheaders
collect2: error: ld returned 1 exit status
CMakeFiles/E0.dir/build.make:94: recipe for target 'E0' failed make[2]: *** 
[E0] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/E0.dir/all' 
failed
make[1]: *** [CMakeFiles/E0.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

您能帮我吗,如何编写 CMakeLists.txt 文件?

【问题讨论】:

  • target_link_libraries 只能用于链接到库(动态或静态)和其他 CMake 目标。只需删除该行。不过,您绝对应该将其他 .cpp 文件添加到您的 add_executable 语句中,以便实际编译代码。

标签: c++ cmake


【解决方案1】:

看起来您的CMakeLists.txt 应该是这样的:

cmake_minimum_required (VERSION 2.6)

project(E0)

set(SOURCE_FILES main.cpp grade.cpp median.cpp student_info.cpp)
include_directories(include)
add_executable(E0 ${SOURCE_FILES})

您确实应该阅读文档,而不是将随机语句放入您的文件中。

【讨论】:

  • 你能建议一个文档来学习 cmake 吗?
猜你喜欢
  • 2020-05-31
  • 1970-01-01
  • 2011-01-16
  • 2017-07-20
  • 1970-01-01
  • 2013-09-23
  • 2011-04-16
  • 1970-01-01
  • 2015-11-07
相关资源
最近更新 更多